1

I have over a thousand files of similar names in a directory and wish to do a rename. The files are of this format

GW_LGMS01-50160306185154-01375272.CDR
GW_LGMS01-50160306237154-01375272.CDR.00001    
GW_LGMS02-50160306133554-02308872.CDR
GW_LGMS02-50160306137554-02308872.CDR.00014
GW_LGMS03-50160306221836-02217475.CDR.00001
GW_LGMS03-50160306235132-02217475.CDR

I want to do a rename on all of them at once to append a 0- before 50160306 on all of them. That is,

GW_LGMS01-0-50160306185154-01375272.CDR
GW_LGMS01-0-50160306237154-01375272.CDR.00001    
GW_LGMS02-0-50160306133554-02308872.CDR
GW_LGMS02-0-50160306137554-02308872.CDR.00014
GW_LGMS03-0-50160306221836-02217475.CDR.00001
GW_LGMS03-0-50160306235132-02217475.CDR

50160306 is what all the files have in common.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
oak
  • 35
  • 5

3 Answers3

0

Assuming that -50160306 is unique in the file names, and that you are using a shell that understands ${parameter/pattern/string} (Bash, KornShell, etc.):

for f in *.CDR*; do
  echo mv "$f" "${f/-50160306/-0-50160306}"
done

Do this with the echo in place to see what would happen, then remove the echo when you are sure it does the right thing.

If you are afraid to mess up, just put the files with the new names in a new folder:

mkdir renamed

for f in *.CDR*; do
  cp "$f" renamed/"${f/-50160306/-0-50160306}"
done
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • I'm speechless. Words are not enough. Thanks so much. it did the exact thing I wanted. But If my understanding is correct, the `echo` displays the outcome of the rename? – oak Jun 19 '16 at 09:21
  • I was indeed afraid but I had to just summon the courage. You might have guessed right, I'm a "newbie". I also noticed that the rename over-wrote the old name. hope the contents won't be affected? – oak Jun 19 '16 at 09:24
  • @oak Removing the `echo` from the first example loop will move the file to its new name (i.e. "rename the file"). Otherwise it will just show what command would have been executed. – Kusalananda Jun 19 '16 at 09:29
  • 1
    I asked because when I used `echo`, it displays a line my line rename of the real file but it didn't replace them so I edited the script and removed `echo` like you suggested. It did the rename and replaced the old filename with the new file name. Much appreciated. – oak Jun 19 '16 at 09:37
  • 1
    I want to ask another question(new thread) based on the reply you gave me. Your reply will be much appreciated. – oak Jun 19 '16 at 09:54
  • @oak Do ask. I will be away for a bit, but will get to it when I'm back. – Kusalananda Jun 19 '16 at 09:55
0

If you don't use bash:

#!/bin/sh
for i in * ; do
    mv "$i" "$(printf '%s' "$i" | sed 's/\(50160306.*\)/0-\1/')"
done
MichalH
  • 1,062
  • 9
  • 20
0

There are two rename tools floating around: one is part of the util-linux package, the other is Perl based (see this answer for details). To find out which one you have, check at the end of the man page (man rename).

With the util-linux version, you can rename your files as follows:

rename 50160306 0-50160306 *

and for the Perl based version, it would be (untested!)

rename 's/50160306/0-$&/' *

Be aware that there are no safeguards with these commands – test them on a small sample before you use them.

Community
  • 1
  • 1
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116