Linux how do I remove all empty directories?

You can use program called cleanlinks. The cleanlinks program searches the directory tree descended from the current directory for symbolic links whose targets do not exist, and removes them. It then removes all empty directories in that directory tree. It was originally created for symbolic links based directories but works with normal directories too.

For example if you want to remove all empty directories from /tmp directory, type the command:
$ cd /tmp
$ cleanlinks

Please note that cleanlinks command is part of XFree86 project. Another method is to use combination of shell commands in script:

 [ -d $DIR ] && [ $(ls -l $DIR | wc -l) -eq 1  ] && rmdir $DIR ||  :

Save and execute a script:
$ dir1

You can also try out tmpreaper command which recursively searches for and removes files and empty directories which haven’t been accessed for a given number of seconds. Normally, it’s used to clean up directories which are used for temporary holding space, such as “/tmp”. Syntax is as follows:


  • TIME-FORMAT : Defines the age threshold for removing files. The TIME-FORMAT should be a number, defaulting to hours, optionally suffixed by one character: d for days, h for hours, m for minutes, or s for seconds.
  • DIRS : Directory name for example /tmp

For example, remove all files accessed 24h before:

# tmpreaper 24h /tmp

Please note that tmpreaper command is not installed by default you may need to install it using apt-get or rpm command.

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

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
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
5 comments… add one
  • don Feb 2, 2007 @ 1:34

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


    find -depth -type d -empty -exec rmdir {} \;

    is easier

  • Lingerance Sep 18, 2007 @ 7:59

    find . -type d -empty -delete

  • Frolic Jun 11, 2008 @ 13:48

    find . -type d -empty | xargs rmdir
    can be faster than using -exec if you find a large number of directories. To be safe if your directory names contain spaces or other characters that act as word separators:
    find . -type d -empty -print0 | xargs -0 rmdir
    The circular things are the digit zero, not the capital letter Oh. The -delete action mentioned above by Lingerance seems to be a nonstandard extension – GNU find doesn’t know about it.

  • Scott Dec 3, 2008 @ 3:30


    for v in targetdir/*; do [ $(ls -lA $v | wc -l) -eq 1 ] && rmdir $v; done;

    -A (almost all) option to ls reports hidden files (without . and .. entries). The for loop removes all empty directories.

  • steez May 6, 2011 @ 23:18

    Where can I install cleanlinks?

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