How to Find All Empty Directories in Ubuntu Linux

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.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. Determine the parent directory you want to scan. For this example, we will assume you want to scan the /var/www/html/ folder.
  3. Type the following command:
    sudo find /var/www/html/ -type d -empty
  4. Press Enter and provide your administrator password.

How the Command Works

  • sudo gives you permission to view all folders.
  • /var/www/html/ is the target location.
  • -type d tells the search engine to ignore empty text files and only look for directories.
  • -empty applies 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.

  1. 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.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.