Bill Herrman
Bill Herrman

Reputation: 65

If directory has no files then ... - Linux

I'm looking to see if a directory has no files in it or not. I do not want to take folders into account.

Right now I have ls -1ap | grep -v / | wc -l which will give me the number of files in the directory, but I cant seem to incorporate that into an if statement.

if ls -1ap | grep -v / | wc -l < 1; then echo "one"; else echo "two"; fi

Is there anything I can tweak a bit to get this to work? Thank you

Upvotes: 0

Views: 108

Answers (2)

Yaron
Yaron

Reputation: 1242

Slightly shorter:

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

Upvotes: 0

alamar
alamar

Reputation: 19313

if [ `ls -1ap | grep -v / | wc -l` == 0 ]; then echo empty; fi

And what you are looking for is man test (that's the `[')

Note that I didn't look into whether your condition is optimal, just rewrote it in test-form.

Upvotes: 1

Related Questions