Delete a list of files with find and grep

Magnus picture Magnus · Dec 31, 2013 · Viewed 79.1k times · Source

I want to delete all files which have names containing a specific word, e.g. "car". So far, I came up with this:

find|grep car

How do I pass the output to rm?

Answer

William Pursell picture William Pursell · Dec 31, 2013
find . -name '*car*' -exec rm -f {} \;

or pass the output of your pipeline to xargs:

find | grep car | xargs rm -f

Note that these are very blunt tools, and you are likely to remove files that you did not intend to remove. Also, no effort is made here to deal with files that contain characters such as whitespace (including newlines) or leading dashes. Be warned.