Paramaeleon Paramaeleon - 4 years ago 307
Bash Question

How to exclude this / current / dot folder from find "type d"

find . -type d


can be used to find all directories below some start point. But it returns the current directory (
.
) too, which may be undesired. How can it be excluded?

Answer Source

POSIX 7 compliant solution:

find . ! -path . -type d

For this particular case (.), golfs better than the mindepth solution (24 vs 26 chars), although this is probably slightly harder to type because of the !.

To exclude other directories, this will golf less well and requires a variable for DRYness:

D="long_name"
find "$D" ! -path "$D" -type d

My decision tree between ! and -mindepth:

  • script? Use ! for portability.
  • interactive session on GNU?
    • exclude .? Throw a coin.
    • exclude long_name? Use -mindepth.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download