Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zsh glob qualifiers in middle of path

Tags:

zsh

Suppose I have the following directory structure:

$ mkdir -p a/1
$ ln -s a b

Globbing for directories, I get the directory within the symlink too:

$ print -l */*(/)
a/1
b/1

How can I restrict the globbing for the first directory level to directories only, excluding symlinks? The obvious doesn't work:

$ print -l *(/)/*(/)
zsh: bad pattern: *(/)/*(/)

More generally, how can I specify glob qualifiers for intermediate path components? In spirit:

$ print -l a(...)/b(...)/c(...)/d(...)/e(...)/f(...)

where (...) denotes glob qualifiers for the respective path components.

like image 841
Powerfool Avatar asked Nov 24 '14 10:11

Powerfool


1 Answers

If you don't mind a itself appearing in your list, then this should do the trick:

# ** doesn't follow symlinks.
print -l **/*(/)

If you want only subdirs, then you'll need to use two patterns, instead of one:

# Make an array with all dirs that are in the pwd.
tmp=( *(/) )

# $^tmp applies brace expansion on the array.
# (N) ignores 'file not found' errors, in case tmp contains dirs w/out children.
print -l $^tmp/*(/N)

This way, you can also have different glob qualifiers for each part in your path, but you'll have to make a separate parameter for each.

like image 110
Marlon Richert Avatar answered Oct 05 '22 23:10

Marlon Richert