How to list non-empty subdirectories on linux?
Question
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?
Solution
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...
OTHER TIPS
find . -type f -print0 | xargs -0 -n 1 dirname | sort -u
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow