How do I remove all files that match a pattern?
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
Related videos on Youtube
JD Isaacks
Author of Learn JavaScript Next github/jisaacks twitter/jisaacks jisaacks.com
Updated on September 18, 2022Comments
-
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
-
Admin over 5 yearsTake a look at superuser.com/a/699287/92334
-
-
StuckAt7 about 11 yearsDoes that work recursively?
-
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 over 8 yearsDefinitely leave the
-delete
at the end of the flags.find . -delete -name '*.orig'
will ignore the filter and clobber your whole directory. -
muru over 8 yearsWhat's the benefit over using
-delete
? -
Peter almost 8 years@muru I suppose you'd get a prompt for each file if you remove -rf.
-
muru almost 8 years@Peter not necessarily. Even then, so? The answer uses
-rf
, andfind
has-ok
. -
kyb over 6 yearsHow to take patterns from file i.e.
.gitignore
-
Andrii Karaivanskyi almost 6 years@muru it looks like -delete does not remove folders
-
muru almost 6 years@AndriiKaraivanskyi unless the deletion failed, it does.
-
Michael almost 6 years@kyb It sounds like you want
git clean
. Maybe with-i
or-n
for safety? -
kyb almost 6 years@Michael, yes. I already solved the problem with
git clean -fdx
-
Déjà vu almost 6 years
rm -rf
will delete recursively directories, thenfind
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 almost 6 years
find -type f ...
will prevent the warnings if a (non empty) dir happens to have a name like*.orig
. -
kamal over 5 yearsCan it be done by
grep
command and then pipe it torm
? -
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 over 2 yearsI 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