Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

e.g:

  • foo.txt
  • foo.txt.2012.03.03.12.04.06
share|improve this question
add comment

2 Answers

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 foo.txt.date +%Y.%m.%d.%H.%M.%S`" to see the response. –  SimonJGreen Mar 3 '12 at 13:09
4  
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 en.wikipedia.org/wiki/ISO_8601 –  davey Mar 3 '12 at 15:22
add comment
cp -p foo.txt foo.txt.`date -I`

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

share|improve this answer
add comment

Your Answer

 
discard

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.