[comp.unix.questions] testing if a directory is empty

pauld@cs.washington.edu (Paul Barton-Davis) (04/16/91)

Oops - correction to previous posting

i didn't bother to check the logic of the echo, i just
shortened the syntax. in fact, the whole idea is broken:

	$ mkdir x
	$ cd x
	$ files=`echo *`
	$ echo $files
	*

So it needs to be:

	[ "`cd $1 ; echo .??* *`" = ".??* *" ]

this picks up a dot files too (anyone who has a dot file called ".a"
should be taken out and shot :-)
-- 
Paul Barton-Davis			<pauld@cs.washington.edu>
UW Computer Science Lab		``to shatter tradition makes us feel free''

meissner@osf.org (Michael Meissner) (04/16/91)

In article <1991Apr15.195513.17840@beaver.cs.washington.edu>
pauld@cs.washington.edu (Paul Barton-Davis) writes:

	...

| So it needs to be:
| 
| 	[ "`cd $1 ; echo .??* *`" = ".??* *" ]
| 
| this picks up a dot files too (anyone who has a dot file called ".a"
| should be taken out and shot :-)

It still isn't any reason to fail.  Off of the top of my head, the
following should work:

	[ "`cd $1; echo .[^.] .??* *`" = ".[^.] .??* *" ]
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Considering the flames and intolerance, shouldn't USENET be spelled ABUSENET?

pauld@stowe.cs.washington.edu (Paul Barton-Davis) (04/17/91)

In article <MEISSNER.91Apr15221545@curley.osf.org> meissner@osf.org (Michael Meissner) writes:
>In article <1991Apr15.195513.17840@beaver.cs.washington.edu>
>pauld@cs.washington.edu (Paul Barton-Davis) writes:
>

         [ my stupid idea deleted ]

>
>It still isn't any reason to fail.  Off of the top of my head, the
>following should work:
>
>	[ "`cd $1; echo .[^.] .??* *`" = ".[^.] .??* *" ]
>--
>Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
>Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142
>
>Considering the flames and intolerance, shouldn't USENET be spelled ABUSENET?

I may be wrong, but it seems regrettably not. From the man page for csh(1): 

	The sequence `[...]'  matches any one of the characters enclosed.
	Within `[...]', a pair of characters separated by `-' matches any
	character lexi- cally between the two.

and from sh(1):

	  [ ... ]
	       Matches any one of the characters enclosed.  A pair of char-
	       acters separated by - matches any character lexically
	       between the pair.


These shell's don't know about the "[^" convention, and indeed:

	# echo .[^.] 
	..			<can someone explain this>
	# echo .[^f]
	echo: No Match
	
Your guess is as good as mine. What will work, for 7-bit systems
anyway, is:

	echo .[^@--/-~]* *

This matches ALL dots files except . and ... Unfortunately, my csh
(Ultrix 4.1) doesn't handle the inclusion of ascii 127 (DELETE) in
this list very graciously, and if you have non-ASCII (> code 127)
chars in any dot file file name, this will also break. I increasingly
feel that the "rmdir; if [ $? ..." approach is the best way to
crack this.
-- 
Paul Barton-Davis			<pauld@cs.washington.edu>
UW Computer Science Lab		``to shatter tradition makes us feel free''

ires@kaspar.ires.com (Bruce R Larson) (04/19/91)

>In article < ... > pauld@cs.washington.edu (Paul Barton-Davis) writes:
>| So it needs to be:
>| 
>| 	[ "`cd $1 ; echo .??* *`" = ".??* *" ]

In article < ... > meissner@osf.org (Michael Meissner) writes:
> ... the following should work:
>
>	[ "`cd $1; echo .[^.] .??* *`" = ".[^.] .??* *" ]

Here's how I've checked for empty dirs

#!/bin/sh
if [ "`ls -la $1 | wc -l`" -eq 3 ]; then echo empty; fi


NOTE: every "ls -l" I've used lists the "total" number of
entries on its first line of output, hence the "3".

Bruce
-- 
Bruce R. Larson
Integral Resources, Milton MA
Internet:  blarson@ires.com
Uucp:  ..!{world|uunet}!ires.com!blarson