Using find to delete all files with a given name except one with a given extension

I want to delete all the files in the directory with the given name, except for one with the given extension. That is, we have a directory with:

foo.txt foo.exe foo.jpg foo.png foo.something foo.somethingelse bar.jpg bar.exe 

I want to get rid of foo.txt foo.jpg foo.png foo.something foo.somethingelse

BUT the main thing, I do not want to get rid of foo.exe

Is there a simple liner for this?

thanks

+4
source share
1 answer

You can use ! inside the find to exclude everything, something like:

 find . -maxdepth 1 -type f -name "foo.*" ! -name foo.exe -exec rm '{}' \; ----------- ------- ------------- --------------- ---------------- in this dir files named foo.* but not foo.exe ...destroy them. 

This should delete the files matching foo.* In the current directory, but leave foo.exe its own.

+7
source

All Articles