Help - Search - Members - Calendar
Full Version: Renaming File In Script To Date
Linuxhelp > Support > Technical Support

I'm relatively new to linux. I'm looking to rename my log files by script using the date as the new name. So:

access_log gets rotated by logrotate, daily to access_log.1 I then I want to run a script to rename access_log.1 to 082303.log.

I tried:

DTE = date
mv access_log.1 $DTE.log

But obviously this doesn't work and I don't realy know where to go? How can I use the date and format it?

Any help very appreciated, thanks.

I think I would also put access in the name ... but what you need to do th DTE is this:
DTE = `date +'%m%d%y'`
mv access_log.1 $DTE.log

If you want a different format than 082803 like dashes 08-23-03 it would be date +'%m-%d-%y' ... 08-23-2003 would be date +'%m-%d-%Y'. See man date for other formats.

The ` key in front of the date and after the %y' is the key normally above the tab key, beside the 1 (one) key at the top left of the keyboard. In sh, ksh and Bash shells it means execute the command and set the variable to the result ... so:

test = `ls`

Would give you a variable that contained all the files in the directory...

What an excellent reply gave me everything I needed and explained some new stuff to me.

Thank you very much for your time & help.

You are welcome ...

btw ` also works in korn shell (pdksh), c-shell (tcsh) and Z shell (zsh) to execute a command and put it in a variable.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2018 Invision Power Services, Inc.