How to locate and delete empty files and directories

How to locate and delete empty files and directories
Chance is that there will be empty files (zero byte files) and empty directories (that doesn’t contain any files) in your system. In this article, I will show you how to locate these empty directories and files and delete them.

The command you can use is the "find" command. To find empty directories in the current directory, you use the option -empty:
 find . -type d -empty  

To remove all these empty directories in the current directory, the command you can use is:
 find . -type d -empty -exec rmdir {} \;  

The option -empty is also used to find empty files inside the current directory, but the syntax is a little bit different:
 find . -type f -empty  

To count the amount of empty files inside the current directory, you need to pipe the find command to wc -l:
 find . -type f -empty | wc -l  

Similarly, to count how many ow many files are located under the current directory (and sub-directories), you can use the following command:
 find . -type f -not -empty | wc -l  

Note:
- In system directories such as /etc/, there are many empty files and directory. But it is strongly recommended to not remove them.
- Typically empty files are created by some programs as place holders, or as lock files, or as socket files for communication.
- In all the commands above, the dot (.) means the current directory or folder, if you want to perform actions in other directories, just replace the dot (.) with the path to the new directory.
linux tips and tricks