In this Linux quick tip we will be discussing how to find empty directories and how to delete them. We will also examine how to find empty files (zero size) and how to act on them as well.

Finding Empty Directories

Using the find command and a few options we can easily find empty directories. Let's take a look at some sample commands, and then we will explain what each option means.

If I wanted to find empty directories in my home folder. I can do so with the following command.

find /home/savona/ -type d -empty

Let's break this down. First we call the find command, then we give it the path of the directory we want to search in (/home/savona/). Then we tell find to only look for directories (-type d) and finally we ask it to only return empty directories (-empty).

Finding Empty Files (zero-byte size)

We can use the same structure as above to find empty or zero-byte size files. This time instead of using d (directory) for the type, we will use f (file).

find /home/savona/ -type f -empty

We can also use the size test to check if it is zero-byte size.

find /home/savona/ -type f -size 0

Deleting Empty Files and Directories

Using the find Command Delete Option

There are several ways to delete the empty files you find with the above command. The easiest is simply using the find command delete option (-delete).

find /home/savona/ -type d -empty -delete

The above will delete all empty directories it finds. To delete empty files, simply replace the type d (directory) with f (files).

Using the find Command Execute Option

Another option is to use the find command execute (-exec) option. This allows you to execute any command and use the items returned from the find command as arguments. Here is an example:

find /home/savona/ -type f -empty -exec rm {} \;

In the above command the curly brackets are used to denote the file name returned by find (the argument). The escaped semi-colon is an argument the command looks for to know when it is completed.

All following arguments to find are taken to be arguments to the command until an argument consisting of ;' is encountered. The string{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command.

-find man page

Using xargs to Delete Arguments in Data Stream

Another option is to pipe the output of the find command into xargs. The xargs commands allow you to execute commands that are built from standard input, which in this case is replaced by the piped data from the find command.

find /home/savona/ -type f -empty | xargs rm -f

In the above example, we are sending each filename found with the find command to the xargs command which is running rm to delete them.

If you want to delete directories with the xargs command, you have to change both the type option in find and the rm command to rmdir. Remember xargs just uses the input to build and run a command. If you run rm against a directory name, it will error out.

$ find ./ -type d -empty | xargs rm -f
rm: cannot remove './testdir': Is a directory

To learn more about standard input, pipes and redirection read "Introduction to Linux IO, Standard Streams, and Redirection".

Conclusion

In this Linux quick tip we showed you how to find and delete empty directories and files on the Linux command line. We also covered using exec or xargs to act on files or directories returned by the find command. In a future article we will show you just how powerful these options can be.

Resources