Command: Locate empty files and directories

When installing software on your VPS you will end up with both empty files and empty directories, often these are used as placeholders/lock files/socket files for communication.

This short guide will give you some examples on how to find those empty files/directories.

The command we are going to use is the “find” command. To find empty directories/files in the current directory, you use the parameter “-empty“.
You also have to use the parameter “-type” to define if you are looking for directories (d) or files (f).

Examples

Here is the command to find empty directories in the current directory:

[precode]find ./ -type d -empty[/precode]

And here is the command to find empty files in the current directory:

[precode]find ./ -type f -empty[/precode]

If you need to know how many empty files you have in the current directory, pipe the find command to “wc -l“:

[precode]find ./ -type f -empty | wc -l[/precode]

Similarly, to recursivly count how many how many files are located under the current directory and sub-directories,  you can use the following command:

[precode]find ./ -type f -not -empty | wc -l [/precode]

 

To remove all empty directories in the current directory, the command you can use is:

[precode]find ./ -type d -empty -exec rmdir {} \;[/precode]

[alert style=”green”]

– In all the commands above, the  (./) means the current directory or folder, if you want to perform actions in other directories, just replace the  (./) with the path to the new directory.
– In system directories such as /etc/, there are many empty files and directory.
But it is strongly recommended to not remove them.
[/alert]

Leave a Reply

Your email address will not be published. Required fields are marked *