Adam Horn Adam Horn - 10 months ago 27
Bash Question

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.

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

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.