Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list empty folders in linux

Tags:

linux

People also ask

How do I list empty folders?

Search for empty foldersClick on the Search Tab to open the Search Menu. Set the Size filter to Empty, and be sure that the All subfolder feature is checked. After the search ends, it will display all files and folders that do not take up any memory space.

How do I find empty files in Linux?

First, search all the empty files in the given directory and then, delete all those files. This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively.

How do I list empty files in Unix?

Understanding find command options -type f : Search and list files only. -type d : Find and list empty directories only. -empty : Only list empty files or folders on Linux or Unix. -ls : Show current file in ls -dils format on your screen.

What is empty directory Linux?

The command-line utility “rmdir” is used to delete empty files or directories. Rather than checking a directory whether it is empty or not, you can only delete an empty directory. In the following example, we will delete the “testfolder” directory with the help of the “rmdir” command.


Try the following:

find . -type d -empty

With Zsh, you can do the following:

printf '%q\n' ./*/**/(/DN^F)

Replace . with the actual path to the directory you want, or remove it if you want to search the entire file system.


From the section called Glob Qualifiers:

F

‘full’ (i.e. non-empty) directories. Note that the opposite sense (^F) expands to empty directories and all non-directories. Use (/^F) for empty directories.

  • / means show directories
  • D means to also search hidden files (directories in this case)
  • N Enables null pattern. i.e. the case where it finds no directories should not cause the glob to fail
  • F means to show non-empty directories
  • ^ is used to negate the meaning of the qualifier(s) following it

To put them all into an array:

empties=(./*/**/(/DN^F))

Bonus: To remove all the empty directories:

rmdir ./*/**/(/DN^F)

Looks like we finally found a useful case for rmdir!