问题描述
我试图使用GNU找只查找不包含其他目录的目录,但可能会或可能不会包含常规文件。
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:
find dir -type d \( -not -exec ls -dA ';' \)
但是这只是让我一个长长的清单。
but this just gets me a long list of "."
谢谢!
推荐答案
您可以使用-links如果你的文件系统是POSIX兼容的(即一个目录下有一个链接,它在每个子目录,来自其母公司和一个链接一个链接自我,从而2链接的数量,如果它没有子目录)。
You can use -links if your filesystem is POSIX compliant (ie, a directory has a link for each subdirectory in it, a link from its parent and a link to self, thus a count of 2 link if it has no subdirectories).
下面的命令应该做你想要什么:
The following command should do what you want:
find dir -type d -links 2
但是,它似乎并不在Mac OS X上运行(如@Piotr mentionned)。这里是另一个版本的比较慢,但在Mac OS X正是基于他的版本确实工作,以校正目录名来处理空白:
However, it does not seems to work on Mac OS X (as @Piotr mentionned). Here is another version that is slower, but does work on Mac OS X. It is based on his version, with correction to handle whitespace in directory names:
find . -type d -exec sh -c '(ls -p "{}"|grep />/dev/null)||echo "{}"' \;
这篇关于使用GNU发现只显示叶目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!