How to copy and add prefix to file names in one step?

a for loop:

for f in *.c; do cp -- "$f" "$OTHERDIR/old#$f"; done

I often add the -v option to cp to allow me to watch the progress.


You can use shell globbing:

for f in *.c; do cp -- "$f" "$OTHERDIR/old#$f"; done

The for variable in GLOB format will expand the glob to all matching files/directories (excluding hidden-ones) and iterate over them, saving each in turn as $variable (in the example above, $f). So, the command I show will iterate over all non-hidden files, copying them and adding the prefix.

Tags:

Bash

Cp

File Copy