Delete files with string found in file – Linux cli

Issue

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]

Solution

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

Answered By – Martin Beckett

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published