I have a directory with files of many extensions in it. I would like to recursively delete *.srt
files (and *.srt
files only) which don't end with -en.srt
(where srt
is an extension). I've come up with the following solution and it seems to work fine, however, I'd like to know whether it is 100% correct.
find . -name "*.srt" ! -name "*-en.srt" -type f -exec rm -rf {} \;
! -- First read the answer completely then use it if you like it -- !
Your command is correct, however there is no need to use
-rf
asrm
parameters. because you are removing files and not directories.Another clear way to write it is (it's almost same as your command):
or as @steeldriver suggested you can use:
It will ask for your permission to remove each founded file.
You can also use
-delete
instead ofrm {} \;
however be aware of its dangers:It is always a good idea to test what is going to happen before doing the actual job, so I suggest running:
If it return nothing then the actual command will work without any problem and you are good to go... or even:
to check what's going to be removed.
And do not forget to quote
'{}'
:Let’s do it solely with
bash
globbing: With theextglob
andglobstar
options enabled,deletes every file ending in
.srt
excluding anything ending in-en.srt
from the current as well as any subdirectory.If you‘re not sure about an expansion like this, test by prepending
echo
(see example below).Example
Explanations
**/
– with theglobstar
option enabled this matches any number of directories and subdirectories!(*-en)
– with theextglob
option enabled this matches anything except the given pattern, so anything not ending in-en