Use GNU find to show only the leaf directories

前端 未结 7 645
爱一瞬间的悲伤
爱一瞬间的悲伤 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:43

    @Sylvian solution didn't work for me on mac os x for some obscure reason. So I've came up with a bit more direct solution. Hope this will help someone:

    find . -type d  -print0 | xargs -0 -IXXX sh -c '(ls -p XXX | grep / >/dev/null) || echo XXX' ;
    

    Explanation:

    • ls -p ends directories with '/'
    • so (ls -p XXX | grep / >/dev/null) returns 0 if there is no directories
    • -print0 && -0 is to make xargs handle spaces in directory names

提交回复
热议问题