Rename file command in unix with timestamp

2019-04-06 06:50发布

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..

标签: linux shell unix
4条回答
疯言疯语
2楼-- · 2019-04-06 07:08

This this:

 str=abc; mv ${str}.log ${str}-$(date +%F'-'%T).log
查看更多
孤傲高冷的网名
3楼-- · 2019-04-06 07:12

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 
查看更多
【Aperson】
4楼-- · 2019-04-06 07:20

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

查看更多
\"骚年 ilove
5楼-- · 2019-04-06 07:21

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.

查看更多
登录 后发表回答