1748219 Members
4472 Online
108759 Solutions
New Discussion юеВ

How to rename file

 
SOLVED
Go to solution
rustam_2
Super Advisor

How to rename file

Hi all,
I need to rename file to 'file_name_sysdate'. Because everyday one log file generates in the same directory and rewrites previous one. I need to have all of log files during 2-3 weeks. Log files cant be generated with unique name(pity but it's software feature). Always the same
File generates once a day in midnight, and i want to rename this file in the morning to . Is it possible with script?

Regards,
Rustam
11 REPLIES 11
Patrick Wallek
Honored Contributor

Re: How to rename file

Something like this in a script should do:

mv file_name.log file_name_$(date +%m%d%Y).log


This would yield a file with a name like file_name_02172011.log

rustam_2
Super Advisor

Re: How to rename file

Hi Patrick,

I couldnt run your syntax in bash environment. I run and got error:

$ mv file_name.log file_name_$(date+%m%d%Y).log
-bash: date+%m%d%Y: command not found

how can i explain to bash date+%m%d%Y?
Shrikant Lavhate
Esteemed Contributor
Solution

Re: How to rename file

Did you forgot the space in between date and + ? :-)

date +%m%d%Y
Will it remain a personal, if I broadcast it here!
sangilak
Trusted Contributor

Re: How to rename file

Hi,


You missed a space:

# date+%m%d%Y
-bash: date+%m%d%Y: command not found

# date +%m%d%Y
02182011

So add the missing space and it will work!


sangilak
Steven Schweda
Honored Contributor

Re: How to rename file

> [...] file_name_$(date +%m%d%Y).log
>
> This would yield a file with a name like
> file_name_02172011.log

And if you use something like "_%Y-%m-%d"
instead, then you might get names which will
be easier to read and which will sort in the
normal order.

Just a thought.
rustam_2
Super Advisor

Re: How to rename file

Thanks for all of you. I hope i can set it in crontab file and run everyday automatically.

Regards,
Rustam
Peter Nikitka
Honored Contributor

Re: How to rename file

Hi Rustam,

be aware, that the percent sign "%" has a special meaning in crontab entries, so it needs an esacpe character.
It may be
date +\%Y ...
or
date +%%Y ...
depending on the OS version you use - look at the man page.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
rustam_2
Super Advisor

Re: How to rename file

Hi Peter,
so if i want to use in crontab i must change my script to something like this?:

mv my_file.log my_file_$(date +%%m%d%Y).log

My OS version is 11.3

regards,
rustam
Dennis Handly
Acclaimed Contributor

Re: How to rename file

>if I want to use in crontab I must change my script to something like this?

You must quote each %:
mv my_file.log my_file_$(date +\%m\%d\%Y).log