Reputation: 6129
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?
Upvotes: 46
Views: 36736
Reputation: 11
du * -hs | grep 4,0K -v
Works for me du * -hs lists how muchs is in a dir grep 4,0k lists the empty dirs, the -v inverts the grep search
Upvotes: 0
Reputation: 131760
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...
Upvotes: 87
Reputation: 755044
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.
Upvotes: 5