Delete empty files and directories in Linux

Empty files and directories in Linux systems don't consume significant disk space, but removing them periodically helps maintain system cleanliness and organization. This tutorial covers various methods to delete empty files and directories using the powerful find command in Linux systems.

Delete Empty Files in a Directory

The find command with specific options can locate and remove all empty files in a directory structure. The basic syntax combines type filtering, empty file detection, and deletion in one command.

find . -type f -empty -print -delete

Let's examine this command with a practical example. Consider the following directory structure containing both empty and non-empty files:

project/
|-- 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/

Running the find command produces the following output, showing each deleted empty file:

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

The command successfully removes all empty files recursively, including those with spaces in their names, while preserving non-empty files and directories.

Non-Recursive Deletion of Empty Files

To delete empty files only from the current directory (not subdirectories), use the -maxdepth option to limit search depth:

find . -maxdepth 1 -type f -empty -print -delete

This command restricts the search to the current directory level:

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

Delete Empty Directories

Empty directories are removed using a similar approach, but with the -type d option to target directories instead of files:

find . -type d -empty -print -delete

Executing this command removes all empty directories recursively:

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

Note that after removing nested empty directories, parent directories may become empty and are also deleted in subsequent passes.

Non-Recursive Directory Deletion

To delete empty directories only at the current level:

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

Delete Empty Files and Directories Together

Combine both operations using the logical OR operator (-o) to delete empty files and directories in a single command:

find . -type d -empty -print -delete -o -type f -empty -print -delete

This comprehensive command handles both file types simultaneously:

$ 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

For non-recursive cleanup of the current directory only:

find . -maxdepth 1 -type d -empty -print -delete -o -type f -empty -print -delete

Command Options Explained

Option Description
-type f Search for files only
-type d Search for directories only
-empty Match empty files/directories
-print Display found items before deletion
-delete Remove matched items
-maxdepth 1 Limit search to current directory level
-o Logical OR operator

Best Practices

Always review the output before actual deletion by first running the command without -delete:

find . -type f -empty -print

Consider automating cleanup with a cron job for regular maintenance:

# Add to crontab for weekly cleanup
0 2 * * 0 find /home/user/projects -type f -empty -delete

Conclusion

The find command provides powerful and flexible options for removing empty files and directories in Linux systems. Whether performing recursive cleanup across directory trees or targeting specific levels, these commands help maintain organized file systems and prevent accumulation of unnecessary empty elements.

Updated on: 2026-03-17T09:01:38+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements