- How do I remove all files that match a pattern?
- 4 Answers 4
- How can I delete all files with a particular extension in a particular folder?
- 4 Answers 4
- Removing files with a certain extension except one file from terminal
- 4 Answers 4
- How to delete all the files with a certain extension from a folder and all of its subfolders? [duplicate]
- 4 Answers 4
How do I remove all files that match a pattern?
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: cannot remove `**/*.orig': No such file or directory
4 Answers 4
Use the find command (with care!)
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.
@FrankBarcenas Yeah — find does everything recursively. If you want to limit how that works, you can play with the -maxdepth or -mindepth arguments.
Definitely leave the -delete at the end of the flags. find . -delete -name ‘*.orig’ will ignore the filter and clobber your whole directory.
@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.
«find» has some very advanced techniques to search through all or current directories and rm files.
find ./ -name ".orig" -exec rm -rf <> \;
I have removed all files that starts with .nfs000000000 like this
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.
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
How can I delete all files with a particular extension in a particular folder?
If I set the current/working directory (navigating to it using cd ) to some particular directory and then type:
What will this command do? Is it true that the above command will only delete files with the extension .xvg only in the working directory? I was nervous about trying this before asking, because I want to be absolutely sure that the above command will only delete .xvg files LOCATED IN THE WORKING DIRECTORY.
4 Answers 4
Yes, rm *.xvg will only delete the files with the specified extension in your current directory.
A good way to make sure you are indeed in the directory you want delete your files is to use the pwd command which will display your current directory and then do an ls to verify you find the files you are expecting.
If you are bit apprehensive about issuing the rm command, there are 2 things you can do:
- type ls *.xvg to see a list of what files would be affected by this command.
- Unless you have a lot of files, you could always also use the -i command line switch for rm (also exists for cp and mv ). Using rm -i *.xvg would prompt you for each individual file if it was ok to delete it, so you could be sure nothing you didn’t expect was getting deleted. (This will be tedious if you have a lot of files though 🙂
You don’t need to navigate to the dir, just use
In the case you have a typo or similar mistake in the path, where /som/dir does not exist:
will accidentally delete all .xvg-files in the current dir. The first command will not, and you don’t need to cd back again.
An alternative way would be to use find:
find /some/dir/ -maxdepth 1 -type f -name "*.xvg" -delete
find /some/dir/ -maxdepth 1 -type f -name «*.xvg» -delete worked for me! use -maxdepth «n» to delete matching files in sub directory if given path
Yes, rm *.xvg will only delete files ending with .xvg in your current directory. Here’s why.
When you type a command like this, work is split up between the shell you are using (let’s assume bash) and the command binary.
You can locate the binary by typing which rm . This little program takes care of unlinking files. Programs like this can be started from the command line and can read a list of arguments prog arg1 arg2 arg3 when they start up. In the case of rm , they are interpreted as a list of fully qualified filenames to be deleted. So if you are in a directory containing the file foo.bar , typing delete ‘foo.*’ will result in rm: foo.*: No such file or directory . Note the single quotes around the file pattern, they tell the shell to pass the argument to the shell as it is.
However if you type rm *.bar in the same directory, it will delete the file. What’s happening here is that your shell, which is the program you are using to type in commands, is performing some transformations before passing the arguments on to the command. One of these is called ‘file name expansion’, otherwise know as ‘globbing’. You can see a list of bash file name expansions here. One of the most common expansions is * , which is expanded to filenames in the current directory.
A simple way to look at globs at work is to use echo , which prints back all arguments passed to it through the shell. So typing echo * in the same directory will output foo.bar . So when you type rm *.bar , what’s actually happening is that the shell expands the argument list to foo.bar , then passes that to the rm command.
There are some ways of controlling globbing. In recent versions of bash, for example, you can turn on an option called globstar which will do recursive expansion. Typing echo **/*.bar will show a list of all files ending in .bar in all subfolders. So typing rm **/*.bar in globstar enabled bash will indeed recursively delete all matching files in subfolders.
Removing files with a certain extension except one file from terminal
I need to remove all files with .gif extension except one file with name say «filename.gif». What is the optimal way to go about doing this in terminal? The command rm *.gif removes all gif files including the file filename.gif .
4 Answers 4
Here’s the simple solution you should probably use:
mv filename.gif filename.gif.keep rm *.gif mv filename.gif.keep filename.gif
There’s nothing special about the .keep extension, this just makes it so that the filename temporarily doesn’t end in .gif .
If you must not rename the file (and there are scripting situations where this is important):
for X in *.gif; do if [ "$X" != "filename.gif" ]; then rm "$X" fi done
Or you can write it shorter like this:
for X in *.gif; do [ "$X" != "filename.gif" ] && rm "$X"; done
You may prefer to use find instead; it’s very powerful, you might consider it more readable, and it better handles weird filenames with characters like * in them.
find . -maxdepth 1 -not -name 'filename.gif' -name '*.gif' -delete
I’ve used the -not operator for readability, but if POSIX compliance is important—if you’re not using GNU find, or if this is for a script you intend to redistribute to others or run on a variety of systems—you should use the ! operator instead:
find . -maxdepth 1 ! -name 'filename.gif' -name '*.gif' -delete
One handy thing about find is that you can easily modify the command for case-insensitivity, so that it finds and deletes files with extensions like .GIF too:
find . -maxdepth 1 -not -name 'filename.gif' -iname '*.gif' -delete
Please note that I’ve used -iname in place of -name for the search pattern *.gif but I have not used it for filename.gif . Presumably you know exactly what your file is called, and -iname will match alternate capitalization not just in the extension, but anywhere in the filename.
All these solutions only delete files residing immediately in the current directory. They don’t delete files not contained in the current directory, and they don’t delete files that reside in subdirectories of the current directory.
If you want to delete files everywhere contained within the current directory (that is, including in subdirectories, and in subdirectories of those subdirectories, and so forth—files contained within the current directory or any of its descendants), use find without maxdepth -1 :
find . -not -name 'filename.gif' -name '*.gif' -delete
You can also set other values with -maxdepth . For example, to delete files in the current directory and its children and grandchildren but not any deeper:
find . -maxdepth 3 -not -name 'filename.gif' -name '*.gif' -delete
Just make sure you never put -delete first, before the other expressions! You’ll see I’ve always put -delete at the end. If you put it at the beginning, it would be evaluated first, and all the files under . (including files not ending in .gif and files in deep sub-sub-sub. directories of . ) would be deleted!
For more information, see the manual pages for bash and sh and for the commands used in these examples: mv , rm , [ , and (especially) find .
How to delete all the files with a certain extension from a folder and all of its subfolders? [duplicate]
I want to remove all the .jpg files from my Music folder in order to save room. My Music folder contains subfolders, and I would like to know if there is a command to remove all the .jpg files from all these folders regardless of their level. Thanks for your help!
4 Answers 4
A quick and clean solution for the command line would be
cd find . -type f -iname \*.jpg -delete
- . tells to start searching in the current folder.
- -type f tells find only to look for files.
- -iname makes the search case insensitive.
- -delete tells find to delete/remove all files found.
CAUTION! I recommend running the command without -delete first to get a list of the files that will be removed when -delete is included in the command. This way a small typo won’t delete anything you didn’t intend to.
For more information on how to use find and what it can do see man find
Note that find will not produce any output when -delete is used.
Regarding the comment on multiple extensions
find . -type f \( -name \*jpg -o -name \*png \) -delete
- ( .. ) Group expression. Needs to be escaped from the shell, hence \( .. \) in the example
- -o logical or
So the above example will match any file which has either jpg or png at the end of it’s name. More extensions can be added in the same way. Just make sure to remember -o between every -name statement, if -o is not specified find will default to -a (and) which won’t work since a file can’t end in both jpg and png at the same time.