60

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:

  • rm -rv *.orig

  • rm -R *\.orig

127

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.

  • Does that work recursively? – Frank Barcenas May 25 '13 at 22:15
  • 2
    @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 9:37
  • 6
    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
  • 1
    @Michael, yes. I already solved the problem with git clean -fdx – kyb Aug 10 '18 at 19:56
  • 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
6

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

find ./ -name ".orig" -exec rm -rf {} \;
  • 2
    What's the benefit over using -delete? – muru Dec 7 '15 at 22:25
  • @muru I suppose you'd get a prompt for each file if you remove -rf. – Peter Jul 12 '16 at 21:06
  • @Peter not necessarily. Even then, so? The answer uses -rf, and find has -ok. – muru Jul 12 '16 at 21:10
  • @muru it looks like -delete does not remove folders – Andrii Karaivanskyi Jul 31 '18 at 22:06
  • @AndriiKaraivanskyi unless the deletion failed, it does. – muru Jul 31 '18 at 22:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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