tags:

views:

383

answers:

5

I have a set of files in a single directory named:

OneThing-x.extension
OneThing-y.extension
OneThing-z.extension

What UNIX command (find, cp, xargs, sed ?) do I use to COPY these into

AnotherThing-x.extension
AnotherThing-y.extension
AnotherThing-z.extension

such that x ->copy-> x, and y ->copy -> y

I have the find .. part of the command to start with,which selects the files, but I am stuck there.

EDIT Obviously, I want to keep both the original and the copy, so a rename does not quite do the trick for me.

+1  A: 

My rough solution would be something like:

for FIL in OneThing*; do NFIL=`echo $FIL|sed 's/OneThing/AnotherThing/'`; cp "$FIL" "$NFIL"; done;
Jason Musgrove
+1  A: 

Extending Jason Musgrove's solution:

for FIL in OneThing*
do
  NFIL=`echo $FIL | sed 's/OneThing/AnotherThing/'`
  cp "$FIL" "$NFIL"
done
Douglas Leeder
Douglas: Oops, yes - I missed out piping echo $FIL into sed. Good catch.
Jason Musgrove
A: 
$ rename 's/OneThing/AnotherThing/' *.extension
jcadam
Given that the original question asked for a copy, this may not actually produce the required result
Jason Musgrove
+3  A: 

How about:

$ touch a1.a a2.a a3.a
$ ls

a1.a  a2.a  a3.a

$ for a in a*.a ; do cp $a $(echo $a | sed 's/^a/b/') ; done
$ ls

a1.a  a2.a  a3.a  b1.a  b2.a  b3.a
Stephen Darlington
The shortest and most elegant solution offered. And, it worked like a charm!
Felix Ogg
A regular cantrip in my toolbox (though I'm old enough to default to backticks). Sometimes with a find to get the input list. The thing to watch out for is uniqueness in the replacement regexp. Safest to use run it with echo instead of cp the first time.
dmckee
Quote your shell variables, in case you ever use such scripts with filenames (or directory names) that contain spaces. (We won't talk about filenames that start with a '-', cos they're just evil.)
ijw
Real Unix users don't create filenames with spaces or upper case letters(!). But yes, if that's a risk then the variables should be quoted. And, as dmckee suggests, running with echo first is a really good idea. That's exactly how I do it.
Stephen Darlington
A: 

You could also use mmv:

$ mmv -c "OneThing*" "AnotherThing#1"

or

$ mmv -c "OneThing-*.extension" "AnotherThing-#1.extension"
Mark van Lent
$ mmv-bash: mmv: command not found
Felix Ogg
"apt-get install mmv" will solve this if you are using a debian based distribution. I'm sure the RPM bases distributions provide a similar package.
Mark van Lent