Rename file command in unix with timestamp

2019-04-06 06:28发布

问题:

Hello i'm using putty and trying to rename a file name with current time stamp ... 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..

回答1:

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 


回答2:

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.



回答3:

This this:

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


回答4:

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



标签: linux shell unix