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.
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.
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.
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 directoriesD
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 failF
means to show non-empty directories^
is used to negate the meaning of the qualifier(s) following itTo 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
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With