Renaming Multiple Files in Unix

眉间皱痕 提交于 2021-01-29 04:12:37

问题


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


回答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.




回答2:


for OLDNAME in EnvName.Fullbkp.schema_10022012_0630_Part*.expd; do
  NEWNAME=`echo "$OLDNAME" | sed 's/10022012_0630/22052013_1000/'`
  mv "$OLDNAME" "$NEWNAME"
done



回答3:


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


来源:https://stackoverflow.com/questions/16691608/renaming-multiple-files-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!