how to delete all empty directories in linux
You might already know that in Linux a directory have to be empty before it can be deleted. Well…not quite true, you can force delete the directories. What if you want to remove only empty directories recursively from the folder structure?
There are a couple of different ways to do this. We will see some of the options from the bash command line. However, these should work with any other shell as well.
using the find command with delete option
The find command can be used to find the empty directories recursively and to delete them. find is one the most powerful and versatile commands in Linux and it would serve you right to learn the ins and outs of it. So, an example to delete directories using find would look something like
bash$ find /path/to/dir/ -type d -empty -delete
- path/to/dir : the path to the directory
- -type : filters by the file type. d is for directories, f is for files, b is for block etc.
- -empty : match against empty directories (or files)
- -delete : execute deletion on the matched directories or files
Some versions of find do not support the delete option. In that case you will need to delete the directories using another command such rm or rmdir.
using find command with exec option
The above example was handy enough as we were using the command line option -delete of the find command. But you could use the -exec command line option that will allow you to use the rm command.
bash$ find /path/to/dir/ -type f -empty -exec rm -f "{}" \;
You can use any command that you would use to delete a single folder in Linux. You could very well use rmdir instead of rm in the above example. The disadvantage of using this instead of the --delete option is that it is quite slow.
Also, this will not delete those directories that have become empty as a result of running this command. This means you might need to run this command multiple times, or until no directories are removed. However, you could use the -depth option of the find command that will process the directory contents before the directory itself. You can group the rm invocations as well to make it a little faster.
If you don't want the verbose output then redirect the stderr to null device.
bash$ find /path/to/dir/ -depth -type f -empty -exec rm -f "{}" + 2>/dev/null
- path/to/dir : the path to the directory
- -type : filters by the file type. d is for directories, f is for files, b is for block etc.
- -empty : match against empty directories (or files)
- -depth or -d : process the contents of the directory before the parent
using rmdir
If you just want to delete empty folders in the current working directory or folders that are just one level deep, you could use the rmdir command. You could avoid all of the typing and use the rmdir command if it is available on your distro. It will remove the directories that are empty
bash$ rmdir -p /path/to/dir/*
The --p or –parents option will remove the directory and then its parents. Unfortunately, rmdir does not have a recursive option unless it is piped through another command such as find. But this will work if you want to remove only directories one level deep.