简体   繁体   中英

Renaming part for file using mv command in shell script

i want to rename a file using mv command in shell script
now file is in the format foo-<date>.tar.gz i want to rename it to foo1-<date>.tar.gz.
i tried, cut the foo and rename it and concatenate and all but i want to something very simple follows
mv foo*.tar.gz foo1*.tar.gz
date should be maintained only foo should be changed foo1 is it possible ? if yes how?

Thank you in advance!

You can use BASH string manipulation:

f='foo-12APR2014.tar.gz'
nf="foo1-${f#*-}"

Test:

echo "$nf"
foo1-12APR2014.tar.gz

PS: If not using BASH then you can use sed:

nf=`echo "$f"|sed 's/^foo/foo1/'`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM