I need to make a backup of a file, and I would like to have a timestamp as part of the name to make it easier to differentiate.
How would you inject the current date into a copy command?
[root@mongo-test3 ~]# cp foo.txt {,.backup.`date`}
cp: target `2013}' is not a directory
[root@mongo-test3 ~]# cp foo.txt {,.backup. $((date)) }
cp: target `}' is not a directory
[root@mongo-test3 ~]# cp foo.txt foo.backup.`date`
cp: target `2013' is not a directory
Best Answer
This isn't working because the command
date
returns a string with spaces in it.If you truly want filenames like that you'll need to wrap that string in quotes.
You're probably thinking of a different string to be appended would be my guess though. I usually use something like this:
See the man page for date for more formatting codes around the output for the date & time.
Additional formats
If you want to take full control if you consult the man page you can do things like this:
NOTE: You can use
date -I
ordate --iso-8601
which will produce identical output todate +"%Y-%m-%d
. This switch also has the ability to take an argument to indicate various time formats:Examples: