bash 如何在linux上列出非空子目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/815668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list non-empty subdirectories on linux?
提问by Jaelebi
I have a directory containing multiple subdirectories. I want to list only those subdirectories that contain at least one file. How can I do that?
我有一个包含多个子目录的目录。我只想列出那些至少包含一个文件的子目录。我怎样才能做到这一点?
回答by David Z
find . -mindepth 1 -maxdepth 1 -not -empty -type d
will give you all nonempty directories. If you want to exclude directories that contain only other directories (but no files), one of the other answers might be better...
将为您提供所有非空目录。如果您想排除仅包含其他目录(但不包含文件)的目录,其他答案之一可能会更好......
回答by Paul Tomblin
find . -type f -print0 | xargs -0 -n 1 dirname | sort -u
回答by Jonathan Leffler
How about:
怎么样:
find /nominated/directory -type f |
sed 's%/[^/]*$%% |
sort -u
Find files - drop file name part - sort uniquely.
查找文件 - 删除文件名部分 - 唯一排序。
It won't list subdirectories that contain only other sub-sub-directories.
它不会列出仅包含其他子子目录的子目录。