Bash: How to Find All Empty Directories


Often you may want to find all empty directories using Bash.

You can use the following syntax to do so:

find . -type d -empty

This will return the path to all empty directories within the current directory.

Here is what the various commands do:

  • find: used to “find” specific files or folders
  • -type d: Specifies that we want to find directories only
  • -empty: Specifies that we want to find empty directories only

The following example shows how to use this syntax in practice.

Example: How to Find All Empty Directories in Bash

Suppose that our current directory is located at the following path:

  • c:/users/bobbi/data1

We can use the following syntax to return the paths to all folders and subfolders within this directory:

find . -type d

The following screenshot shows how to use this syntax in practice:

Now suppose that we would like to return only the empty directories.

We can use the following syntax to do so:

find . -type d -empty

The following screenshot shows how to use this syntax in practice:

Bash find empty directories

Notice that this returns the path to three directories.

From the output we can see:

  • The athletes directory within the central_data directory is empty.
  • The managers directory within the central_data directory is empty.
  • The pacific_data directory is empty.

If we use the ls command to view the files and folders of any of these directories, nothing will be returned since these directories are empty.

For example, suppose we use the following syntax to view all files and folders in the pacific_data directory:

ls ./pacific_data

Notice that no file or folder names are returned because this directory is empty.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Delete All Empty Lines in File
Bash: How to Delete All Files in Directory
Bash: How to Find All Files with Specific Extension

Leave a Reply