zsh-users
 help / color / mirror / code / Atom feed
* Compound Glob Specifiers
@ 2021-11-08 23:03 Zach Riggle
  2021-11-09  3:04 ` Lawrence Velázquez
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zach Riggle @ 2021-11-08 23:03 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 962 bytes --]

I expect this is a case of "you're holding it wrong", but I figure it's
worth asking what the right way to do this is...

I want to glob the equivalent of */*/somefile (not **/foo, specifically two
directories deep).

However, I do NOT want the glob to match if any of the directory components
are a symlink.

*(/) is the glob specifier for directories [1]

It would then follow that *(/)/*(/)/somefile would match dir/dir/somefile
and not dir/link/somefile.

This is not the case -- even *(/)/ (i.e. appending a trailing slash to all
directories) do not work out-of-the-box as one might expect.

I've read through 14.8 Filename Generation [2] as best I can (my favorite
hack being the NTREF=reffile bit) but haven't found anything that suggests
how one might do this.

[1]
https://zsh.sourceforge.io/Doc/Release/Expansion.html#index-BARE_005fGLOB_005fQUAL_002c-use-of
[2]
https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 1576 bytes --]

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

* Re: Compound Glob Specifiers
  2021-11-08 23:03 Compound Glob Specifiers Zach Riggle
@ 2021-11-09  3:04 ` Lawrence Velázquez
  2021-11-09  3:30 ` Bart Schaefer
  2021-11-09  7:52 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: Lawrence Velázquez @ 2021-11-09  3:04 UTC (permalink / raw)
  To: Zach Riggle; +Cc: zsh-users

On Mon, Nov 8, 2021, at 6:03 PM, Zach Riggle wrote:
> *(/) is the glob specifier for directories [1]
>
> [...]
>
> [1] 
> https://zsh.sourceforge.io/Doc/Release/Expansion.html#index-BARE_005fGLOB_005fQUAL_002c-use-of

From that URL, except immediately above the anchor (emphasis mine):

    Patterns used for filename generation may *end* in a list of
    qualifiers enclosed in parentheses.

> It would then follow that *(/)/*(/)/somefile would match 
> dir/dir/somefile and not dir/link/somefile.
>
> This is not the case -- even *(/)/ (i.e. appending a trailing slash to 
> all directories) do not work out-of-the-box as one might expect.

Since your instances of (...) do not terminate your patterns, they
are interpreted as delimiters for grouping [*].  Another example:

    % touch /tmp/foo
    % print -r -- /*(p)/foo
    /tmp/foo

Clearly (p) is not acting as a glob qualifier here, as /tmp is not
a FIFO.  That pattern is equivalent to /*p/foo.

[*]: https://zsh.sourceforge.io/Doc/Release/Expansion.html#Glob-Operators

> I've read through 14.8 Filename Generation [2] as best I can (my 
> favorite hack being the NTREF=reffile bit) but haven't found anything 
> that suggests how one might do this.

I'll leave that for someone else, as I don't have any suggestions
at the moment.

-- 
vq


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

* Re: Compound Glob Specifiers
  2021-11-08 23:03 Compound Glob Specifiers Zach Riggle
  2021-11-09  3:04 ` Lawrence Velázquez
@ 2021-11-09  3:30 ` Bart Schaefer
  2021-11-09  7:52 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2021-11-09  3:30 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Mon, Nov 8, 2021 at 3:03 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> It would then follow that *(/)/*(/)/somefile would match dir/dir/somefile and not dir/link/somefile.

Lawrence also said this, but since I've already typed it ...

Parenthesized qualifiers can't appear in the middle of a glob.  The
stat() etc. calls that implement the qualifiers are only applied after
the full path name is generated.  Manual in section 14.8.7 says --

  Patterns used for filename generation may end in a list of qualifiers

That means the whole pattern, not individual path segments.  The only
things you can put elsewhere are the flags listed in 14.8.4.

> I want to glob the equivalent of */*/somefile (not **/foo, specifically two directories deep).

If your tree is not excessively deep, you can do it like this (assumes
extendedglob)

% ls **/somefile~*/*/*/*

This says "find all somefile below here, without following symlinks,
and then throw away any names that contain three slashes" (which
includes any that have more than three).

For a tree deep enough to cause **/ to be slow, you have to get
fancier (and this doesn't require extendedglob):

% ls */*/somefile(e^'[[ $REPLY:a = $REPLY:P ]]'^)

The fun thing about that last one is that if you replace "=" with "!="
you can find all the paths that DO have symbolic links.


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

* Re: Compound Glob Specifiers
  2021-11-08 23:03 Compound Glob Specifiers Zach Riggle
  2021-11-09  3:04 ` Lawrence Velázquez
  2021-11-09  3:30 ` Bart Schaefer
@ 2021-11-09  7:52 ` Mikael Magnusson
  2 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2021-11-09  7:52 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On 11/9/21, Zach Riggle <zachriggle@gmail.com> wrote:
> I expect this is a case of "you're holding it wrong", but I figure it's
> worth asking what the right way to do this is...
>
> I want to glob the equivalent of */*/somefile (not **/foo, specifically two
> directories deep).
>
> However, I do NOT want the glob to match if any of the directory components
> are a symlink.
>
> *(/) is the glob specifier for directories [1]
>
> It would then follow that *(/)/*(/)/somefile would match dir/dir/somefile
> and not dir/link/somefile.
>
> This is not the case -- even *(/)/ (i.e. appending a trailing slash to all
> directories) do not work out-of-the-box as one might expect.
>
> I've read through 14.8 Filename Generation [2] as best I can (my favorite
> hack being the NTREF=reffile bit) but haven't found anything that suggests
> how one might do this.

You can do this explicitly by
one=( *(/N) )
two=( $^one/*(/N) )
result=( $^two/somefile(N) )
or more cryptically
() { () { cmd $^@/somefile(N) } $^@/*(/N) } *(/N)
(this will run cmd even if there were no matches)

-- 
Mikael Magnusson


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

end of thread, other threads:[~2021-11-09  7:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-08 23:03 Compound Glob Specifiers Zach Riggle
2021-11-09  3:04 ` Lawrence Velázquez
2021-11-09  3:30 ` Bart Schaefer
2021-11-09  7:52 ` Mikael Magnusson

Code repositories for project(s) associated with this 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).