cp foo.txt {,.backup.`date`}
This expands to something like cp foo.txt .backup.Thu Oct 17 01:02:03 GMT 2013
. The space before the braces starts a new word.
cp foo.txt {,.backup. $((date)) }
The braces are in separate words, so they are interpreted literally. Furthermore, $((…))
is the syntax for arithmetic expansion; the output of date
is nothing like an arithmetic expression. Command substitution uses a single set of parentheses: $(date)
.
cp foo.txt foo.backup.`date`
Closer. You could have expressed this with braces as cp foo.{txt,.backup.`date`}
. There is still the problem that the output of date
contains spaces, so it needs to be put inside double quotes. This would work:
cp foo.{txt,backup."`date`"}
or
cp foo.{txt,backup."$(date)"}
The default output format of date
is not well-suited to a file name, and it might even not work if a locale uses /
characters in the default output format. Use a Y-M-D date format so that the lexicographic order on file names is the chronological order (and also to avoid ambiguity between US and international date formats).
cp foo.{txt,backup."$(date +%Y%m%d-%H%M%S)"}