get the current date and use it in a filename
I'm trying to do something like this:
mysqldump --user c1bt3 --password=blah c1bt3开发者_如何学Python > c1bt{date}.sql
where date is replaced with the current date, i.e. c1bt5-11-10.sql, and I'm trying to do it from a linux shell script.
Any ideas how I can do this?
You can either use the date command with your favourite formating
DATE=$(date)
mysqldump --user c1bt3 --password=blah c1bt3 > c1bt${DATE}.sql
or use the date formating capailities of your shell, which can vary a bit.
This is ZSH:
$ print -P "%D{%H:%M:%S}"
22:30:23
Same usage...
DATE=$(print -P "%D{%H:%M:%S}")
mysqldump --user c1bt3 --password=blah c1bt3 > c1bt${DATE}.sql
e.g.
date +%Y%m%d-%H%M
A fast way is this, which will give the date as yyy-mm-dd:
mysqldump --user c1bt3 --password=blah c1bt3 > c1bt$(date -I).sql
If the ordering of the date parts is important, try this:
mysqldump --user c1bt3 --password=blah c1bt3 > c1bt$(date +%d-%m-%y).sql
(I'm a big fan of Bash and am not very fluent in other shells, so my answer should be treated as Bash-only)
精彩评论