I want to create a directory bar under each of the subdirectories under foo.

If I try to do this with

% mkdir -p /tmp/foo/*/bar

...I get the error

zsh: no matches found: /tmp/foo/*/bar

(In hindsight, I can understand the reason for the error.)

I know that I can solve the original problem with a for-loop, but I'm curious to know if zsh supports some form of parameter expansion that would produce the desired argument for a single invocation of mkdir -p. IOW, a parameter expansion equivalent to "append /bar to every prefix generated by expanding /tmp/foo/*", resulting in

5 Answers
5

This is extended globbing that has a quiet glob flag that uses a glob qualifier to match only directories and a modifier to perform a substitution (using the % pattern character that is only available in history substitution pattern mode) that appends a string to each word.

Wow, I'm impressed. I even found it all in the man page you specify.
– WildcardOct 11 '16 at 10:17

1

Note that the Original Poster specified mkdir -p, which would have the slightly different effect from your command of not throwing errors if some of those directories already have bar subdirectories. Minor point but worth noting here. :)
– WildcardOct 11 '16 at 10:18

1

Another way is to use array expansion via ${^spec} and append bar to each element of whatever /tmp/foo/*(/) expands to: set -- /tmp/foo/*(/) then mkdir -p -- "${^@}/bar"
– don_crisstiOct 11 '16 at 10:21