[ALUG] How to check a directory is empty (probably in bash)?

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Wed Feb 21 13:00:57 GMT 2007


On 21-Feb-07 Paul wrote:
> On Wednesday 21 February 2007 11:42, Eur Ing Chris Green wrote:
>> What's the simplest (and/or most concise) way to check for an empty
>> directory?
> 
> if test -z `ls foo` ; then wibble ; fi
> 
>  Or
> 
> if [ -z `ls foo` ] ; then wibble ; fi
> 
> There's probably other ways....
> 
> Regards, Paul.

NB: `ls foo` will not detect filenames in foo that begin with "."

Hence my suggestion using "ls -la"; but this then outputs the
"." for current, and ".." for parent directories, regardless
of whether "foo" is empty (and also a first line which gives
the "total"). Hence the test for "-eq 3".

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Feb-07                                       Time: 13:00:54
------------------------------ XFMail ------------------------------



More information about the main mailing list