[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: best way to test for empty dir?

From: Greg Wooledge
Subject: Re: best way to test for empty dir?
Date: Thu, 10 Dec 2009 16:34:44 -0500
User-agent: Mutt/

On Thu, Dec 10, 2009 at 05:37:04PM -0200, Matias A. Fonzo wrote:
> Maybe you want the Chris F.A Johnson's implementation [1]:
> set -- "/tmp/emptydir"/*
> [[ -f $1 ]] && echo non-empty || echo empty;
> References:
> [1] 
> http://www.issociate.de/board/goto/866027/checking_if_a_directory_is_empty.html

The -f in the [[...]] should be -e, or it may give erroneous results if
the first thing matched by the glob happens to be a subdirectory (or
anything other than a plain file).

It's just a positional-parameter variant of:

if [[ -e ${files[0]} ]] ...

Has the disadvantage that it clobbers the positional parameters, which
maybe you still want.  Has the advantage that it'll work in ksh88,
which doesn't support the array=(...) syntax.  They're both non-POSIX
though (due to the [[...]]).  Of course, the PP variant is easier to
convert into a working POSIX version, since POSIX has no guarantee of

reply via email to

[Prev in Thread] Current Thread [Next in Thread]