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

Brett Parker iDunno at sommitrealweird.co.uk
Wed Feb 21 12:54:35 GMT 2007


On Wed, Feb 21, 2007 at 12:49:13PM +0000, 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....

Just a quick note - in bash, and any real posix shell, it's (generally)
better form to use $(command) than `command`, it's more readable and is
nestable if needed...

Mr Pashley has a few things to say about backticks, it's worth a quick
read if nothing else:
        http://www.davidpashley.com/blog/programming/shell/backticks.html

Cheers,
-- 
Brett Parker



More information about the main mailing list