145

When I revert in Mercurial, it leaves several .orig files. I would like to be able to run a command to remove all of them.

I have found some sources that say to run:

rm **/*.orig

But that gives me the message:

rm: cannot remove `**/*.orig': No such file or directory

I have also tried these commands:

rm -rv *.orig
rm -R *\.orig
BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
JD Isaacks
  • 5,057
  • 10
  • 28
  • 27

4 Answers4

257

Use the find command (with care!)

find . -name '*.orig' #-delete

I've commented out the delete command but once you're happy with what it's matching, just remove the # from the line and it should delete all those files.

Oli
  • 289,791
  • 117
  • 680
  • 835
  • 1
    Does that work recursively? – Frank Barcenas May 25 '13 at 22:15
  • 4
    @FrankBarcenas Yeah - find does everything recursively. If you want to limit how that works, you can play with the `-maxdepth` or `-mindepth` arguments. – Oli May 26 '13 at 09:37
  • 21
    Definitely leave the `-delete` at the _end_ of the flags. `find . -delete -name '*.orig'` will ignore the filter and clobber your whole directory. – Michael Nov 18 '15 at 17:02
  • How to take patterns from file i.e. `.gitignore` – kyb Jan 31 '18 at 18:28
  • @kyb It sounds like you want `git clean`. Maybe with `-i` or `-n` for safety? – Michael Aug 09 '18 at 20:30
  • 2
    @Michael, yes. I already solved the problem with `git clean -fdx` – kyb Aug 10 '18 at 19:56
  • `find -type f ...` will prevent the warnings if a (non empty) dir happens to have a name like `*.orig`. – Déjà vu Sep 30 '18 at 08:14
  • Can it be done by `grep` command and then pipe it to `rm`? – kamal Dec 17 '18 at 13:58
  • 1
    @kamal I'd probably still use find but with its `-regex` or `-iregex` predicates. Parsing filenames (when you're piping them around) can be hard to do safely sometimes. – Oli Dec 17 '18 at 14:29
25

"find" has some very advanced techniques to search through all or current directories and rm files.

find ./ -name ".orig" -exec rm -rf {} \;
user2038042
  • 351
  • 3
  • 4
13

I have removed all files that starts with .nfs000000000 like this

rm .nfs000000000*
Taras Vaskiv
  • 441
  • 4
  • 5
5

The below is what I would normally do

find ./ -name "*.orig" | xargs rm -r

It's a good idea to check what files you'll be deleting first by checking the xargs. The below will print out the files you've found.

find ./ -name "*.orig" | xargs

If you notice a file that's been found that you don't want to delete either tweak your initial find or add a grep -v step, which will omit a match, ie

find ./ -name "*.orig" | grep -v "somefiletokeep.orig" | xargs rm -r
RuNpiXelruN
  • 151
  • 1
  • 2