I know that my issue seems very simple but it took me a day and no luck. I have checked many other posts but none of them solved my problem. I have some files
touch wrfchemi_d01_2010061500:00:00
touch wrfchemi_d01_2010061600:00:00
touch wrfchemi_d01_2010061700:00:00
touch wrfchemi_d01_2010061800:00:00
I want to rename them to:
wrfchemi_d01_2010_06_15_00:00:00 wrfchemi_d01_2010_06_16_00:00:00 wrfchemi_d01_2010_06_17_00:00:00 wrfchemi_d01_2010_06_18_00:00:00
I wrote a simple script
for item in `ls wrfchemi*`
do
year=`echo "$item" | cut -c14-17`
mon=`echo "$item" | cut -c18-19`
day=`echo "$item" | cut -c20-21`
hr=`echo "$item" | cut -c22-23`
dat="wrfchemi_d01_"$year"_"$mon"_"$day"_"$hr":00:00"
echo $dat
mv $item "$dat"
done
It throws out this error
mv: cannot move 'wrfchemi_d01_2010061500:00:00' to 'wrfchemi_d01_2010_06_15_00:00:00': No such file or directory
mv: cannot move 'wrfchemi_d01_2010061600:00:00' to 'wrfchemi_d01_2010_06_16_00:00:00': No such file or directory
mv: cannot move 'wrfchemi_d01_2010061700:00:00' to 'wrfchemi_d01_2010_06_17_00:00:00': No such file or directory
mv: cannot move 'wrfchemi_d01_2010061800:00:00' to 'wrfchemi_d01_2010_06_18_00:00:00': No such file or directory
UPDATE:
SO weird. It also fails in terminal whereas file does exists. It automatically adds "\" to zeros
"mv wrfchemi_d01_2010061500\:00\:00 wrfchemi_d01_2010_06_15_00:00:00"
but it still causes the error
No such file or directory
Thanks
By using rename
:
rename 's/_(\d{4})(\d{2})(\d{2})/_$1_$2_$3_/' wrfchemi_d01_??????????:*
By using bash:
for file in wrfchemi_d01_??????????:*;do
mv "$file" "${file:0:17}_${file:17:2}_${file:19:2}_${file:21}"
done