HOWTO: Locate empty files and directories

mikhomikho AdministratorHosting ProviderOG
edited May 2021 in LES Talk

Written by @mikho, 5 May 2021
Article was migrated from WordPress to Vanilla in March 2022

image

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:

find ./ -type d -empty

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

find ./ -type f -empty

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

find ./ -type f -empty | wc -l

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

find ./ -type f -not -empty | wc -l 

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

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

– 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 directories.

But it is strongly recommended to not remove them.

Thanked by (1)ehab
Tagged:
Sign In or Register to comment.