Ubuntu – How to exclude a directory by name using find

bashfind

I have a lot of directories on my system, with a structure looking like this:

\-----data
       \------- 001abc
       \------- 002abc
       \------- 003abc
       \------- 004abc
       \------- 005abc
       \ ....

When I want find a certain directory, I just type find . -iname "002abc*" but how can I find a directory excluding certain name matches?

Something like this in MySQL select * from folder where filename != '0021bc'

Best Answer

Use ! in a find command to negate (invert) the option following after. In your case:

find . ! -iname "002abc*"

and optionally, only matching folders:

find . ! -iname "002abc*" -type d

will list all folders except the ones named matching the pattern 002abc*.

The ! can be problematic in shell scripting sometimes, so as Flimm pointed out, the -not parameter is a very useful synonym to it.