Display empty directories only with the ls command

Asked

Viewed 463 times

1

Hello,

I’m a beginner in Ubuntu and would like to know if there is any way to display only directories that contain subdirectories or files on the command line. If I just type ls, appear directories, executable and non-executable files grouped by color. I would like only non-empty directories to appear. Thank you!

2 answers

2


Use:

find . -mindepth 1 -maxdepth 1 -not -empty -type d

Where . is the path to the directory you want to list, in which case he is looking in the current directory.

1

You can use the following commands:

find ~/meuDiretorio -not -empty -type d -ls

The above command will list only folders that have some content.

The command find serves to search folders and files, option -not means negation, in this case it is to reverse the return of the option -empty which is to specify empty files or folders, and the option -type is to specify a type you want, the type set was directories through the option d passed over to -type, and she would be like a kind of filter, and finally the option -ls which is responsible for getting some more information, and in this context it is an option of the command find, you can remove it if you want to see only non-empty directories.

The character ~ indicates your home folder, example /home/usuario, you can change if you want.

Sources:
How can I list only non-empty files using ls?: https://superuser.com/questions/191889/how-can-i-list-only-non-empty-files-using-ls
Manual of the find command: http://man7.org/linux/man-pages/man1/find.1.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.