Problem

You have a huge number of files (50000)  in a single directory. You need to automatically delete the ones which contain a specific text, i.e. “SPAM”.

Analysis

You can not simply use rm combined with grep, because the argument list will be too long and you will get an error such as:

Solution

Use the following script:

#!/bin/bash
for file in `find . | xargs grep -l "SPAM"`
do
  rm -v $file
done
exit 0

Copy the text to a text file and execute it.

bash: Removing large number of files which contain specific text

Leave a Reply

Your email address will not be published. Required fields are marked *