Many times empty directories get cluttered in the Linux file system, and it becomes a difficult task to manually search for and delete each of them. The command rmdir (remove directory) is used in Linux to delete empty folders.
The command is quite simple to use and the basic syntax is:
$ rmdir <empty folder name1> <empty folder name1> ... <empty folder nameN>
Here, ‘empty folder name1‘, ‘empty folder name2‘, etc. are the names of the folders including the full path. If the folders are in the same directory then, as you might already know, there is no need to write down full paths.
You can also use wildcard expressions to remove empty directories with patterns in their names. For example, to remove empty directories with the substring ‘test‘ in their name, run:
$ rmdir *test*
However, to use rmdir we always need to specify the name (or full path) of each empty directory to be removed. There is no option in rmdir to recursively look for empty directories and then remove them.
We make use of the functionalities of the find command in such cases.
Find and Remove Empty Directories in Linux
The find command is used to search for files and folders in Linux based on different parameters like filename, size, type, etc. We will use find to determine empty directories recursively and then execute rmdir to delete the found directories.
Use the argument '-empty'
to look for empty objects and specify '-type d'
to only find directories.
$ find path_of_folder_to_search -type d -empty
To find empty directories recursively in the same folder, run:
$ find . -type d -empty
Now, since we already have the recursively found list of empty directories, we use the '-exec'
argument of the find command to run rmdir over them.
$ find . -type d -empty -exec rmdir {} \;
The placeholder {}
substitutes each entry in the list of found directories and '\;'
signifies the end of the command to be executed.
However, even with this, it will just do a single round of search and remove directories that are empty, but it will not remove directories that become empty after the first round of deletion.
To tackle this, we simply use the '-delete'
option, which will repeatedly delete all empty directories till the top-level directory.
$ find . -type d -empty -delete
This is how we can remove all empty directories recursively in Linux.
Conclusion
We learned how to use the rmdir command and find command in Linux to delete empty directories recursively. Learn more about these commands in their respective man pages:
$ man rmdir $ man find
Thanks for reading and do share your thoughts below!
It’s more efficient to end the exec command with
'+'
instead of';'
, as only one rm will be executed with the list of dirs as argument, instead of individual rms per dir.Also, you can use xargs like this:
Note the use of
-print0
and-0
, to manage directories with whitespace in them.Thank you very much, this was exactly what I needed to clean out my music library.
Doesn’t work “find: missing argument to `-exec'”
@TaZeR,
A
-exec
command must be end with a;
(so you generally need to type\;
or';'
to avoid interpretion by the Linux shell).The final command (with
-delete
) should not contain the-exec
option.@Roland,
Thanks for pointing out, corrected the command in the article…
Very helpful article. Thank you.