Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
A synonym of ! is -not, like this: find . -not -iname "002abc*" – Flimm Jan 15 '13 at 14:48

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.

share|improve this answer
find -name 0021bc -prune -o -print
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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