126

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
1

4 Answers 4

230

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.

9
  • Does that work recursively? May 25, 2013 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, 2013 at 9:37
  • 19
    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, 2015 at 17:02
  • 1
    @Michael, yes. I already solved the problem with git clean -fdx
    – kyb
    Aug 10, 2018 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, 2018 at 14:29
20

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

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

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

rm .nfs000000000*
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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