Wildcard Expansion

Solution 1:

My first thought is you are probably making this problem too complicated. However, it is solvable.

You can do something like this with a crazy bash loop:

for i in /dir/*/file
do
  j=${i%/file}
  k=${j#/dir/}

  cp $i %k-file
done

that uses a couple of bash string operators to extract the number of the source directory.

I guarantee there are waaay better ways to do this, that's just what came to mind.

Solution 2:

zsh% autoload zmv       # should be done for you, noting it just in case
zsh% zmv -C '(*)/file' '$1-file'

zmv is a shell function, often used via alias mmv='noglob zmv -W' for an even easier invocation, but in this case you want the regular usage, plus -C to copy instead of move.


Solution 3:

ls /dir |while read dirname; do cp -v /dir/"$dirname"/file "$dirname"-file ; done

Tags:

Bash

Wildcard