What's the simplest (and/or most concise) way to check for an empty directory?
I can't just do a rmdir as I need to check three directories are empty before I can do anything drastic (like removing the directory). Not to mention that for one application of this I don't want to remove the empty directory anyway.
As you've probably guessed this is to check if a maildir mailbox is empty - to do this you need to check if all three subdirectories of the maildir are empty.
I can see various rather clumsy ways of doing it but nothing neat and elegant. A Google search didn't turn up much either, just some rather arcane zsh ways of doing it which were arcane enough to be zsh only I suspect.
I might get carried away and write a little 'C' utility do do it if nothing sensible turns up.
On Wed, Feb 21, 2007 at 11:42:10AM +0000, Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
I can't just do a rmdir as I need to check three directories are empty before I can do anything drastic (like removing the directory). Not to mention that for one application of this I don't want to remove the empty directory anyway.
As you've probably guessed this is to check if a maildir mailbox is empty - to do this you need to check if all three subdirectories of the maildir are empty.
Well, I'd do something like: function directory_empty() { filecount=$(ls -lAU "$1" | sed '1{s/total //; p}; 1,$d;') if [[ $filecount -gt 0 ]]; then return 1 else return 0 fi }
Then you can use it as such: if (directory_empty $directory); then echo "It's empty." fi
OK - it's not the prettiest code in the world, but it *should* do what you need!
Cheers,
On Wed, Feb 21, 2007 at 11:42:10AM +0000, Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
Is it [ -d $dirname ] && [ $dirname/* == "$dirname/*" ] in Bourne shell?
Regards,
On Wed, Feb 21, 2007 at 12:41:41PM +0000, MJ Ray wrote:
On Wed, Feb 21, 2007 at 11:42:10AM +0000, Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
Is it [ -d $dirname ] && [ $dirname/* == "$dirname/*" ] in Bourne shell?
ENOCIGAR
brettp@fudge:~$ if [ -d empty ] && [ empty/* == "empty/*" ]; then echo empty; fi empty brettp@fudge:~$ if [ -d .xmms ] && [ .xmms/* == ".xmms/*" ]; then echo empty; fi bash: [: too many arguments brettp@fudge:~$
Cheers,
[Sorry -- sent earlier version from wrong email address: Moderator -- pleae ignore earlier message]
On 21-Feb-07 Eur Ing Chris Green wrote:
What's the simplest (and/or most concise) way to check for an empty directory?
I can't just do a rmdir as I need to check three directories are empty before I can do anything drastic (like removing the directory). Not to mention that for one application of this I don't want to remove the empty directory anyway.
As you've probably guessed this is to check if a maildir mailbox is empty - to do this you need to check if all three subdirectories of the maildir are empty.
I can see various rather clumsy ways of doing it but nothing neat and elegant. A Google search didn't turn up much either, just some rather arcane zsh ways of doing it which were arcane enough to be zsh only I suspect.
I might get carried away and write a little 'C' utility do do it if nothing sensible turns up.
Try on the following lines (test thoroughly first!), exemplified on an empty directory "temp":
$ if [ "`ls -la temp | wc -l`" -eq 3 ] ; then echo "Empty" ; else echo "Not Empty" ; fi Empty
Now I "touch" a file in temp:
$ if [ "`ls -la temp | wc -l`" -eq 3 ] ; then echo "Empty" ; else echo "Not Empty" ; fi Not Empty
HTH Ted
-------------------------------------------------------------------- E-Mail: (Ted Harding) Ted.Harding@manchester.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 21-Feb-07 Time: 12:22:42 ------------------------------ XFMail ------------------------------
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
There's probably other ways....
Just a quick note - in bash, and any real posix shell, it's (generally) better form to use $(command) than `command`, it's more readable and is nestable if needed...
Mr Pashley has a few things to say about backticks, it's worth a quick read if nothing else: http://www.davidpashley.com/blog/programming/shell/backticks.html
Cheers,
On Wed, Feb 21, 2007 at 12:54:35PM +0000, Brett Parker 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
There's probably other ways....
Just a quick note - in bash, and any real posix shell, it's (generally) better form to use $(command) than `command`, it's more readable and is nestable if needed...
Mr Pashley has a few things to say about backticks, it's worth a quick read if nothing else: http://www.davidpashley.com/blog/programming/shell/backticks.html
Very true (the comments above backticks that is), however:-
At work my development machine is a Solaris system where (on the default shell at least) the $(command) syntax doesn't work. I use a lot of the scripts I develop both at home and at work.
Backticks are still more 'universal', for example backticks work in mutt's configuration file.
On Wed, Feb 21, 2007 at 01:08:34PM +0000, Eur Ing Chris Green wrote:
On Wed, Feb 21, 2007 at 12:54:35PM +0000, Brett Parker 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
There's probably other ways....
Just a quick note - in bash, and any real posix shell, it's (generally) better form to use $(command) than `command`, it's more readable and is nestable if needed...
Mr Pashley has a few things to say about backticks, it's worth a quick read if nothing else: http://www.davidpashley.com/blog/programming/shell/backticks.html
Very true (the comments above backticks that is), however:-
At work my development machine is a Solaris system where (on the default shell at least) the $(command) syntax doesn't work. I use a lot of the scripts I develop both at home and at work.
Yeah - but Solaris' /bin/sh isn't POSIX compliant, it's a broken evil thing - I'm fairly sure that they haven't bothered to fix it yet because a lot of people worked round the fact that solaris had a broken /bin/sh by default and so wrote around the broken shell.
Cheers,
On Wed, Feb 21, 2007 at 01:16:51PM +0000, Brett Parker wrote:
On Wed, Feb 21, 2007 at 01:08:34PM +0000, Eur Ing Chris Green wrote:
On Wed, Feb 21, 2007 at 12:54:35PM +0000, Brett Parker 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
There's probably other ways....
Just a quick note - in bash, and any real posix shell, it's (generally) better form to use $(command) than `command`, it's more readable and is nestable if needed...
Mr Pashley has a few things to say about backticks, it's worth a quick read if nothing else: http://www.davidpashley.com/blog/programming/shell/backticks.html
Very true (the comments above backticks that is), however:-
At work my development machine is a Solaris system where (on the default shell at least) the $(command) syntax doesn't work. I use a lot of the scripts I develop both at home and at work.
Yeah - but Solaris' /bin/sh isn't POSIX compliant, it's a broken evil thing - I'm fairly sure that they haven't bothered to fix it yet because a lot of people worked round the fact that solaris had a broken /bin/sh by default and so wrote around the broken shell.
I quite agree, but that doesn't change the fact that I have to develop and maintain code on Solaris.
On Wed, Feb 21, 2007 at 01:48:40PM +0000, Eur Ing Chris Green wrote:
On Wed, Feb 21, 2007 at 01:16:51PM +0000, Brett Parker wrote:
Yeah - but Solaris' /bin/sh isn't POSIX compliant, it's a broken evil thing - I'm fairly sure that they haven't bothered to fix it yet because a lot of people worked round the fact that solaris had a broken /bin/sh by default and so wrote around the broken shell.
I quite agree, but that doesn't change the fact that I have to develop and maintain code on Solaris.
Not got bash installed on there for sanity, then? (The solaris boxen we have "laying about", i.e. customer kit that we maintain things on, have all got bash installed - bash is the way forwards for a non-broken shell on slowarsis! Long live bash! Err, or sommit like that :)
On Wed, Feb 21, 2007 at 02:30:38PM +0000, Brett Parker wrote:
On Wed, Feb 21, 2007 at 01:48:40PM +0000, Eur Ing Chris Green wrote:
On Wed, Feb 21, 2007 at 01:16:51PM +0000, Brett Parker wrote:
Yeah - but Solaris' /bin/sh isn't POSIX compliant, it's a broken evil thing - I'm fairly sure that they haven't bothered to fix it yet because a lot of people worked round the fact that solaris had a broken /bin/sh by default and so wrote around the broken shell.
I quite agree, but that doesn't change the fact that I have to develop and maintain code on Solaris.
Not got bash installed on there for sanity, then? (The solaris boxen we have "laying about", i.e. customer kit that we maintain things on, have all got bash installed - bash is the way forwards for a non-broken shell on slowarsis! Long live bash! Err, or sommit like that :)
We don't own the target platforms for our code and they are at customer sites all around the world so we basically have to make sure our code works on a default Solaris (2.8 currently) system.
On 21-Feb-07 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
There's probably other ways....
Regards, Paul.
NB: `ls foo` will not detect filenames in foo that begin with "."
Hence my suggestion using "ls -la"; but this then outputs the "." for current, and ".." for parent directories, regardless of whether "foo" is empty (and also a first line which gives the "total"). Hence the test for "-eq 3".
Ted.
-------------------------------------------------------------------- E-Mail: (Ted Harding) ted.harding@nessie.mcc.ac.uk Fax-to-email: +44 (0)870 094 0861 Date: 21-Feb-07 Time: 13:00:54 ------------------------------ XFMail ------------------------------
On Wed, Feb 21, 2007 at 01:00:57PM -0000, Ted Harding wrote:
On 21-Feb-07 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
There's probably other ways....
Regards, Paul.
NB: `ls foo` will not detect filenames in foo that begin with "."
Hence my suggestion using "ls -la"; but this then outputs the "." for current, and ".." for parent directories, regardless of whether "foo" is empty (and also a first line which gives the "total"). Hence the test for "-eq 3".
Hence my use of ls -lAU, U is for unsorted - and thus is quicker than a default ls, -A means "almost all files" and skips . and .., and as ls gives a nice line containing "total" as the first line of output, it seemed sensible to use that to find out if it contained anything or not :)
Cheers,
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).
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