Sometimes you need to use get the date string as part of your shell command. For example, you want to do a daily dump of your MySQL database as backup. You can do that with a cron job. You want the date string as the file name it dumps to. You can do something like as your cron job:
mysqldump -hlocalhost -uroot mydb > /home/john/db_backup/mydb-`date +%Y%m%d`.sql
There are also times you want to do something for yesterday’s activity. For example, you have a daily log of some user activity, and you would like to run some script in early morning every day to process the yesterday’s log so that by morning you have the reports for yesterday. So here you need to get the date string for yesterday.
In this case, you can use the –date option together with +format. For example:
yesterday=`date –date=”1 days ago” +%Y%m%d`; /home/john/my_report_script.sh -log /var/log/mylog-$yesterday.log






