Linux / Unix: Find and Delete All Empty Directories & Files

How do I find out all empty files and directories on a Linux / Apple OS X / BSD / Unix-like operating systems and delete them in a single pass?

You need to use the combination of find and rm command. [donotprint]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements find command
Est. reading time 5m
[/donotprint]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. 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.
WARNING! These examples may crash your computer if executed. Some background process (daemons) may use empty files as a lock files or as default location to lock (chroot) down daemons. Do not delete those files. Usually, located in /var/, /lib/ and other important locations.

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 -f -delete

Sample session:

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


  • -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 "{}"


## 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 "{}"


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

See man pages – xargs(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 12 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Scott Carlson Oct 25, 2012 @ 12:26

    my pruneEmptyDirs script looks like :

    perl -MFile::Find -e”finddepth(sub{rmdir},’.’)”

  • Balakrishnan B Oct 25, 2012 @ 17:47

    zsh only
    To delete all empty directories

    1. rmdir –ignore-fail-on-non-empty **/*(/)
    2. rmdir **/*(/^F)
  • AngelVG Oct 11, 2013 @ 15:42

    Take care:

    If you have the directory /home/ftp/pub
    empty, the command

    find /home/ftp/pub -type d -empty -exec rm {} ;

    deleted the pub directory (i tested this in FreeBSD 9.1)

    The solution:

    add the option
    -depth 1


    find /home/ftp/pub -depth 1 -type d -empty -exec rm {} ;

    For example, for maintain my ftp directory clear (up to 7 days), y have this lines in script /usr/home/backups/scripts/ with this

    #! /bin/sh

    # Files
    find /usr/home/ftp/pub -atime +1w -exec rm {} ;
    # Directories
    find /usr/home/ftp/pub -depth 1 -type d -empty -atime +1w -exec rm {} ;

    Executions permissions
    # chmod +x /usr/home/backups/scripts/

    and in my cron add

    00 01 * * * root /usr/home/backups/scripts/

    This is all, bye.

    • Allen Cohen Jan 15, 2015 @ 22:28

      No such syntax: “-atime +1w”
      Must use: “-atime +7”

      Also there’s “-amin …” for minutes ago

      I prefer to use the modification times rather than access times.

  • AngelVG Nov 1, 2013 @ 16:29

    Hi, some correction…

    For example, for maintain my ftp directory clear (up to 7 days)
    the correct command is

    # Files
    find /usr/home/invitadoftp/pub -type f -mtime +1w -exec rm {} ;
    # Direcories
    find /usr/home/invitadoftp/pub -depth 1 -type d -empty -mtime +1w -exec rm {} ;

    Please update may older command and delete this

  • uniqueinx Jul 17, 2014 @ 12:14

    nice one ;)

  • Anderson Figueiredo Aug 22, 2014 @ 11:24

    Very good… thanks!

  • Ryan T Jan 2, 2015 @ 19:30

    For Delete empty directories you need ‘-type d’ not ‘-type -d’. Same with the file removal example.

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

    • 🐧 nixCraft Jan 3, 2015 @ 17:37

      Thanks for the heads up!

      • J A Aug 8, 2020 @ 12:07

        There is still an error
        find ~/Downloads/ -empty -type -f -delete

        it should be
        find ~/Downloads/ -empty -type f -delete

  • Allan Nila Chongwe Oct 1, 2015 @ 8:09

    When deleting empty files, the above example will not work. For example if you execute the command ‘find ~/Downloads/ -empty -type -f -delete’ you will get the following error message; find: Arguments to -type should contain only one letter. The solution is to remove the minus (-) in front of the f option. Thus the correct command should be ‘find ~/Downloads/ -empty -type f -delete’ without the quotes (‘) ofcourse.

  • anthony Feb 3, 2016 @ 1:23

    What you must realise is that not all UNIX machines has the latest and greatest find!

    Solaris for example uses a very old version (no -empty or -delete).

    Here is one that finds OR finds and removes empty directories on either solaris or linux

    find “$dir” -depth -type d |
    while read sub; do
    # case “$sub” in */*) ;; *) continue ;; esac # sub-dir only
    [ “`cd “$sub”; echo .* * ?`” = “. .. * ?” ] || continue
    echo rmdir “$sub”
    #rmdir “$sub”

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum