To delete all files ending in .jpg
(case-insensitive) except for files with sample
in the file name (case-insensitive:
find . ! -iname '*sample*' -iname '*.jpg' -delete
This recurses through all directories in the tree starting at the current directory.
How it works:
.
This specifies that we start with the current directory.
! -iname '*sample*'
This instructs find
to ignore all files with sample
in their name. The i
in -iname
makes this test case-Insensitive.
-iname '*.jpg'
This condition, which is and'd with the previous one, looks for files ending in .jpg
.
-delete
This instructs find
to delete all such files.
Before running the above command, you might want to test it. Run:
find . ! -iname '*sample*' -iname '*.jpg'
This will print out the files of interest. If this list is good, then run the command again with -delete
appended.
2Do you mean
a-sample.jpg
andb-Sample.jpg
? – choroba – 2015-04-15T20:46:30.760Yes I forgot to type the .jpg for a-sample.jpg – tachikoma01 – 2015-04-15T21:04:44.347