Operating System - HP-UX
1748129 Members
3649 Online
108758 Solutions
New Discussion юеВ

Re: how do I add a date to the name of copied files

 
SOLVED
Go to solution
Mark Vollmers
Esteemed Contributor

how do I add a date to the name of copied files

I had this great idea to setup a cron job to go and pull the syslog and whatever logs that I wanted to keep an eye on. I would do this every week. I also thought that it would be neat if I could set up a script to take the file, copy it to another folder, and rename it by adding the date so I could keep them straight. However, I was planning on pulling the date using "grep {regular exp} date", and the system tells me that I can't use grep with the date command. Any ideas on how to get a date so that I can add the date to the file names? Thanks.
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
7 REPLIES 7
Patrick Wallek
Honored Contributor
Solution

Re: how do I add a date to the name of copied files

To create a file with a date extension you can use the date command with the +format syntax. See the date man page for more info.

I created a file called file.02192001 with the command:

touch file.`date +%m%d%Y`

You can use the `date +%m%d%Y` syntax in a script as well.
James R. Ferguson
Acclaimed Contributor

Re: how do I add a date to the name of copied files

Hi Mark:

You can do something like this:

cp /var/adm/syslog/syslog.log /var/adm/syslog/syslog.log_`date +%Y%m%d_%H%M%S`

This will copy the syslog.log appending the current date and time, as for example:

...syslog_20010219_161148

that is, CCYYMMDD_HHMMSS.

...JRF...
Rick Garland
Honored Contributor

Re: how do I add a date to the name of copied files

As an idea;

set `date +'%d %b %Y'`
DAY=$1
MONTH=$2
YEAR=$3
DATE=$DAY$MONTH$YEAR

Then copy the file to the new location with the $DATE appended to the end.

cp FILE /tmp/FILE.$DATE

Steven Sim Kok Leong
Honored Contributor

Re: how do I add a date to the name of copied files

Hi,

If you are copying a considerable number of log files, then an alternate way of organisation would be to create subdirectories for each timestate. For example, if you have the cron job running everyday at 2300 hrs, then your cron job can be:

============================================
#!/sbin/sh

DATE=`date +%Y%m%d`.`date +%H%M`

for log in /var/adm/syslog.log /var/adm/cron/log /var/adm/shutdownlog # modify your list accordingly
do
mkdir -p /archive/logs/$DATE
cp $log /archive/logs/$DATE
done
============================================

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Steven Sim Kok Leong
Honored Contributor

Re: how do I add a date to the name of copied files

The "mkdir" should be outside the for loop.
============================================
#!/sbin/sh

DATE=`date +%Y%m%d`.`date +%H%M`

mkdir -p /archive/logs/$DATE
for log in /var/adm/syslog.log /var/adm/cron/log /var/adm/shutdownlog # modify your list accordingly
do
cp $log /archive/logs/$DATE/
done
============================================
Shannon Petry
Honored Contributor

Re: how do I add a date to the name of copied files

IMPORTANT

If you are going to copy syslog.log to syslog.date then you will also have to either stop and start the system logger, or send it a kill -HUP so that it restarts it's logs.

Interestingly enough if you rename syslog.log, it will keep growing as the name is not used by syslogd.

This is true of more services than syslog, so read a bit before you just rename as you may not always get the results you expect.


Now, heres my idea for your task. Myself, I have done what you are attempting. First rule is to keep the date where you will understand it, and can manipulate it.
#!/bin/sh
DATE=`date +%m-%d-%y`
gives the format 02-20-01 which is much easier for ME than
#!/bin/sh
DATE=`date +%m.%d.%y`

Next, if you have many files, write your script to do them all at once. It is not a bad idea to keep your list of files in a separate file to keep maintanance easy. NOTE: I customize /etc/syslog.log, so have more than the default logs.
I.E.
>cat /usr/local/etc/rotate.txt
/var/adm/syslog/syslog.log
/var/adm/syslog/mail.log
/var/adm/syslog/daemon.log
/var/adm/syslog/critical.log

Next, a simple script
#!/bin/sh
INFILE='/usr/local/etc/rotate.txt'
DATE=`/usr/bin/date +%m-%d-%y`
for LOG in [/bin/cat $INFILE ] ; do
/bin/cp -p $LOG $LOG.$DATE
done
/sbin/init.d/sendmail stop
/sbin/init.d/sendmail start
/usr/bin/kill -HUP `cat /var/run/syslog.pid`


HP needs to build in "restart" in the script cases for init scripts....

Hope it helps!
Shannon
Microsoft. When do you want a virus today?
Mark Vollmers
Esteemed Contributor

Re: how do I add a date to the name of copied files

Thanks for all the ideas! I really appreciate the help.
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"