1

I have files in below format

EnvName.Fullbkp.schema_10022012_0630_Part1.expd
EnvName.Fullbkp.schema_10022012_0630_Part2.expd
EnvName.Fullbkp.schema_10022012_0630_Part3.expd
EnvName.Fullbkp.schema_10022012_0630_Part4.expd

I want to rename this with below files

EnvName.Fullbkp.schema_22052013_1000_Part1.expd
EnvName.Fullbkp.schema_22052013_1000_Part2.expd
EnvName.Fullbkp.schema_22052013_1000_Part3.expd
EnvName.Fullbkp.schema_22052013_1000_Part4.expd

It means I just want to rename the 10022012_0630 with 22052013_1000 what would be the commands and loop to be used to rename all the files in singe go

Shriraj
  • 133
  • 4
  • 11
  • 1
    possible duplicate of [Rename multiple files in Unix](http://stackoverflow.com/questions/1086502/rename-multiple-files-in-unix) – Kevin May 22 '13 at 12:23
  • 1
    [`mmv - move/copy/append/link multiple files by wildcard patterns`](http://manpages.ubuntu.com/manpages/precise/man1/mmv.1.html) – Grijesh Chauhan May 22 '13 at 12:26

3 Answers3

1

This can work:

rename 's/10022012_0630/22052013_1000/' EnvName.Fullbkp.schema_10022012_0630_Part*

Given files with EnvName.Fullbkp.schema_10022012_0630_Part* pattern, it changes 10022012_0630 with 22052013_1000.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
for OLDNAME in EnvName.Fullbkp.schema_10022012_0630_Part*.expd; do
  NEWNAME=`echo "$OLDNAME" | sed 's/10022012_0630/22052013_1000/'`
  mv "$OLDNAME" "$NEWNAME"
done
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • You can even do `NEWNAME=${OLDNAME/10022012_0630/22052013_1000}`. Based on [String Operations](http://tldp.org/LDP/abs/html/refcards.html#AEN22664) – fedorqui May 22 '13 at 12:38
  • I tend to keep my stackoverflow answers more instructive than productive, so I took my time to put every step in a line on its own. – Eugen Rieck May 22 '13 at 12:40
1

A very effecient method, especially if you're dealing with thousands of files is to use bash for the string replacements and find for the lookup. This will avoid many useless forks/execve's and keep the process count down to a minimum:

for F in $(find /your/path/ -type f -name '*10022012_0630*'); do
  mv $F ${F/10022012_0630/22052013_1000};
done
smassey
  • 5,875
  • 24
  • 37