zsh-users
 help / color / mirror / code / Atom feed
* Finding empty directories
@ 2003-10-23 15:35 DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2003-10-23 15:35 UTC (permalink / raw)
  To: Zsh Users

    Hello all :)

    I want to test if a directory is empty or not and I've thought
that I could test for the expansion of dirname/*(DN[1]). If this is an
empty string, then the directory is empty, otherwise it has at least
one file.

    Anyway I was wondering if is there a better way of doing that
using just zsh code, with no external commands (I know I can do a
'find -type d -empty', for example, or 'ls | wc -l', etc...).

    I would like to test if a filename correspond to a dangling
symlink, too. I can find dangling symlinks using **/*(-@), but if I
just have a file listing, how can I test if a file is a dangling
symlink or not? I've found that doing the following test:

    [[ -h file && ( -r file || -d file ) ]]

    will only return true for a symlink that really points to a file
or a directory, and false otherwise, but I'm not sure if this is a
proper way or if it will fail miserably on some obscure case :? In
fact, I'm not sure if '-r' and '-d' follow symlinks by default :?

    Thanks in advance for your help ;)

    Raúl Núñez de Arenas Coronado

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


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

* Re: Finding empty directories
  2003-10-24 14:44   ` Bart Schaefer
@ 2003-10-25 17:55     ` DervishD
  0 siblings, 0 replies; 5+ messages in thread
From: DervishD @ 2003-10-25 17:55 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> }  * Bart Schaefer <schaefer@brasslantern.com> dixit:
> } > [My ISP had a "latency problem" (their words)
> }     Sorry :((( I've had problems with email, too, and its *very*
> } annoying...
> Even more annoying is that my DSL was unable to renew its DHCP token
> for about 12 straight hours beginning late yesterday afternoon.

    8-Oooo Yes, it's even more annoying :((
 
> } > 	dirname(N-/l2)
> }     I tried that too, but it doesn't work, because under Linux,
> } directories with just files on them seems to have only two links :(
> Oh, of course; silly me.  Yes, that's standard for a unix FS.

    I remember to have read that an empty directory has just two
links in some Unix book in the past :??? 
 
> } > What do you mean by "just have a file listing"?
> }     A generated list of files in a file, for example, which I must
> } examine entry by entry.
>     print ${^$(<filelist)}(N-@)
> would work, I think ...

    Yes, it works, my problem was not that. I process the list
file-per-file, and I thought that you couldn't use glob modifiers if
the filename didn't have any glob metachar in it ;) Now that I know
that I can do it, processing is very easy ;))

    Thanks a lot ;)

    Raúl Núñez de Arenas Coronado

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


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

* Re: Finding empty directories
  2003-10-23 18:16 ` DervishD
@ 2003-10-24 14:44   ` Bart Schaefer
  2003-10-25 17:55     ` DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-10-24 14:44 UTC (permalink / raw)
  To: zsh-users

On Oct 23,  8:16pm, DervishD wrote:
}
}  * Bart Schaefer <schaefer@brasslantern.com> dixit:
} > [My ISP had a "latency problem" (their words)
} 
}     Sorry :((( I've had problems with email, too, and its *very*
} annoying...

Even more annoying is that my DSL was unable to renew its DHCP token
for about 12 straight hours beginning late yesterday afternoon.

} > 	dirname(N-/l2)
} 
}     I tried that too, but it doesn't work, because under Linux,
} directories with just files on them seems to have only two links :(

Oh, of course; silly me.  Yes, that's standard for a unix FS.

} > What do you mean by "just have a file listing"?
} 
}     A generated list of files in a file, for example, which I must
} examine entry by entry.

    print ${^$(<filelist)}(N-@)
    
would work, I think ...


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

* Re: Finding empty directories
  2003-10-23 17:40 Bart Schaefer
@ 2003-10-23 18:16 ` DervishD
  2003-10-24 14:44   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: DervishD @ 2003-10-23 18:16 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

    Hi Bart :)

 * Bart Schaefer <schaefer@brasslantern.com> dixit:
