Linux / Unix Find and Delete All Empty Directories & Files

See all UNIX related articles/faq
How do I find out all empty files and directories on a Linux / Apple macOS (OS X) / *BSD / Unix-like operating system and delete them in a single pass?

You need to use the combination of find and rm command. GNU/find has an option to delete files with -delete option. Please note that Unix / Linux filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed by many utilities including rm command/rmdir command or mv command. To avoid problems you need to pass the -print0 option to find command and pass the -0 option to xargs command, which prevents such problems.
Tutorial details
Difficulty level Intermediate
Root privileges No
Requirements Linux or Unix terminal
Category File Management
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes
Advertisement

WARNING! These examples may crash your computer if executed. Some background processes (daemons) may use empty files as lock files or as the default location to lock (chroot) down daemons. Do not delete those files. They are usually located in /var/, /lib/ and other important locations. Do not try to phrase the ls command output for finding and deleting any files[1].

Method # 1: Find and delete everything with find command only

The syntax is as follows to find and delete all empty directories using BSD or GNU find command:

find /path/to/dir -empty -type d -delete

Find and delete all empty files:

find /path/to/dir -empty -type f -delete

Delete empty directories

In this example, delete empty directories from ~/Downloads/

find ~/Downloads/ -empty -type d -delete

Delete empty files

In this example, delete empty files from ~/Downloads/

find ~/Downloads/ -empty -type -delete

Sample session:

Fig.01: Delete empty directories and files.

Fig.01: Delete empty directories and files.

How to count all empty files or directories?

The syntax is as follows:

## count empty dirs only ##
find /path/ -empty -type d | wc -l
 
## count empty files only ##
find /path/ -empty -type f | wc -l

Where,

  • -empty : Only find empty files and make sure it is a regular file or a directory.
  • -type d : Only match directories.
  • -type f : Only match files.
  • -delete : Delete files. Always put -delete option at the end of find command as find command line is evaluated as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.

This is useful when you need to clean up empty directories and files in a single command.

Method # 2: Find and delete everything using xargs and rm/rmdir command

The syntax is as follows to find and delete all empty directories using xargs command:

## secure and fast version ###
find /path/to/dir/ -type d -empty -print0 | xargs -0 -I {} /bin/rmdir "{}"

OR

## secure but may be slow due to -exec  ##
find /path/to/dir -type d -empty -print0 -exec rmdir -v "{}" \;

The syntax is as follows to delete all empty files:

## secure and fast version ###
find /path/to/dir/ -type f -empty -print0 | xargs -0 -I {} /bin/rm "{}"

OR

## secure but may be slow due to -exec  ##
find . -type f -empty -print0 -exec rm -v "{}" \;

Summing up

This page explained how to use the find command along with the xargs command to find and remove all empty files or directories (also known as folders) under Linux, macOS, *BSD and Unix-like systems. For more info, see the following manual pages using the man command or help command. For example:
$ man find
$ man rmdir
$ man xargs
$ man rm

See also

  1. ParsingLs – Why you shouldn’t parse the output of ls command.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

15 comments… add one
  • Editor Jan 29, 2023 @ 17:27

    in the “Delete empty files” section, it should read “f” instead of “-f”.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.