Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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



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

share|improve this question
up vote 1 down vote accepted

The simple way:

cd /path/to/directory
rm *_foo_*


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.

share|improve this answer
Thanks I was unaware you could do that with * before and after. – Darth_Vader Sep 13 '13 at 18:55
@Matt_2.0 You can use it as many times as you want: rm *foo_1*foo_2*...*foo_n* – Radu Rădeanu Sep 13 '13 at 19:11

If you want to do it in all subdirectories too, the easiest way is to enable the globstar shell option:

shopt -s globstar
rm **/*foo_*

There should be a commented (i.e. starting with a #) line in the default ~/.bashrc (line 29 in mine):

# shopt -s globstar extglob

Just uncomment that line and you'll have access to some improved bash features.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.