Friday, October 7, 2022

[SOLVED] Delete a list of files with find and grep

Issue

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?


Solution

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.



Answered By - William Pursell
Answer Checked By - Pedro (WPSolving Volunteer)