Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append the date to a string in bash

I have a script that backups my Raspberry Pi

sudo dd bs=1M if=/dev/sda1 of=/home/pi/backup.img
zip -r /home/pi/backup/backup.zip /home/pi/backup.img
cp backup.zip ~/backup

I want to know how I can append the date to the backup.zip file, generated by the second line.

Any tips?

like image 710
thelearnerofcode Avatar asked Nov 05 '14 22:11

thelearnerofcode


1 Answers

You can use command substitution to accomplish this.
You might also want to familiarize yourself with the date components:

# Save the file name in a variable so we don't repeat ourselves
outfile="/home/pi/backup/backup.zip.$(date +%Y%m%d)"

sudo dd bs=1M if=/dev/sda1 of=/home/pi/backup.img
zip -r "${outfile}" /home/pi/backup.img
cp "${outfile}" ~/backup

The magic here is the $(date +%Y%m%d). This runs date +%Y%m%d and captures the output which will be the current date in YYYYMMDD format.

like image 141
Mr. Llama Avatar answered Oct 28 '22 05:10

Mr. Llama