1. Overview

In this tutorial, we’ll learn how to delete empty files and directories on Linux.

An empty file is a file of zero bytes size. An empty directory is a directory that doesn’t contain any file or directory. It’s fair to say that empty files don’t consume space, but we should clean our file system from time to time as a best practice.

All the commands discussed in this tutorial are Linux-specific and won’t work on Windows.

2. Delete Empty Files in a Directory

We can use the find command to delete all the empty files in a given directory:

$ find . -type f -empty -print -delete

In order to delete empty files, we need to perform two steps. 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. Then, we add the -delete option to delete all those files.

Let’s try to understand this with an example.

Consider a directory that contains both empty and non-empty files and sub-directories. Here, the files prefixed with data-file are non-empty files and the ones prefixed with empty are empty files:

|-- data-file1
|-- data-file2
|-- empty-file1
|-- empty-file2
|-- empty-file3
|-- empty file 4
|-- mydir1
|   |-- data-file3
|   `-- empty-file5
|-- mydir2
|   |-- data-file4
|   `-- empty-file6
`-- mydir3
`-- mydir4
    `-- mydir5

Now, we’ll run the above command inside this directory. It will delete all the empty files recursively. This means that the empty-file5 and empty-file6 inside the directory mydir1 and mydir2, respectively, will also be deleted:

$ find . -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir1/empty-file5
./mydir2/empty-file6
./empty file 4

Let’s observe the output closely. We’ll notice that this command has deleted the files whose name includes space in it (“empty file 4” in our example).

Also, this command has only deleted empty files and not the empty directories like mydir3 and mydir5.

3. Non-Recursive Deletion of Empty Files

Until now, we’ve discussed the scenario where we delete empty files recursively inside a directory. What if we need to delete the empty files present in the current directory and not the ones that are present inside the sub-directories?

The find command has an option -maxdepth that defines the maximum number of directory levels deep the find command should search for a file.

Using -maxdepth 1, the find command will search for a file in the current directory only:

$ find . -maxdepth 1 -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./empty file 4

4. Delete All Empty Directories

We can search for the directories by using -type d with the find command:

$ find . -type d -empty -print -delete

This will delete all the empty directories present inside the current directory recursively.

Let’s run this command inside the same file system as mentioned above:

$ find . -type d -empty -print -delete
./mydir3
./mydir4/mydir5
./mydir4

Again, notice the output carefully. mydir4 directory is also deleted because after deleting the mydir5 directory, mydir4 becomes an empty directory (closely observe the directory structure once again).

5. Non-Recursive Deletion of Empty Directories

Using -maxdepth 1, we can restrict the find command to search for the empty directories in the current directory only:

$ find . -maxdepth 1 -type d -empty -print -delete
./mydir3

6. Delete Empty Files and Directories Together

Now it’s time to combine everything that we’ve learned so far. Let’s delete all the empty files and directories present inside the current directory using a single command.

We’ll use the logical OR operator, -o, with the find command to search for empty files and directories:

$ find . -type d -empty -print -delete -o -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir1/empty-file5
./mydir2/empty-file6
./mydir3
./mydir4/mydir5
./mydir4

The -o option breaks the command from the file path into two parts. The first part,-type d -empty -print -delete, will delete all the empty directories, and the second part, -type f -empty -print -delete, will delete all the empty files.

Again, we can use -maxdepth 1 to delete the empty files and directories non-recursively:

$ find . -maxdepth 1 -type d -empty -print -delete -o -type f -empty -print -delete
./empty-file1
./empty-file2
./empty-file3
./mydir3

7. Conclusion

In this tutorial, we’ve learned about empty files, empty directories, and how to delete them in Linux. We’ve looked into two types of deletion approaches, recursive and non-recursive.

Once a file or directory is deleted, it cannot be restored. So, it’s highly recommended to review all the files/directories before deleting them. In all the commands discussed above, we can remove the -delete option to review all the files that would be deleted.

Also, as a good practice, we can create a cron job to delete empty files and directories. This way, we’ll never accumulate empty files/directories on our machine.

Comments are closed on this article!