使用 GNU find 仅显示叶目录 [英] Use GNU find to show only the leaf directories

查看:10
本文介绍了使用 GNU find 仅显示叶目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 GNU find 仅查找不包含其他目录但可能包含也可能不包含常规文件的目录.

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

谢谢!

推荐答案

如果您的文件系统是 POSIX 兼容的,您可以使用 -links到自己,因此如果它没有子目录,则计数为 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 提到的那样).这是另一个较慢的版本,但可以在 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 find 仅显示叶目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