7

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'

3 Answers 3

9

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.

2
  • 3
    A synonym of ! is -not, like this: find . -not -iname "002abc*"
    – Flimm
    Jan 15, 2013 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 Jun 28, 2018 at 11:12
1

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.

0
1
find -name 0021bc -prune -o -print

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .