[ALUG] How to check a directory is empty (probably in bash)?
(Ted Harding)
Ted.Harding at manchester.ac.uk
Wed Feb 21 12:22:45 GMT 2007
On 21-Feb-07 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.
>
> I can see various rather clumsy ways of doing it but nothing neat and
> elegant. A Google search didn't turn up much either, just some rather
> arcane zsh ways of doing it which were arcane enough to be zsh only I
> suspect.
>
> I might get carried away and write a little 'C' utility do do it if
> nothing sensible turns up.
Try on the following lines (test thoroughly first!), exemplified on an
empty directory "temp":
$ if [ "`ls -la temp | wc -l`" -eq 3 ] ; then echo "Empty" ; else echo
"Not Empty" ; fi
Empty
Now I "touch" a file in temp:
$ if [ "`ls -la temp | wc -l`" -eq 3 ] ; then echo "Empty" ; else echo
"Not Empty" ; fi
Not Empty
HTH
Ted
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Feb-07 Time: 12:22:42
------------------------------ XFMail ------------------------------
More information about the main
mailing list