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:
#/bin/bash DIR="$1" [ -d $DIR ] && [ $(ls -l $DIR | wc -l) -eq 1 ] && rmdir $DIR || :
Save and execute a script:
$ script.sh 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:
tmpreaper TIME-FORMAT DIRS
Where,
- 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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop








![HowTo: Use grep Command In Linux / UNIX [ Examples ]](http://s13.cyberciti.org/images/shared/rp/3/9.jpg)


{ 4 comments… read them below or add one }
perl -MFile::Find -e “finddepth(sub{rmdir},’.')”
or
find -depth -type d -empty -exec rmdir {} \;
is easier
find . -type d -empty -delete
find . -type d -empty | xargs rmdircan be faster than using
-execif 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 rmdirThe circular things are the digit zero, not the capital letter Oh. The
-deleteaction mentioned above by Lingerance seems to be a nonstandard extension – GNU find doesn’t know about it.Try:
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.