35

Possible Duplicate:
Delete files recursively matching a name on the command line (OS X)

I want to remove all files with extension .orig from my tree. The tree is deep. Is there an easy way to do that?

I will probably have to do this many times a day, with different trees. So ease is important.

William Jockusch
  • 4,573
  • 10
  • 34
  • 44

3 Answers3

92

Use the find tool:

find /path -name '*.orig' -delete

Note that the wildcard must be quoted (either as "*.orig" or '*.orig' or \*.orig), as you want it to be only handled by 'find' but not by the shell.

Some operating systems might not have the -delete option, in which case make it invoke rm:

find /path -name "*.orig" -exec rm -i {} \;
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • 2
    I habitually add find's -x flag (`find -x /path ...`) to keep it from crossing mount points onto other volumes. It's usually irrelevant, but I'd rather be safe than sorry. – Gordon Davisson Feb 03 '11 at 20:48
  • 3
    To search in the current folder (including sub folders) `find . -name...` – Alex Ilyaev Jul 09 '16 at 23:25
17

I prefer this method (very similar to @grawity) but with the type of file included:

find /path . -name '*.orig' -type f -delete

1

Can you execute shell commands in bash? This would do the trick:

find /path/to/your/tree | egrep .orig$ | xargs rm
Apache User
  • 49
  • 1
  • 1
  • 3