Use GNU find to show only the leaf directories

前端 未结 7 644
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 18:57

I\'m trying to use GNU find to find only the directories that contain no other directories, but may or may not contain regular files.

My best guess so far has been:

相关标签:
7条回答
  • 2020-12-04 19:57

    This awk/sort pipe works a bit better than the one originally proposed in this answer, but is heavily based on it :) It will work more reliably regardless of whether the path contains regex special characters or not:

    find . -type d | sort -r | awk 'index(a,$0)!=1{a=$0;print}' | sort
    

    Remember that awk strings are 1-indexed instead of 0-indexed, which might be strange if you're used to working with C-based languages.

    If the index of the current line in the previous line is 1 (i.e. it starts with it) then we skip it, which works just like the match of "^"$0.

    0 讨论(0)
提交回复
热议问题