How To Find and Delete Empty Directories in Linux?

How To Find And Delete Empty Directories In Linux

Got too much clutter to clear on your system? It is hard and time-consuming to search for empty directories and delete them one by one. But, Using some commands, You can delete all the empty directories at once. In this article, We will discuss commands like rmdir, rm and find commands in detail.

When deleting directories using the command line, they are deleted permanently i.e. they are not moved to trash. rmdir and rm commands can only be used to manually delete one or multiple directories at a time. The find command is used to search all the empty directories and delete them all using a single command.

Using rmdir command

rmdir command is used to delete empty directories. It only deletes the empty directories, so, if the directory is not empty it will show an error. You don’t need to check for empty directories while using this command, To use rmdir command, Let’s take an example, we have a directory named test-directory which is not empty:

rmdir test-directory
remove-empty-dir-1

If the directory is empty:

rmdir test-directory
remove-empty-dir-2

If you want to delete all empty directories all at once, rmdir command cannot be used. To know more about the rmdir command, Run man rmdir.

Using rm command

Unlike rmdir command, rm command is used to delete both files and directories whether empty or non-empty. When using rm command without any parameters, it will not delete directories:

rm test-directory
remove-empty-dir-3

use -d with the above command to remove empty directories:

rm -d test-directory
remove-empty-dir-4

To remove directories along with their contents, use -r or -R or –recursive. Run the following command:

rm -rf test-directory 
remove-empty-dir-5

There are other parameters like -f to never prompt, -i to prompt before every removal. Run man rm or click here to know more about the rm command.

Using the find command

The find command is used to search for files and directories. Using find command along with some parameters, We can search all the empty directories in the system and delete them at once.

find </path> -type d -empty

Here,

  • </path> :- path of the directory to search
  • -type d :- limits the search only to directories ( you can also use -type f to search for files.
  • -empty :- limits the search only to empty directories
find /sid/Documents -type d -empty
remove-empty-dir-6

Use -delete with the above command to delete all the empty directories in the specified location. -name ‘*.extension’ to search and delete files or directories ending with the specified extension.

find /sid/documents -type d -empty -delete
remove-empty-dir-7

There are several parameters used to search for files or directories as per our requirements. Run man find or click here to open the find manpage.

Conclusion

So, We discussed how to find and delete empty directories using three different commands. These are easy commands to try, even for beginners. Thank you for reading!