How do I exclude a directory by name using find?

Solution 1:

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.

Solution 2:

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.

Solution 3:

find -name 0021bc -prune -o -print