Linux cp with regex

I would like to copy some files to a directory, rename files, but save the extension. Is this possible with a simple cp, using regular expression?

For instance:

cp ^myfile\.(.*) mydir/newname.$1

So I could copy the file, saving the extension, but renaming it. Is there a way to get consistent elements in a regular expression cpto use it in a team? If not, I will make a perl script, I think, or if you have another way ...

thank

+5
source share
2 answers

Suppose that you have myfile.a, myfile.b, myfile.c:

for i in myfile.*; do echo mv "$i" "${i/myfile./newname.}"; done

( echo) newname.a, newname.b, newname.c.

+14

; .

for a in myfile.* ; do
  b=`echo $a | sed 's!^myfile!mydir/newname!'`
  cp $a $b
done

perl script , .

+1

All Articles