Shell tips: Detecting Empty Directories
13:30, 30.04.2024
Key Aspects
Administration of the Linux system is not a simple task and for doing it effectively, you would rather know commands and understand the main aspects of the processes. For instance, the important thing is to know if the directory is empty prior to dealing with any crucial operations.
Based on our practical experience in the field, we can give you a couple of instructions that will help with the determination of an empty directory. Here you will get practical recommendations that can be used in shell script and shell.
1. Employing the find Command
As a rule, find is used to search according to a specific pattern when speaking about directories and files. In this part, we will explain how this command can be used in another way particularly be used for checking whether a directory is empty.
1.1. -empty Option
According to the default characteristics of this command, -empty is checking for the directories and files. In order to change something and select only directories use the following:
$ find . -type d -empty
In this command line, -empty is used to specify that the search is only for the empty file or directory, and by using -type d we are mentioning that the search is only for the directories. This line will lead to the paths of the found directory that should be empty.
1.2. Command with if Statement
Here we will show you 2 variants of how to use if statements together with test command. The first approach counts the files in a certain directory, and it is the following:
if [ "$(find projects -mindepth 1 -maxdepth 1 | wc -l)" -eq 0 ]; then
echo "Empty Directory"
else
echo "Directory isn’t empty"
fi
In this script -eq 0 functions by comparing whether the number of items is zero, wc -l counts the items in the directory. -mindepth 1 – this part guarantees that find doesn’t have a directory in the search and -maxdepth 1 – this means that directories/files will be searched in a certain directory.
Another option looks like this:
if [ -z "$(find projects -mindepth 1 -maxdepth 1)" ]; then
echo "Empty Directory"
else
echo "Directory isn’t empty"
fi
In this script, -z indicates true in the case of the empty output. That means if the project is actually empty then you will be notified with this phase " Empty Directory".
2. Application of the ls Command
Probably the most uncomplicated way of checking the directory is the use of ls command. You won’t need to create any difficult scripts, just use the next line:
$ ls projects
The output of this line will show only directories that include some kind of information, so others that are not shown will be empty.
2.1. Application of the test Command with if Statement
To review the content of the directory, you will need to utilize ls together with another option, in such a way:
$ if [ -z "$(ls -A projects)" ]; then echo "Empty Directory "; else echo "Directory isn’t empty"; fi
In this line, A projects joined with ls is used to display the contents in all directories besides the current one and parent directory.
Moreover, it is possible to count the items’ number with:
$ if [ "$(ls -A projects | wc -l)" -eq 0 ]; then echo "Empty Directory"; else echo "Directory isn’t empty"; fi
3. Shopt Option
Shopt option is a command that allows unsetting and setting the shell options. With the use of -s argument in order to set nullglob. Such an option is responsible for how shell performs with patterns that have no matching files. For example, a script with nullglob that doesn’t match the file expands to an empty list while without it, the pattern is just a literal string.
# Path of directory
directory_path="/home/samuel/Desktop/projects"
# Enable the nullglob
shopt -s nullglob
# Whether is it an empty directory?
files=( "$directory_path"/* )
if [ ${#files[@]} -eq 0 ]; then
echo "Empty Directory"
else
echo "Directory isn’t empty"
fi
By using this Bash script, you will have an output “Empty Directory” in case it is empty or includes only hidden types of files.
4. Concluding Thoughts
We decided to share our practical experience and dive into various approaches that can be used to determine whether the directory is empty or not. The main approaches are the usage of ls and find commands.
After reviewing all the available variants, you can select what better works for your specific case based on the requirements and performance. Just understanding the mentioned techniques for scripts and shell commands will powerfully support the management of directories in Linux systems. Hope that our instructions are helpful to you!