linuxfilefindcommand-line-interfacerm

Delete files with string found in file - Linux cli


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@example.com

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@example.com


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@example.com | awk '{print "rm "$1}' > doit.sh
    vi doit.sh // check for murphy and his law
    source doit.sh