Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 651
ART Avatar asked Jul 05 '26 05:07

ART


1 Answers

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/'`
like image 149
anubhava Avatar answered Jul 07 '26 05:07

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!