Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 463
Jaelebi Avatar asked May 02 '09 21:05

Jaelebi


People also ask

How find non-empty directory in Linux?

Use the 'find' command along with the '-type' flag that specifies the directory type search using the keyword 'd'. The word '-empty' has been used as a flag to search only empty directories within the home directory as stated below. The dot means the current location which is the home directory of a Linux-based system.

How do I display contents of subdirectories in Linux?

Type the ls -l command to list the contents of the directory in a table format with columns including: content permissions.

How show non-empty files in Linux?

Find all (non-)empty files in a directory By default, the find command excludes symbolic files. Use the -L option to include them. The expression -maxdepth 1 specifies that the maximum depth to which the search will drill is one only. By default, the find command will recursively go down the directory.

How do I list subfolders in Linux?

If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively. That shows you the whole directory tree starting at the current directory (or the directories you name on the command line).


3 Answers

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

like image 91
David Z Avatar answered Oct 02 '22 13:10

David Z


find . -type f -print0 | xargs -0 -n 1 dirname | sort -u
like image 45
Paul Tomblin Avatar answered Oct 02 '22 15:10

Paul Tomblin


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.

like image 37
Jonathan Leffler Avatar answered Oct 02 '22 13:10

Jonathan Leffler