zsh-users
 help / color / mirror / code / Atom feed
* Globbing for Empty Directories?
@ 2004-03-28  7:00 Aaron Davies
  2004-03-28 19:21 ` Bart Schaefer
  2004-03-28 19:49 ` DervishD
  0 siblings, 2 replies; 16+ messages in thread
From: Aaron Davies @ 2004-03-28  7:00 UTC (permalink / raw)
  To: zsh-users

Is there a way to get empty directories from a glob pattern? '*(L0)' 
finds empty files, but doesn't work for dirs. I'm looking for something 
the equivalent of find's -empty argument, since I hate find with a 
passion and would love to never use it again.
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28  7:00 Globbing for Empty Directories? Aaron Davies
@ 2004-03-28 19:21 ` Bart Schaefer
  2004-03-28 20:13   ` Aaron Davies
  2004-03-28 19:49 ` DervishD
  1 sibling, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2004-03-28 19:21 UTC (permalink / raw)
  To: zsh-users

On Mar 28,  2:00am, Aaron Davies wrote:
}
} Is there a way to get empty directories from a glob pattern?

There's some discusson of this in the archives from a few months ago.

It's not possible to tell if a directory is empty without actually looking
for files in it.  So the best you can do is something like this:

	isempty() {
	  reply=( $REPLY/*(DN) )
	  (( $#reply )) && unset reply || reply=( $REPLY )
	}

	print /path/to/emptydirectory(/e{isempty})

The (/) is important, otherwise you need to test [[ -d $REPLY ]] in the
isempty function (because as-is it matches files as well as empty dirs).
If you want to follow symlinks to empty directories you need:

	print /path/to/emptydirectory(-/e{isempty})

It's much easier to get only NON-empty directories:

	print /path/to/nonempty/*(DN[-1]:h)

In this case, though, symlinks are always followed because of appending
"/*" to the directory name, and there's not much to be done about it.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28  7:00 Globbing for Empty Directories? Aaron Davies
  2004-03-28 19:21 ` Bart Schaefer
@ 2004-03-28 19:49 ` DervishD
  2004-03-29  6:03   ` Aaron Davies
  1 sibling, 1 reply; 16+ messages in thread
From: DervishD @ 2004-03-28 19:49 UTC (permalink / raw)
  To: Aaron Davies; +Cc: zsh-users

    Hi Aaron :)

 * Aaron Davies <agdavi01@louisville.edu> dixit:

> Is there a way to get empty directories from a glob pattern? '*(L0)' 
> finds empty files, but doesn't work for dirs. I'm looking for something 
> the equivalent of find's -empty argument, since I hate find with a 
> passion and would love to never use it again.

    There is not, AFAIK. See the mailing list archives, I asked more
or less the same a few months ago. Testing for the number of links
won't work for directories :(( The only way is to look whether the
directory has files in it, with some globbing pattern; if the
expansion is empty, then the directory is empty.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28 19:21 ` Bart Schaefer
@ 2004-03-28 20:13   ` Aaron Davies
  2004-03-28 20:55     ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Aaron Davies @ 2004-03-28 20:13 UTC (permalink / raw)
  To: zsh-users

On Sunday, March 28, 2004, at 02:21 PM, Bart Schaefer wrote:

