How do I tell when a directory is empty in a script?

Leslie Mikesell les at chinet.chi.il.us
Tue Apr 2 02:01:38 AEST 1991


In article <1991Mar30.040400.13893 at ncsu.edu> harris at catt.ncsu.edu (Michael Harris) writes:

>When I am running a shell script, how can I tell when a directory is empty?
>I tried looking at the output of ls -a but it includes . and ..

Usually the reason I want to know this is that I plan to do something
to all the visible files if there are any, so something like this
will work using the builtin "echo" command and only require the
directory to be read once.

LIST=`echo *`
if [ "*" = "$LIST" ]
 then :   # no files - do nothing
else
 for i in $LIST
 do
 ....  # commands to process files
 done
fi

Les Mikesell
  les at chinet.chi.il.us



More information about the Comp.unix.questions mailing list