PavanBasutkar
PavanBasutkar

Reputation: 237

Rename file command in Unix with timestamp

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:

Please Help, Thanking you all in advance.

Upvotes: 21

Views: 76560

Answers (5)

PaulRM
PaulRM

Reputation: 409

in one-liner version

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

Upvotes: 0

tao
tao

Reputation: 11

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

Upvotes: 0

sat
sat

Reputation: 14949

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.

Upvotes: 14

Tiago Lopo
Tiago Lopo

Reputation: 7959

This this:

 str=abc; mv ${str}.log ${str}-$(date +%F'-'%T).log

Upvotes: 3

Patrick
Patrick

Reputation: 4562

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 

Upvotes: 39

Related Questions