Bash Shell Check Whether a Directory is Empty or Not

by Vivek Gite · 7 comments

Q. How do I check whether a directory is empty or not under Linux / UNIX using a shell script? I'd like to take some action if directory is empty.

A. There are many ways to find out if a directory is empty or not under UNIX / Linux bash shell. You can use find command to list only files. For example, following find command will only print file name from /tmp. If there is no output, directory is empty.

$ find "/tmp" -type f -exec echo Found file {} \;
Output:

Found file /tmp/_.c
Found file /tmp/orbit-vivek/bonobo-activation-server-ior
Found file /tmp/orbit-vivek/bonobo-activation-register.lock
Found file /tmp/_.vsl
Found file /tmp/.X0-lock
Found file /tmp/.wine-1000/server-802-35437d/lock
Found file /tmp/.wine-1000/cxoffice-wine.lock
Found file /tmp/ksocket-vivek/Arts_PlayObjectFactory
Found file /tmp/ksocket-vivek/Arts_SimpleSoundServer
Found file /tmp/ksocket-vivek/secret-cookie
Found file /tmp/ksocket-vivek/Arts_AudioManager
Found file /tmp/ksocket-vivek/Arts_SoundServer
Found file /tmp/ksocket-vivek/Arts_SoundServerV2
Found file /tmp/vcl.XXf8tgOA
Found file /tmp/Tracker-vivek.6126/cache.db
Found file /tmp/gconfd-vivek/lock/ior

However, the simplest and most effective way is to use ls command with -A option:

$ [ "$(ls -A /path/to/directory)" ] && echo "Not Empty" || echo "Empty"
or
$ [ "$(ls -A /tmp)" ] && echo "Not Empty" || echo "Empty"

Use if..else.fi in a shell script:

#!/bin/bash
FILE=""
DIR="/tmp"
# 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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 7 comments… read them below or add one }

1 David 11.23.07 at 7:36 pm

Seems to me that using ls is not required – in fact, it is doable within the shell alone:

set - `echo .* *`
if [ $# = "2" ] ; then
: empty directory
else
: not empty ...
fi

David
UNIX Administratosphere

2 Raju 11.23.07 at 9:06 pm

David,

It is not working for me. I’m using Debian + Bash 3. It returns 3 when directory is empty, it should be 2 as empty directory has only . and ..

Any idea?

3 David 11.23.07 at 9:29 pm

Yes. I’d forgotten: when no matches are found (“*”) then the resulting text is the character unchanged. This should work better:

FILES=”`echo .* *`”
if [ $FILES = '. .. *' ] ; then
: empty dir
else
: not empty
fi

4 Scot 01.06.09 at 1:06 am

find -type d -empty

5 Tomas M 05.21.09 at 12:48 pm

Thanks Scot!

6 Kyle 10.15.09 at 6:48 pm

Scot’s suggestion works absolutely perfectly.

7 Indie 02.02.10 at 5:49 pm

A slight variation on the original post which does it numerically and includes dot files.

DIR=/home/user
[ $(( $(ls -a1 $DIR | wc -l) >= 3 )) = 0 ] && echo Empty || echo Nope

That’s a one and not an ‘l’ to the ls command. An empty directory only has the two entries – . & ..

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All