Reputation: 65
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
Reputation: 1242
Slightly shorter:
[ ! "$(ls -A /path/to/directory)" ] && echo "Empty" || echo "Not Empty"
Upvotes: 0
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