The Problem with Empty Directories
When you uninstall complex software packages or run automated data scraping scripts on an Ubuntu Linux server, the system often leaves behind dozens of empty subdirectories. While these folders do not consume meaningful hard drive space, they heavily clutter your file system tree. If you are navigating a massive web server directory or analyzing a codebase, having to click through three levels of folders only to find they contain absolutely nothing is a massive waste of time. Instead of hunting them down manually, you can use a powerful terminal command to locate them all instantly.
Using the find Command for Empty Folders
The standard Linux find command includes a specific flag designed exactly for this scenario: -empty.
- Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
- Determine the parent directory you want to scan. For this example, we will assume you want to scan the
/var/www/html/folder. - Type the following command:
sudo find /var/www/html/ -type d -empty - Press Enter and provide your administrator password.
How the Command Works
sudogives you permission to view all folders./var/www/html/is the target location.-type dtells the search engine to ignore empty text files and only look for directories.-emptyapplies the final filter, displaying only directories that contain absolutely zero files or subfolders.
How to Automatically Delete Them
Once you run the command above and verify that the output list consists entirely of useless, orphaned folders, you can instruct the find command to automatically delete them as it finds them by appending the -delete flag.
-
sudo find /var/www/html/ -type d -empty -delete
Warning: Always run the search command without the delete flag first. Review the list carefully to ensure you are not deleting a crucial structural folder required by an application before you execute the final deletion command.