Find file names that don’t contain a specified string

filesfind

I'd like to execute the opposite of:

find . -name "*2013*"

Find all the files in the current directory that don't contain the string "2013" in their names. How can I do that?

Best Answer

Simply:

find . ! -name '*2013*'

Add a ! -type d to also exclude the files of type directory (like . itself), or -type f to include only regular files, excluding all other types of files (directories, fifos, symlinks, devices, sockets...).

Beware however that * matches a sequence of 0 or more characters. So it could report file names that contain 2013 if that 2013 was preceded or followed by something that cannot be fully decoded as valid characters in the current locale.

That can happen if you're in a locale where the characters can be encoded on more than one byte (like in UTF-8) for file names that are encoded in a different encoding. For instance, in a UTF-8 locale, it would report a Stéphane2013 file if that é had been encoded in the iso8859-15 character set (as the 0xe9 byte).

Best would be to make sure the file names are encoded in the locale's character set, but if you can't guarantee it, a work around is to run find in the C locale:

LC_ALL=C find . ! -name '*2013*'
Related Question