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: Sven Mascheck
Subject: Re: best way to test for empty dir?
Date: Fri, 11 Dec 2009 13:52:33 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

On Fri, Dec 11, 2009 at 12:31:49PM +0000, pk wrote:
> Marc Herbert wrote:

> > is_file3()
> > { 
> >     for f
> >     do
> >         [ -e "$f" -o -L "$f" ] && return
> >     done
> >     return 1
> > }
> 
> You might also want to enable "dotglob" to catch hidden files...

    empty=yes
    for i in .?* *
    do  
        test "$i" = '..' || test -e "$i" && empty=no && break
    done

and with test -L added as needed.
Or (beacuse test -e is not strictly traditionally available)
and without special glob options:

    empty=no
    for i in .* * ; do
            case "$i" in
            .|..)   : ;;
            \*) for i in ? ; do
                        test "$i" = \? && empty=yes ; break
                done ; break ;;
            *) break ;;
            esac
    done


or just (if builtins-only is not strictly required)

    test -z "`find . ! -name . -prune | sed q`"

comp.unix.shell might match well here and could be entertaining -
IMHO worth to migrate; objections?




reply via email to

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