On Wednesday 21 February 2007 13:03, Eur Ing Chris Green wrote:
On Wed, Feb 21, 2007 at 12:49:13PM +0000, Paul wrote:
On Wednesday 21 February 2007 11:42, Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
if test -z `ls foo` ; then wibble ; fi
Or
if [ -z `ls foo` ] ; then wibble ; fi
That's neat! It's not actually perfect because hidden files in the directory will break it, but it's perfect for my application as I don't care about hidden files.
There's probably other ways....
No doubt, but the above will do very well, thanks (and for all the other ideas).
Just a thought, you could use something like: find dirname -maxdepth 1 -empty -type d which I believe would return the string "dirname" if it's empty and something else if it's not - correct me if I'm wrong there. Also, you could pop an "isemptydir" snippet in your path like: #!/usr/bin/env python import glob, os, sys args=[] if len(sys.argv) < 2: args=['','.'] elif len(sys.argv) > 2: print """Usage: isemptydir [path...] isemptydir - test for whether current directory is empty. isemptydir <path> - test for whether the directory <path> is empty. Exit status is 0 if true, 1 if false, 2 if weird arguments were passed.""" sys.exit(2) else: args=sys.argv[:] if glob.glob(os.path.join(args[1],"*")) == [] and glob.glob(os.path.join(args[1],".*")) == []: sys.exit(0) else: sys.exit(1) ...so you can do stuff in your shell like if isemptydir /opt/PlaneShift/; then echo "Yes"; else echo "No"; fi No or if isemptydir; then echo "Our CWD is empty. Do stuff here";fi -- There are 10 types of people in this world, those who understand binary, and those who don't.