1. Overview

In this tutorial, we’ll take a look at ways to delete files with size zero in Linux.

2. Find 0-Byte Files

Let’s first search for all 0-byte files in our home directory, using the find command:

$ find /home/baeldung -size 0

find searches in all files, directories, and sub-directories starting from the path we specified. The -size option limits the found files to ones that use less than, more than, or exactly n units of space, rounding up. If we don’t specify any unit, the default unit for size would be the number of blocks used. So, if we want the files with exactly 0-byte size, we should use the ‘c’ suffix, which is for bytes:

$ find /home/baeldung -size 0c

3. Using find

If we have a long list of 0-byte files, we can search for them with the find command and use the -delete option to delete all the files:

$ find /home/baeldung -size 0c -delete

It’s always better to check the find results before using the -delete option:

$ find /home/baeldung -size 0c

Note that find will go to all directories and subdirectories (in our case, the test directory) and remove all the files starting from /home/baeldung. We can limit this functionality by setting the -maxdepth option.

3.1. Using -maxdepth

The -maxdepth option tells the find command to search for files with a limit in the depth starting from the /home/baeldung directory:

$ find /home/baeldung -maxdepth 1 -size 0c -delete

Let’s check the files that were not deleted:

$ find /home/baeldung -size 0c 

Because we set -maxdepth, find doesn’t go to the test directory this time. So, the logging.log file still exists.

3.2. Using -name

If we want to search only in the current directory and filter the files with specific names, we can use the -name option:

$ find /home/baeldung -maxdepth 1 -size 0c -name "log*" -delete

This command only deletes files whose names start with ‘log’.

3.3. Using -exec

In older versions of find, the -delete option may not be available. In this case, we can use the -exec option. Here, -exec allows us to execute the rm command and delete the files found:

$ find /home/baeldung -maxdepth 1 -size 0c -exec rm '{}' \;

The string ‘{}‘ after -exec is replaced by the found file names. Note that we enclosed the braces in single quotes to protect them from interpretation as shell script punctuation.

Also, notice that at the end of our command and all its required arguments, we should use a ‘;’ to specify the end of the command. We also protect the semicolon from being expanded by the shell by using ‘\’. Using single quotes around the semicolon (‘;’) provides the same protection.

3.4. Order of Commands

The order of commands to filter the files and delete them is important. Therefore, if we use the -delete or -exec option before limiting the file size, find will ignore all the other options. So, all the files in the directory will be deleted:

$ find /home/baeldung -delete -maxdepth 1 -size 0c 

4. Conclusion

In this article, we discussed the approaches to delete files with size zero.

We can use the find command to filter file sizes with the -size option, and then, delete all files with the -delete option. If the -delete option is not available, we can use -exec and execute the rm command on all 0-byte files.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.