Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Renaming File In Script To Date, How to use date as new name for file
roo
post Aug 28 2003, 02:32 AM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 2
Joined: 28-August 03
Member No.: 1,337



Hi

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.

Z
Go to the top of the page
 
+Quote Post
hughesjr
post Aug 28 2003, 09:12 PM
Post #2


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 3,433
Joined: 25-July 03
From: Corpus Chrsiti, TX, USA
Member No.: 1,151



I think I would also put access in the name ... but what you need to do th DTE is this:
CODE
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:

CODE
test = `ls`


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


--------------------
Johnny Hughes
hughesjr@linuxhelp.net
Enterprise Alternatives: CentOS, WhiteBoxEL
Favorite Workstation Distros (in order): CentOS, Gentoo, Debian Sarge, Ubuntu, Mandrake, FedoraCore, Slackware, SUSE
Favorite Server Distros (in order): CentOS, WhiteBoxEL, Debian Sarge, Slackware, Mandrake, FedoraCore, Gentoo, SUSE
Go to the top of the page
 
+Quote Post
roo
post Aug 29 2003, 05:16 AM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 2
Joined: 28-August 03
Member No.: 1,337



Thanks

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

Thank you very much for your time & help.

Zac
Go to the top of the page
 
+Quote Post
hughesjr
post Aug 29 2003, 05:39 AM
Post #4


Its GNU/Linuxhelp.net
*******

Group: Admin
Posts: 3,433
Joined: 25-July 03
From: Corpus Chrsiti, TX, USA
Member No.: 1,151



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.


--------------------
Johnny Hughes
hughesjr@linuxhelp.net
Enterprise Alternatives: CentOS, WhiteBoxEL
Favorite Workstation Distros (in order): CentOS, Gentoo, Debian Sarge, Ubuntu, Mandrake, FedoraCore, Slackware, SUSE
Favorite Server Distros (in order): CentOS, WhiteBoxEL, Debian Sarge, Slackware, Mandrake, FedoraCore, Gentoo, SUSE
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 22nd October 2017 - 04:13 PM