Ubuntu – Remove files in a directory based on partial name

bashcommand linerm

I wanted to know what is the best way to remove any files no matter what their extension is based on a partial name.

Example:

13-05-12_foo_something.jpg
13-06-01_something.png
13-05-05_foo_site.html

rm everything in a directory and within the sub folders with _foo_ in the name.

Best Answer

The simple way:

cd /path/to/directory
rm *_foo_*

or

rm /path/to/directory/*_foo_*

The asterisk (*) matches all characters.

To get explanations about what is being done, use rm with -v option. To be prompted before every removal, use rm with -i option:

rm -vi *_foo_*

See man rm for more info.