On 11:12 07 Mar 2003, Zhi Cheng Wang <[EMAIL PROTECTED]> wrote:
|From: Anand Buddhdev [mailto:[EMAIL PROTECTED]
|> On Fri, Mar 07, 2003 at 08:52:56AM -0000, Zhi Cheng Wang wrote:
|> > how can i test if a directory is empty? at the moment i use the following method:
|> > 
|> > ls -l /dirname | grep "total 0"
|> > if [ $? ] then ....
|> 
|> This is not a good test. "ls -l" will not reveal dot-files.
|> 
|> Use "ls -A" which will list all files, including the dotted ones, but
|> excluding '.' and '..'.
|> 
|> if [ -z `ls -A /dirname` ]; then echo "empty"; fi
| 
| this seems a very good test, but the system says "[: too may arguments"

Yeah, Anand didn't quote it. Try this:

        if [ -z "`ls -A /dirname`" ]; then ......

You have to make the output of ls stick together as one string so that the
"-z" has exactly one argument.

I case actually think of a pathological case where it won't work, too,
because backticks strip some whitespace, so if you have a file in the
whole name is pure whitespace...

Perhaps we should ask: why do you want to know if it's empty?  There may
be a cleaner approach. In particular, I would point out that rmdir WILL
NOT remove a nonempty directory, so you can go:

        rmdir /dirname 2>/dev/null

with impunity, IF all you want to do is clean up an empty directory
without risk of losing files if it's not empty.

More background on your larger task please?
-- 
Cameron Simpson, DoD#743        [EMAIL PROTECTED]    http://www.zip.com.au/~cs/

George, discussing a patent and prior art:
"Look, this  publication has a date, the patent has a priority date,
can't you just compare them?"
Paul Sutcliffe:
"Not unless you're a lawyer."



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to