0

I have files in unix directory in below format using sed or some other commands.

Owner.Env.File_010513_1200_ver1.expdp
Owner.Env.File_010513_1200_ver2.expdp
Owner.Env.File_010513_1200_ver3.expdp
Owner.Env.File_010513_1200_ver4.expdp

I want to rename these files in below format

Owner.Env.File_100613_2300_ver1.expdp
Owner.Env.File_100613_2300_ver2.expdp
Owner.Env.File_100613_2300_ver3.expdp
Owner.Env.File_100613_2300_ver4.expdp
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Shriraj
  • 1
  • 3

2 Answers2

2
for file in Owner.Env.File_10513_1200_ver*.expdp; do
    mv "$file" "${file/010513_1200/100613_2300}"
done
Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • Hi This is giving me error as ksh[2]: "${file/010513_1200/100613_2300}": 0403-011 The specified substitution is not valid for this command. – Shriraj May 03 '13 at 14:36
  • @Shriraj Chris' solution works in bash. So you may run it as a bash script (copy it into a file with `#!/bin/bash` as first line). I would expect people who use ksh as default shell to be aware what causes such problems. Would make sense to mention in advance that you are a ksh user, too. – Hauke Laging May 03 '13 at 14:52
  • @Shiraj - Your question has the tag "bash-scripting"... – Chris Down May 03 '13 at 15:38
0

If it's not too many (several ten thousand) files then this should do the job (check afterwards with ls Owner.Env.File_010513_1200_ver*.expdp whether files are left and repeat the command if so):

rename File_010513_1200_ File_100613_2300_ Owner.Env.File_010513_1200_ver*.expdp

With GNU an alternative for huge numbers of files (and files with strange names) is:

find . -regex ".*/Owner.Env.File_010513_1200_ver[1-9][0-9]*.expdp" -print0 | \
  xargs -0 rename File_010513_1200_ File_100613_2300_ 
Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
  • it is giving me error as find: 0652-017 -regex is not a valid option. – Shriraj May 03 '13 at 14:45
  • @Shriraj So obviously not GNU find (or maybe one from stone age). Try `-name "Owner.Env.File_010513_1200_ver*.expdp"` instead. – Hauke Laging May 03 '13 at 14:49
  • 1
    You could also use `find`'s `-exec` (or `-execdir`) option and avoid the invocation of `xargs`. Also, some systems (like Debian/Ubuntu and co.) use perl-rename as their `rename` command, which has a perl substitution syntax (`rename 's/replacethis/withthis/'`) and can use perl regex. – evilsoup Jun 28 '13 at 12:03
  • @evilsoup You are right with `rename` but you don't get the `xargs` performance feature with `find`. Or: Show me the working code... :-) – Hauke Laging Jun 28 '13 at 12:19
  • 1
    `find . -name 'blahblah' -exec rename fromthis tothis '{}' +` will feed multiple arguments to the `rename`, if that's what you mean. Using `;` as a terminator for `-exec` will call rename once for each file, while using `+` means that `'{}'` will expand to a list of files, rather than a single file. See the [GNU find documentation](http://www.gnu.org/software/findutils/manual/html_mono/find.html#Multiple-Files) for some more information. I'm not sure whether it's POSIX... but then, neither is your existing solution with `-print0` ;) – evilsoup Jun 28 '13 at 12:32
  • @evilsoup Thanks. And funny: I have been making a mistake with using `find` for a while and misunderstood this mistake... To give something back: `-exec +` is POSIX meanwhile. – Hauke Laging Jun 28 '13 at 12:51