3

How do I remove all *.foo in a directory tree?

rm -r *.foo

does not work.

I could try a loop, but that needs recursive expansion:

for x in */*/.../*.foo
    do rm $x
done

Which is not possible either.

user877329
  • 768
  • 1
  • 5
  • 16

4 Answers4

9

You can use find:

find -name '*.foo' -delete
cYrus
  • 22,335
4

Assuming you have a fairly recent version of bash:

shopt -s globstar
rm -- **/*.foo
evilsoup
  • 14,056
1

You could try using find:

find /dir/path -name *.foo -exec rm -f {} \;
arco444
  • 381
0

One of the easiest solutions would be to use the find command with the exec parameter:

find /path/where/to/start/search -type f -name "*.foo" -exec rm {} \;

This will look for all files named *.foo and perform rm {filename} for each one.

If you have lots of files, use of the xargs command may be faster:

find /path/where/to/start/search -type f -name "*.foo" | xargs rm 

This will find all files, and will put the file names behind the rm command up until the max length of a command your distro/shell will support, so when run it'll to this:

rm dir1/file1.foo dir1/file2.foo dir1/file3.foo 

As you can imagine now the rm binary needs to be started much less, making this a faster option.

mtak
  • 17,262