Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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'

share|improve this question
add comment

3 Answers 3

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
3  
A synonym of ! is -not, like this: find . -not -iname "002abc*" –  Flimm Jan 15 '13 at 14:48
add comment
find -name 0021bc -prune -o -print
share|improve this answer
add comment

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

or

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
add comment

Your Answer

 
discard

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.