Delete files with string found in file - linux cli

Spechal picture Spechal · Dec 25, 2010 · Viewed 62.7k times · Source

I am trying to delete erroneous emails based on finding the email address in the file via Linux CLI.

I can get the files with

find . | xargs grep -l [email protected]

But I cannot figure out how to delete them from there as the following code doesn't work.

rm -f | xargs find . | xargs grep -l [email protected]

Thank you for your assistance.

Answer

Martin Beckett picture Martin Beckett · Dec 25, 2010

For safety I normally pipe the output from find to something like awk and create a batch file with each line being "rm filename"

That way you can check it before actually running it and manually fix any odd edge cases that are difficult to do with a regex

find . | xargs grep -l [email protected] | awk '{print "rm "$1}' > doit.sh
vi doit.sh // check for murphy and his law
source doit.sh