> On Mar 28,  2:00am, Aaron Davies wrote:
> }
> } Is there a way to get empty directories from a glob pattern?
>
> There's some discusson of this in the archives from a few months ago.
>
> It's not possible to tell if a directory is empty without actually 
> looking
> for files in it.  So the best you can do is something like this:
>
> 	isempty() {
> 	  reply=( $REPLY/*(DN) )
> 	  (( $#reply )) && unset reply || reply=( $REPLY )
> 	}
>
> 	print /path/to/emptydirectory(/e{isempty})
>
> The (/) is important, otherwise you need to test [[ -d $REPLY ]] in the
> isempty function (because as-is it matches files as well as empty 
> dirs).
> If you want to follow symlinks to empty directories you need:
>
> 	print /path/to/emptydirectory(-/e{isempty})
>
> It's much easier to get only NON-empty directories:
>
> 	print /path/to/nonempty/*(DN[-1]:h)
>
> In this case, though, symlinks are always followed because of appending
> "/*" to the directory name, and there's not much to be done about it.

Thanks much. In that case, could I make this a feature request?
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28 20:13   ` Aaron Davies
@ 2004-03-28 20:55     ` Bart Schaefer
  2004-03-28 23:55       ` Aaron Davies
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2004-03-28 20:55 UTC (permalink / raw)
  To: zsh-users

On Mar 28,  3:13pm, Aaron Davies wrote:
} Subject: Re: Globbing for Empty Directories?
}
} On Sunday, March 28, 2004, at 02:21 PM, Bart Schaefer wrote:
} 
} > It's not possible to tell if a directory is empty without actually 
} > looking for files in it.
} 
} Thanks much. In that case, could I make this a feature request?

What I meant was, the filesystem typically doesn't provide any way to
discover whether a directory is empty without looking for files in it.

So even if zsh implemented an "empty directory" globbing flag internally,
it'd be doing effectively the same thing as the function I showed.  And
that must be what "find ... -empty" is doing as well.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28 20:55     ` Bart Schaefer
@ 2004-03-28 23:55       ` Aaron Davies
  2004-03-29  0:54         ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Aaron Davies @ 2004-03-28 23:55 UTC (permalink / raw)
  To: zsh-users

On Sunday, March 28, 2004, at 03:55 PM, Bart Schaefer wrote:

> On Mar 28,  3:13pm, Aaron Davies wrote:
> } Subject: Re: Globbing for Empty Directories?
> }
> } On Sunday, March 28, 2004, at 02:21 PM, Bart Schaefer wrote:
> }
> } > It's not possible to tell if a directory is empty without actually
> } > looking for files in it.
> }
> } Thanks much. In that case, could I make this a feature request?
>
> What I meant was, the filesystem typically doesn't provide any way to
> discover whether a directory is empty without looking for files in it.
>
> So even if zsh implemented an "empty directory" globbing flag 
> internally,
> it'd be doing effectively the same thing as the function I showed.  And
> that must be what "find ... -empty" is doing as well.

Alright then. It'd still be a nice thing to have in zsh. Is it a policy 
that globbing be limited to things the filesystem can tell you directly?
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28 23:55       ` Aaron Davies
@ 2004-03-29  0:54         ` Bart Schaefer
  2004-03-29  6:05           ` Aaron Davies
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2004-03-29  0:54 UTC (permalink / raw)
  To: zsh-users

On Mar 28,  6:55pm, Aaron Davies wrote:
}
} > So even if zsh implemented an "empty directory" globbing flag
} > internally, it'd be doing effectively the same thing as the function
} > I showed.
} 
} Alright then. It'd still be a nice thing to have in zsh. Is it a policy 
} that globbing be limited to things the filesystem can tell you directly?

I wouldn't say its "a policy", but so far it is a truism, AFAIK.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-28 19:49 ` DervishD
@ 2004-03-29  6:03   ` Aaron Davies
  2004-03-29 13:28     ` DervishD
  0 siblings, 1 reply; 16+ messages in thread
From: Aaron Davies @ 2004-03-29  6:03 UTC (permalink / raw)
  To: DervishD; +Cc: Aaron Davies, zsh-users

On Sunday, March 28, 2004, at 02:49 PM, DervishD wrote:

>     Hi Aaron :)
>
>  * Aaron Davies <agdavi01@louisville.edu> dixit:
>
>> Is there a way to get empty directories from a glob pattern? '*(L0)'
>> finds empty files, but doesn't work for dirs. I'm looking for 
>> something
>> the equivalent of find's -empty argument, since I hate find with a
>> passion and would love to never use it again.
>
>     There is not, AFAIK. See the mailing list archives, I asked more
> or less the same a few months ago. Testing for the number of links
> won't work for directories :((

Actually, I'm not sure of that. According to the info page for find, a 
directory will always have two hard links, itself and it's "." pointer. 
Adding any subdirectories or files to it seems to increase the number 
of links it has. So wouldn't *(/l2) find empty directories? Or is this 
hard-link policy not true on all systems?
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29  0:54         ` Bart Schaefer
@ 2004-03-29  6:05           ` Aaron Davies
  0 siblings, 0 replies; 16+ messages in thread
From: Aaron Davies @ 2004-03-29  6:05 UTC (permalink / raw)
  To: zsh-users

On Sunday, March 28, 2004, at 07:54 PM, Bart Schaefer wrote:

> On Mar 28,  6:55pm, Aaron Davies wrote:
> }
> } > So even if zsh implemented an "empty directory" globbing flag
> } > internally, it'd be doing effectively the same thing as the 
> function
> } > I showed.
> }
> } Alright then. It'd still be a nice thing to have in zsh. Is it a 
> policy
> } that globbing be limited to things the filesystem can tell you 
> directly?
>
> I wouldn't say its "a policy", but so far it is a truism, AFAIK.

Well, it might not be a policy either, but it seems to me that a worthy 
goal (and one nearly reached) for zsh's globbing system would be to 
completely obsolete find. On cursory examination, finding empty 
directories seems to be the only thing missing. Is there any particular 
reason it shouldn't be added?
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29  6:03   ` Aaron Davies
@ 2004-03-29 13:28     ` DervishD
  2004-03-29 15:19       ` Danek Duvall
  2004-03-29 23:11       ` Aaron Davies
  0 siblings, 2 replies; 16+ messages in thread
From: DervishD @ 2004-03-29 13:28 UTC (permalink / raw)
  To: Aaron Davies; +Cc: Aaron Davies, zsh-users

    Hi Aaron :)

 * Aaron Davies <agdavi01@louisville.edu> dixit:
> >>Is there a way to get empty directories from a glob pattern?
> >>'*(L0)' finds empty files, but doesn't work for dirs. I'm looking
> >>for something the equivalent of find's -empty argument, since I
> >>hate find with a passion and would love to never use it again.
> >    There is not, AFAIK. See the mailing list archives, I asked more
> >or less the same a few months ago. Testing for the number of links
> >won't work for directories :((
> Actually, I'm not sure of that. According to the info page for find, a 
> directory will always have two hard links, itself and it's "." pointer.

    That's true.
 
> Adding any subdirectories or files to it seems to increase the number 
> of links it has.

    Not under Linux, at least, although it may depend on the
filesystem type. For ext3, only subdirs increase the number of links
of a directory.

> So wouldn't *(/l2) find empty directories?

    It will find directories without subdirectories.

> Or is this hard-link policy not true on all systems?

    I have not tested in many systems, but Linux don't do it, and
I've not found any standard that require files to be links on the
directory. If you find any, please tell me to report the current
behaviour as a bug to Linux kernel developers, but looking at
findutils sources (that being GNU are intended to be very portable),
you can see the following:

{
  if (S_ISDIR (stat_buf->st_mode))
    {
      DIR *d;
      struct dirent *dp;
      boolean empty = true;

      errno = 0;
      d = opendir (rel_pathname);
      if (d == NULL)
	{
	  error (0, errno, "%s", pathname);
	  exit_status = 1;
	  return (false);
	}
      for (dp = readdir (d); dp; dp = readdir (d))
	{
	  if (dp->d_name[0] != '.'
	      || (dp->d_name[1] != '\0'
		  && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
	    {
	      empty = false;
	      break;
	    }
	}
      if (CLOSEDIR (d))
	{
	  error (0, errno, "%s", pathname);
	  exit_status = 1;
	  return (false);
	}
      return (empty);
    }
  else if (S_ISREG (stat_buf->st_mode))
    return (stat_buf->st_size == 0);
  else
    return (false);
}

    This is the code for '-empty', and as you can see, it examines
the files in the directory, not the number of links it has.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29 13:28     ` DervishD
@ 2004-03-29 15:19       ` Danek Duvall
  2004-03-29 15:57         ` DervishD
  2004-03-29 23:11       ` Aaron Davies
  1 sibling, 1 reply; 16+ messages in thread
From: Danek Duvall @ 2004-03-29 15:19 UTC (permalink / raw)
  To: Aaron Davies; +Cc: zsh-users

On Mon, Mar 29, 2004 at 03:28:00PM +0200, DervishD wrote:

> > Adding any subdirectories or files to it seems to increase the
> > number of links it has.
> 
>     Not under Linux, at least, although it may depend on the
> filesystem type. For ext3, only subdirs increase the number of links
> of a directory.

As it should be.  Think of it this way.  st_nlink is the number of names
a filesystem object has on a filesystem.  For any directory, that'll be
1) the name the directory has in its parent, 2) the name "." it contains
itself, and 3-nlink) the name ".." its children contain.

Non-directory files (generally) don't contain other files, so they won't 
add to st_nlink.

Danek


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29 15:19       ` Danek Duvall
@ 2004-03-29 15:57         ` DervishD
  0 siblings, 0 replies; 16+ messages in thread
From: DervishD @ 2004-03-29 15:57 UTC (permalink / raw)
  To: Danek Duvall, Aaron Davies, zsh-users

    Hi Danek :)

 * Danek Duvall <duvall@emufarm.org> dixit:
> > > Adding any subdirectories or files to it seems to increase the
> > > number of links it has.
> >     Not under Linux, at least, although it may depend on the
> > filesystem type. For ext3, only subdirs increase the number of links
> > of a directory.
> As it should be.  Think of it this way.  st_nlink is the number of names
> a filesystem object has on a filesystem.

    I'm with you, it's quite logical, but unfortunately people tends
to think that, since a filename is 'linked' to its container
directory, that should affect st_nlink (which is not true, this can
be seen even in the GNU libc documentation). I was not sure if the
standard (POSIX or SuS) specified the opposite.

    Summarizing: Zsh should follow 'find' example and implement tests
for empty dirs investigating its contents, but since this can be
easily done in, let's say, 'user space' (I mean, outside the shell,
using shell commands), I see no need for it. I would like to have a
globbing flag for empty dirs, because it is simpler, better
integrated with the rest of globbin syntax and less error prone, so I
vote for including it O:)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29 13:28     ` DervishD
  2004-03-29 15:19       ` Danek Duvall
@ 2004-03-29 23:11       ` Aaron Davies
  2004-03-29 23:24         ` DervishD
  1 sibling, 1 reply; 16+ messages in thread
From: Aaron Davies @ 2004-03-29 23:11 UTC (permalink / raw)
  To: zsh-users

On Monday, March 29, 2004, at 08:28 AM, DervishD wrote:

> Aaron Davies <agdavi01@louisville.edu> dixit:
>> Adding any subdirectories or files to it seems to increase the number
>> of links it has.
>
>     Not under Linux, at least, although it may depend on the
> filesystem type. For ext3, only subdirs increase the number of links
> of a directory.
>
>> Or is this hard-link policy not true on all systems?
>
>     I have not tested in many systems, but Linux don't do it, and
> I've not found any standard that require files to be links on the
> directory. If you find any, please tell me to report the current
> behaviour as a bug to Linux kernel developers, but looking at
> findutils sources (that being GNU are intended to be very portable),
> you can see the following:

I'm on OS X, so it may be a peculiarity of HFS+.
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29 23:11       ` Aaron Davies
@ 2004-03-29 23:24         ` DervishD
  2004-03-31  1:44           ` Aaron Davies
  0 siblings, 1 reply; 16+ messages in thread
From: DervishD @ 2004-03-29 23:24 UTC (permalink / raw)
  To: Aaron Davies; +Cc: zsh-users

    Hi Aaron :)

 * Aaron Davies <agdavi01@louisville.edu> dixit:
> >    I have not tested in many systems, but Linux don't do it, and
> >I've not found any standard that require files to be links on the
> >directory. If you find any, please tell me to report the current
> >behaviour as a bug to Linux kernel developers, but looking at
> >findutils sources (that being GNU are intended to be very portable),
> >you can see the following:
> I'm on OS X, so it may be a peculiarity of HFS+.

    Do you mean you can know if a dir is empty or not in OS X looking
for the number of st_nlinks it has? I must confess I don't know a
workd about HFS+ O:)

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-29 23:24         ` DervishD
@ 2004-03-31  1:44           ` Aaron Davies
  2004-03-31  8:44             ` DervishD
  0 siblings, 1 reply; 16+ messages in thread
From: Aaron Davies @ 2004-03-31  1:44 UTC (permalink / raw)
  To: zsh-users

On Monday, March 29, 2004, at 06:24 PM, DervishD wrote:

> Aaron Davies <agdavi01@louisville.edu> dixit:
>
>>>    I have not tested in many systems, but Linux don't do it, and
>>> I've not found any standard that require files to be links on the
>>> directory. If you find any, please tell me to report the current
>>> behaviour as a bug to Linux kernel developers, but looking at
>>> findutils sources (that being GNU are intended to be very portable),
>>> you can see the following:
>> I'm on OS X, so it may be a peculiarity of HFS+.
>
>     Do you mean you can know if a dir is empty or not in OS X looking
> for the number of st_nlinks it has? I must confess I don't know a
> workd about HFS+ O:)

On cursory examination, it would appear so. I haven't done any serious 
testing (or looked at any docs), but the number of links listed in ls 
-l for a directory seems to equal the number of files in it plus two.
-- 
     __                        __
    /  )                      /  )
   /--/ __.  __  ________    /  / __. , __o  _  _
  /  (_(_/|_/ (_(_) / / <_  /__/_(_/|_\/ <__</_/_)_


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: Globbing for Empty Directories?
  2004-03-31  1:44           ` Aaron Davies
@ 2004-03-31  8:44             ` DervishD
  0 siblings, 0 replies; 16+ messages in thread
From: DervishD @ 2004-03-31  8:44 UTC (permalink / raw)
  To: Aaron Davies; +Cc: zsh-users

    Hi Aaron :)

 * Aaron Davies <agdavi01@louisville.edu> dixit:
> >>I'm on OS X, so it may be a peculiarity of HFS+.
> >    Do you mean you can know if a dir is empty or not in OS X looking
> >for the number of st_nlinks it has? I must confess I don't know a
> >workd about HFS+ O:)
> On cursory examination, it would appear so. I haven't done any serious 
> testing (or looked at any docs), but the number of links listed in ls 
> -l for a directory seems to equal the number of files in it plus two.

    Well, that seems an exception. I'll try to search the Internet
for seeing current practice. The fact that 'find' uses directory
contents and not the number of links is interesting, though.

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2004-03-31  8:46 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-28  7:00 Globbing for Empty Directories? Aaron Davies
2004-03-28 19:21 ` Bart Schaefer
2004-03-28 20:13   ` Aaron Davies
2004-03-28 20:55     ` Bart Schaefer
2004-03-28 23:55       ` Aaron Davies
2004-03-29  0:54         ` Bart Schaefer
2004-03-29  6:05           ` Aaron Davies
2004-03-28 19:49 ` DervishD
2004-03-29  6:03   ` Aaron Davies
2004-03-29 13:28     ` DervishD
2004-03-29 15:19       ` Danek Duvall
2004-03-29 15:57         ` DervishD
2004-03-29 23:11       ` Aaron Davies
2004-03-29 23:24         ` DervishD
2004-03-31  1:44           ` Aaron Davies
2004-03-31  8:44             ` DervishD

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).