This question already has an answer here:

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


I want to rename these files in below format


marked as duplicate by manatwork, Patrick, slm, Shadur, Thor May 3 '13 at 13:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

for file in Owner.Env.File_10513_1200_ver*.expdp; do
    mv "$file" "${file/010513_1200/100613_2300}"
  • 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 3 '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 3 '13 at 14:52
  • @Shiraj - Your question has the tag "bash-scripting"... – Chris Down May 3 '13 at 15:38

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_ 
  • it is giving me error as find: 0652-017 -regex is not a valid option. – Shriraj May 3 '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 3 '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 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.