1. Overview

Managing directories efficiently is a key aspect of Linux system administration. For example, we may need to check whether a directory is empty or not before performing any operations on it.

In this tutorial, we’ll explore different ways that we can use to determine if a directory is empty. To illustrate, we’ll define some of our instructions in the shell and others in a shell script.

2. Using the find Command

The find command helps us search for files and directories based on a specified pattern. However, in this section, we’ll use find to specifically check if a directory is empty.

2.1. Using the -empty Option

By default, the -empty option instructs find to check for empty files and directories. Here, we’ll concentrate on directories:

$ find . -type d -empty
  • -type d – declares the type of file to search for; in this case, only directories
  • -empty – specifies that we’re only searching for empty directories in the specified path; in this case, the current directory (.)

So, when we execute this command, it looks for empty directories starting from the current directory. It shows the paths of any empty directories it finds.

2.2. Using test Command With if Statement

Here, we’ll use test, a built-in Bash command useful for evaluating conditional expressions. In practice, we can use it as part of an if statement to check for an empty directory. To clarify, we’ll use its shorthand syntax:

[ expression ]

First, let’s define an expression that utilizes the -z option:

#!/bin/bash

if [ -z "$(find projects -mindepth 1 -maxdepth 1)" ]; then
    echo "Directory is empty."
else
    echo "Directory is not empty."
fi

Let’s take a closer look at each part of our test expression:

  • find projects – lists all files in the projects directory
  • -mindepth 1 – ensures that find doesn’t include the directory itself in the search
  • -maxdepth 1 – ensures that find only looks for files/directories in the specified directory and not its subdirectories
  • -z – returns true if the output string is empty

The -z option is designed to specifically look for an empty string. So, if projects is empty, the script prints Directory is empty with the help of the echo command.

Another approach that we can take is counting the number of files and directories found by the find command in the specified directory:

#!/bin/bash

if [ "$(find projects -mindepth 1 -maxdepth 1 | wc -l)" -eq 0 ]; then
    echo "Directory is empty"
else
    echo "Directory is not empty"
fi

Let’s break it down:

  • wc -l – counts the number of lines in the output string, which in this case is the number of items in the directory
  • -eq 0 – does an equality check to compare if the number of items found is equal to 0

Both scripts work correctly to display whether the specified directory is empty.

3. Using the ls Command

Typically, the basic use of the ls command is to list the contents of a directory. For this reason, using it to check whether a directory is empty is pretty straightforward:

$ ls projects

If the specified projects directory is empty, there will be no output. However, if it contains any files or subdirectories, they will be displayed.

3.1. Using test Command With if Statement

Here, we’ll leverage ls in combination with the -A option to list the contents of our directory:

$ if [ -z "$(ls -A projects)" ]; then echo "Directory is empty."; else echo "Directory is not empty."; fi
  • ls -A projects – list all the contents in the projects directory except for the . and .. entries. The . entry represents our current directory while the .. entry represents the parent directory.
  • $(…) – holds the output of the ls command
  • -z – this test parameter returns true if the output string is empty and false if not

Above, we utilize the if statement and the echo command to print whether the projects directory is empty.

Also, we can count the number of items:

$ if [ "$(ls -A projects | wc -l)" -eq 0 ]; then echo "Directory is empty"; else echo "Directory is not empty"; fi

This command establishes the number of items and then checks whether this total is equal to zero.

4. Using the shopt Option

In Bash, shopt is a built-in command that enables setting and unsetting of shell options. Here, we’ll use the -s argument to set the nullglob option. This option influences how the shell handles patterns that match no files. For instance, without nullglob, a pattern that doesn’t match any files is treated as a literal string. On the other hand, with nullglob, the same pattern expands to an empty list:

#!/bin/bash

# Define the directory path
directory_path="/home/samuel/Desktop/projects"
# Enable the nullglob option
shopt -s nullglob

# Check if the directory is empty
files=( "$directory_path"/* )

if [ ${#files[@]} -eq 0 ]; then
    echo "Directory is empty"
else
    echo "Directory is not empty"
fi

Above, we define our instructions in a Bash script. To explain:

  • directory_path – represents the location of the specified directory
  • shopt -s nullglob – sets up the nullglob option
  • files=( “$directory_path”/* ) – stores the list of matched files and directories in an array. The * wildcard matches all files and directories within the projects directory.
  • if [ ${#files[@]} -eq 0 ]; then – checks whether the files array is empty

This approach ensures that if the directory is empty or contains only hidden files (prefixed with .), the script identifies it as empty.

5. Conclusion

In this article, we explored multiple approaches for checking whether a directory is empty. In detail, they include using find, the ls command, and array manipulations with the shopt option.

The choice of which one of these approaches to use can be based on preference or requirements. Equally important, understanding these methods allows us to write efficient shell commands or scripts to manage directories in Linux.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.