Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks I was unaware you could do that with * before and after. –  Matt 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

 
discard

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.