You are not logged in.

#1 2005-12-10 11:15:20

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Check if a directory is not empty?

How to make an "if" statement checking if a directory is not empty.
Something like....

if [ `ls /test/*` !="" ]; then
echo "not empty"
fi


Markku

Offline

#2 2005-12-10 12:05:10

atze
Member
From: Netherlands (Leiden)
Registered: 2005-10-22
Posts: 76
Website

Re: Check if a directory is not empty?

if[ 'ls /dir/*' !="ls: /dir/*: No such file or directory" ]; then
     echo "not empty"
fi

Offline

#3 2005-12-10 19:44:27

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Check if a directory is not empty?

if [ `ls -A1 /test/ |wc -l` -gt 0 ]; then
    echo "directory is not empty"
fi

Offline

#4 2005-12-11 04:44:43

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: Check if a directory is not empty?

Thanks Penguin. Atxe's solution gave an error:
bash: [: ls test/*: unary operator expected


Markku

Offline

Board footer

Powered by FluxBB