bug-bash
[Top][All Lists]
Advanced

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

Re: best way to test for empty dir?


From: Matias A. Fonzo
Subject: Re: best way to test for empty dir?
Date: Thu, 10 Dec 2009 17:37:04 -0200

On Thu, 10 Dec 2009 17:31:20 +0000
Marc Herbert <Marc.Herbert@gmail.com> wrote:

> Hi,
> 

Hello

> Does anyone know a more elegant way to check for file existence?
> Something that does not fork a subshell. And is also more readable
> maybe. And is obviously not much longer.
> 
> empty_dir() 
> { 
>     test "x$(echo $1/*$2)" = "x$1"'/*'"$2"
> }
> 
> 
> Warning: I find neither "noglob" nor "ls" elegant, sorry!
> 

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

-- 
Matias A. Fonzo <selk@dragora.org>




reply via email to

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