Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Globbing for Empty Directories?



    Hi Aaron :)

 * Aaron Davies <agdavi01@xxxxxxxxxxxxxx> 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/



Messages sorted by: Reverse Date, Date, Thread, Author