> [My ISP had a "latency problem" (their words)

    Sorry :((( I've had problems with email, too, and its *very*
annoying...

> >     I want to test if a directory is empty or not and I've thought
> > that I could test for the expansion of dirname/*(DN[1]).
> >     Anyway I was wondering if is there a better way of doing that
> 	dirname(N-/l2)

    I tried that too, but it doesn't work, because under Linux,
directories with just files on them seems to have only two links :(
Don't know if this is a problem of the filesystem (ext3) or if this
is the standard Unix behaviour...

    To my knowledge, and empty dir has only two hardlinks: the '.'
and the '..' entries :?? But anyway, using stat on empty dirs and on
dirs with just files in it, gives me 'nlink' as '2' :((
 
> In some older versions of zsh you may need to do something funky to get
> the parenthesized part to be interpreted as glob qualifiers, because in
> those versions qualifiers are interpreted only when a metacharacter is
> part of the pattern.

    I'm using 4.0.7 right now, so this is not a problem.
 
> >     I would like to test if a filename correspond to a dangling
> > symlink, too. I can find dangling symlinks using **/*(-@), but if I
> > just have a file listing, how can I test if a file is a dangling
> > symlink or not?
> What do you mean by "just have a file listing"?  Except as noted above for
> older versions, you can always append a glob qual, as in file(N-@).

    A generated list of files in a file, for example, which I must
examine entry by entry. Obviously I didn't know that you can append a
glob qual even if no glob metachar is present O:)) I did a quick and
dirty test that failed (a typo, obviously) and I thought that it
couldn't be done at all. My fault, sorry O:)))

> > I've found that doing the following test:
> >     [[ -h file && ( -r file || -d file ) ]]
> Yes, -r and -d use stat(2) rather than lstat(2), but you probably want
> 	[[ -h file && ( -f file || -d file ) ]]

    Yes, true, just a typo O:))
 
> You could also do something like
> 	zmodload zsh/stat
> 	{ stat +link bar && ! stat +nlink bar 2>/dev/null } >/dev/null

    Yes, I know, but I wanted to avoid depending on zsh/stat module,
although it is not an issue.

    Thanks a lot :)))

    Raúl Núñez de Arenas Coronado

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


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

* Re: Finding empty directories
@ 2003-10-23 17:40 Bart Schaefer
  2003-10-23 18:16 ` DervishD
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-10-23 17:40 UTC (permalink / raw)
  To: zsh-users

[My ISP had a "latency problem" (their words) with mail servers from late
yesterday evening (PST) until a few minutes ago this morning.  I may have
lost some mail as a result, or it may be sitting in queues at the sending
machines waiting to be delivered.  I'm responding to this one by cutting
and pasting from the archives.  Oh -- there's the original, just as I was
about to send ...]

>     I want to test if a directory is empty or not and I've thought
> that I could test for the expansion of dirname/*(DN[1]).
> 
>     Anyway I was wondering if is there a better way of doing that

	dirname(N-/l2)

(That's "ell two" as in "link count is two".)  I suppose if you already
know it is a directory, the (/) is redundant.

In some older versions of zsh you may need to do something funky to get
the parenthesized part to be interpreted as glob qualifiers, because in
those versions qualifiers are interpreted only when a metacharacter is
part of the pattern.

>     I would like to test if a filename correspond to a dangling
> symlink, too. I can find dangling symlinks using **/*(-@), but if I
> just have a file listing, how can I test if a file is a dangling
> symlink or not?

What do you mean by "just have a file listing"?  Except as noted above for
older versions, you can always append a glob qual, as in file(N-@).

> I've found that doing the following test:
> 
>     [[ -h file && ( -r file || -d file ) ]]
> 
>     will only return true for a symlink that really points to a file
> or a directory, and false otherwise, but I'm not sure if this is a
> proper way or if it will fail miserably on some obscure case :? In
> fact, I'm not sure if '-r' and '-d' follow symlinks by default :?

Yes, -r and -d use stat(2) rather than lstat(2), but you probably want

	[[ -h file && ( -f file || -d file ) ]]

You could also do something like

	zmodload zsh/stat
	{ stat +link bar && ! stat +nlink bar 2>/dev/null } >/dev/null


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

end of thread, other threads:[~2003-10-26 10:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-23 15:35 Finding empty directories DervishD
2003-10-23 17:40 Bart Schaefer
2003-10-23 18:16 ` DervishD
2003-10-24 14:44   ` Bart Schaefer
2003-10-25 17:55     ` 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).