Using find and rm in Conjunction

I downloaded a bunch of music or pictures or somesuch beyond memory, and in each folder the generous sharer included two or three dozen rich text files (.rtf) with some advertising for something about which I couldn’t have cared less. ┬áIn the end this accounted for a few hundred unwanted files all ending in .rtf.

In order to find them I used this command:

find /path/to/containing/folder/ -type f -name \*.[Rr][Tt][Ff]

That’s all well and good, but I want to delete them (using rm presumably).

I tried a few guesses…

rm `find /path/to/containing/folder/ -type f -name \*.[Rr][Tt][Ff]`
rm: missing operand

rm "`find /path/to/containing/folder/ -type f -name \*.[Rr][Tt][Ff]`"
rm: cannot remove `': No such file or directory

rm '`find /path/to/containing/folder/ -type f -name \*.[Rr][Tt][Ff]`'

This last one was close but it divided words on spaces and so failed. At this point I thought about changing the internal field separator ($IFS) to the line break (\n) instead of the default space ( ) but instead decided to look for something simpler.

I found it here. In the end my code looked like this:

find /path/to/containing/folder/ -type f -name \*.[Rr][Tt][Ff] -exec rm -f {} \;

You can read quite a bit about the -exec argument in the find manpage (in your terminal type man find).

Warning: This will delete all rich text files located under the containing folder. I was safe to do this because there is nothing on my server stored in the rich text format. If you have files of the same type you are seeking to remove, you will want to take appropriate precautions.

Hope this helps you out.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *