Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
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.
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.
By default, the -empty option instructs find to check for empty files and directories. Here, we’ll concentrate on directories:
$ find . -type d -empty
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.
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:
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:
Both scripts work correctly to display whether the specified directory is empty.
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.
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
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.
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:
This approach ensures that if the directory is empty or contains only hidden files (prefixed with .), the script identifies it as empty.
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.