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

Re: Globbing for Empty Directories?



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.



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