0

How to ignore directories during find executing? For example there is the following:

.
├── fold
│   └── something
└── something

fold/something - directory ./something - file

I do find . -name "something" and I wont to ignore fold/something because this is the directory

1 Answer 1

2

You test for a directory with -type d. You invert the sense of a test with !.

Therefore:

find . -name something ! -type d

This would find everything called something in or below the current directory, but it would not report any directory called something.

If you want to ignore ./fold/something specifically, use a negated -path test:

find . -name something ! -path ./fold/something

Note that the pathname that you use in the -path test should match the full path from the top-level search path (.).

If you want to stop find from reporting any directory called something, and also stop it from even entering those directories, use -prune:

find . \
    -name something -type d -prune -o \
    -name something -print

or, equivalently,

find . -name something \( -type d -prune -o -print \)

This would first prune any found directory called something from the search tree. If the current path being examined is not a directory called something, but something else called something, then it is printed. This would not find anything called something under ./fold/something, regardless of its filetype (since ./fold/something would have been removed from the search tree).

0

You must log in to answer this question.

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