How to check whether a directory is empty or not in Shell Scripting?

22,342

From here. This should help you run your statements within the if else loop. I saved the DIR in the variable

#!/bin/bash
FILE=""
DIR="/empty_dir"
# init
# look for empty dir 
if [ "$(ls -A $DIR)" ]; then
     echo "Take action $DIR is not Empty"
else
    echo "$DIR is Empty"
fi
# rest of the logic
Share:
22,342
Animesh
Author by

Animesh

Updated on March 28, 2020

Comments

  • Animesh
    Animesh over 4 years

    I have a directory. It is empty. If i perform ls -lrt , it shows total 0

    How do I specify an If condition to perform something, only if the directory is empty.

    I mean to ask how to capture that 0 value.