21

Hello I'm using Putty and trying to rename a file name with current timestamp.

I've used following command to rename the files and according to date

mv abc.log $(date +%F)prod.txt

Above command renames but not able to rename with time, it giving output as

2014-05-12prodabc.log

And following command

abc.log $(date +%y)$(date +%m)$(date +%d)abcprod.log

giving output as

140512abc.log

Actually my requirement is as following:

  • Rename abc.log to abc-<current timestamp>.log
    • e.g abc.log become abc-12-05-2014-17:31.log
  • Then create new file abc.log

Please Help, Thanking you all in advance.

U880D
  • 8,601
  • 6
  • 24
  • 40
PavanBasutkar
  • 237
  • 1
  • 2
  • 5
  • 1
    Possible duplicate of [Linux Rename file with only time/date stamp](http://stackoverflow.com/questions/10639088/linux-rename-file-with-only-time-date-stamp) – kirill_igum May 10 '16 at 15:47

5 Answers5

39

You can use

mv test.dat test_$(date +%d-%m-%Y).dat

If you want to know how you can control your output have a look at the date Manpages..

man date 
Patrick
  • 4,532
  • 2
  • 26
  • 32
14

Use this:

mv abc.log $(date +%F-%H:%M).log && touch abc.log

Here,

+%F-%H:%M will give you a format like 2014-05-19-14:47. If the renaming has done successfully, touch will create a new empty file.

sat
  • 14,589
  • 7
  • 46
  • 65
3

This this:

 str=abc; mv ${str}.log ${str}-$(date +%F'-'%T).log
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51
0

If you are using cPanel to create a cron job: be careful that you need back slash for %. this works : cp log.txt log.date +"\%d\%m\%Y".txt

tao
  • 11
0

in one-liner version

FILE=abc.log ;  BASE=${FILE%%.*} ; EXT=${FILE##*.} ; cp $BASE.$EXT $BASE-$(date +%F'-'%T).$EXT ; > $BASE.$EXT
PaulRM
  • 389
  • 4
  • 13