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

       \------- 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'


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.

  • 3
    A synonym of ! is -not, like this: find . -not -iname "002abc*" – Flimm Jan 15 '13 at 14:48
  • In case anyone is wondering, you can combine the flags to produce something like: find -L . -name '*.php' ! -name 'vendor/*.php' -print0 which will find all php files that are NOT inside a vendor folder – Craig Wayne Jun 28 '18 at 11:12

You can find "all" folders and then select out the one you don't want with grep -v (or egrep for that matter)

find . -iname "abc*" | grep -v 002

If you want to select out more than one name, you can use sequential grep -v or one egrep statement.

find . -iname "abc*" | grep -v 002 | grep -v 003


find . -iname "abc*" | egrep -v "002|003"

You'll have to tune it to what you want, and I'm happy to iterate. If you read the man page for find, you'll find lots of flags to sort for files and folders, too.

find -name 0021bc -prune -o -print

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.