Find (and delete) all files that do NOT contain certain patterns (PLURAL) in their name

67 Views Asked by At

I'm looking for an easy way to find and delete all files in a directory and all its subdirectories that do NOT contain certain strings in their name.

I tried starting with the find command first:

find . -type f \! \( -name "TMP_TGL_2" -o -name "PRES_SFC_0" \)

But it doesn't seem to exclude the second string pattern. How can I accomplish this?

1

There are 1 best solutions below

0
datawookie On

Try this:

find . -type f ! \( -name "*TMP_TGL_2*" -o -name "*PRES_SFC_0*" \)

The wildcards round the patterns will insure that they match files which contain those strings (otherwise they would only match files with names that are identical to the strings).