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

后端 未结 4 1927
一向
一向 2020-11-30 19:38
find . -type d

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

4条回答
  •  有刺的猬
    2020-11-30 19:56

    POSIX 7 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.

提交回复
热议问题