Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to run a cp command which creates a backup of a file and renames it with todays date/time


  • foo.txt
  • foo.txt.2012.
share|improve this question
up vote 11 down vote accepted
cp -p foo.txt foo.txt.`date +%Y.%m.%d.%H.%M.%S`
share|improve this answer
Is there a shorter way to write the date part? I am not bothered how the date is formatted. – John Magnolia Mar 3 '12 at 13:08
This. For elaboration, the backtick () is used in BASH to execute the command within the backticks and echo the response in line. For example, run "echo +%Y.%m.%d.%H.%M.%S`" to see the response. – SimonJGreen Mar 3 '12 at 13:09
Shortest way that is still useful would be date +%s. That will output unix timestamp which will allow you to order by date easily. – SimonJGreen Mar 3 '12 at 13:10
The manual page which describes all the % values is strftime %F will give you an ISO8601 date: 2012-01-31 so you can dispense with the %Y%M%D part. You specify dots (.) above as separators. Unless these are absolutely necessary then I'd use the dashes. See – davey Mar 3 '12 at 15:22
cp -p foo.txt foo.txt.`date -I`

this puts a date in the ISO format yyyy-mm-dd

share|improve this answer
cp foo.txt{,`date +%Y.%m.%d.%H.%M.%S`}
share|improve this answer
Why does every one hate $()? – Aaron Copley May 20 '14 at 19:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.