问题
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