How do I remove all files that match a pattern?

156,755

Solution 1

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.

Solution 2

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

find ./ -name ".orig" -exec rm -rf {} \;

Solution 3

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

rm .nfs000000000*

Solution 4

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
Share:
156,755

Related videos on Youtube

JD Isaacks
Author by

JD Isaacks

Author of Learn JavaScript Next github/jisaacks twitter/jisaacks jisaacks.com

Updated on September 18, 2022

Comments

  • JD Isaacks
    JD Isaacks almost 2 years

    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
    
  • StuckAt7
    StuckAt7 about 11 years
    Does that work recursively?
  • Oli
    Oli about 11 years
    @FrankBarcenas Yeah - find does everything recursively. If you want to limit how that works, you can play with the -maxdepth or -mindepth arguments.
  • Michael
    Michael over 8 years
    Definitely leave the -delete at the end of the flags. find . -delete -name '*.orig' will ignore the filter and clobber your whole directory.
  • muru
    muru over 8 years
    What's the benefit over using -delete?
  • Peter
    Peter almost 8 years
    @muru I suppose you'd get a prompt for each file if you remove -rf.
  • muru
    muru almost 8 years
    @Peter not necessarily. Even then, so? The answer uses -rf, and find has -ok.
  • kyb
    kyb over 6 years
    How to take patterns from file i.e. .gitignore
  • Andrii Karaivanskyi
    Andrii Karaivanskyi almost 6 years
    @muru it looks like -delete does not remove folders
  • muru
    muru almost 6 years
    @AndriiKaraivanskyi unless the deletion failed, it does.
  • Michael
    Michael almost 6 years
    @kyb It sounds like you want git clean. Maybe with -i or -n for safety?
  • kyb
    kyb almost 6 years
    @Michael, yes. I already solved the problem with git clean -fdx
  • Déjà vu
    Déjà vu almost 6 years
    rm -rf will delete recursively directories, then find might try to navigate through deleted folders (errors displayed). Also, not sure OP wants to delete .orig directories that are likely to include files which are not .orig...
  • Déjà vu
    Déjà vu almost 6 years
    find -type f ... will prevent the warnings if a (non empty) dir happens to have a name like *.orig.
  • kamal
    kamal over 5 years
    Can it be done by grep command and then pipe it to rm?
  • Oli
    Oli over 5 years
    @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.
  • Felipe Centeno
    Felipe Centeno over 2 years
    I sign up to this site just that I could upvote this answer. The accepted answer didn't work, but this did the trick for me. Thank you kind sir