Question

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!

Was it helpful?

Solution

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/'`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top