Bash Shell Script To Test For Empty Folder

You can use the following bash shell script to test for an empty folder:

#!/bin/bash

folder=$1

if [ -d "$folder" ]
then
if [ -z "$(ls -A $folder)" ]
then
echo "The folder $folder is empty."
else
echo "The folder $folder is not empty."
fi
else
echo "The folder $folder does not exist."
fi

This script takes a folder name as an argument, and it checks if the folder exists and whether it is empty or not. If the folder exists and is empty, the script will print “The folder $folder is empty.” If the folder exists and is not empty, the script will print “The folder $folder is not empty.” If the folder does not exist, the script will print “The folder $folder does not exist.”

Leave a Comment