On Wed, Feb 21, 2007 at 11:42:10AM +0000, Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
I can't just do a rmdir as I need to check three directories are empty before I can do anything drastic (like removing the directory). Not to mention that for one application of this I don't want to remove the empty directory anyway.
As you've probably guessed this is to check if a maildir mailbox is empty - to do this you need to check if all three subdirectories of the maildir are empty.
Well, I'd do something like: function directory_empty() { filecount=$(ls -lAU "$1" | sed '1{s/total //; p}; 1,$d;') if [[ $filecount -gt 0 ]]; then return 1 else return 0 fi }
Then you can use it as such: if (directory_empty $directory); then echo "It's empty." fi
OK - it's not the prettiest code in the world, but it *should* do what you need!
Cheers,