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”.


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:


Use the following script:

for file in `find . | xargs grep -l "SPAM"`
  rm -v $file
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 *