Jaelebi
Jaelebi

Reputation: 6129

How to list non-empty subdirectories on linux?

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

Answers (4)

Bjarke Fjeldsted
Bjarke Fjeldsted

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

Paul Tomblin
Paul Tomblin

Reputation: 182880

find . -type f -print0 | xargs -0 -n 1 dirname | sort -u

Upvotes: 11

David Z
David Z

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

Jonathan Leffler
Jonathan Leffler

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

Related Questions