View Single Post
  #1   (View Single Post)  
Old 4th February 2010
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default Archiving a log file to a directory with the date incorporated in the new name

Being tired of renaming my serial.log files, I hacked the following script.
I left in the comments and the debugging stuff, so you can see my step by step approach
Code:
#!/bin/sh

FILE=serial.log
BUPDIR=Serconlogs

if [ ! -f ${FILE} ] ; then
   echo "$0: Sorry no file ${FILE} found!"
   exit 1
fi

mkdir -p ${BUPDIR}

# -rw-r--r--  1 j65nko  j65nko  146366 Feb  3 05:03:54 2010 serial.log
#    1        2   3       4       5     6   7    8      9     10

listing=$(ls -lT ${FILE})

# echo ${listing} | awk '{ print $6, $7, $8, $9 }'
# FILEnew=$(echo ${listing} | awk '{ printf("%s_%s_%s_%s_%s" , $9,$6,$7,$8,$10) }')
# echo $FILEnew
# 2010_Feb_3_05:03:54_serial.log
# we need to align the day in a field of 2 

FILEnew=$(echo ${listing} | awk '{ printf("%s_%s_%02u_%s_%s" , $9,$6,$7,$8,$10) }')
# echo $FILEnew
# 2010_Feb_03_05:03:54_serial.log

#echo mv ${FILE} ${BUPDIR}/${FILEnew}
mv ${FILE} ${BUPDIR}/${FILEnew}

ls -l ${BUPDIR}
In the following I first create a fake new file to be archived then run the script
Code:
$ echo aap >serial.log
$ ./arch-serial.log                                                      

total 3364
-rw-r--r--  1 j65nko  j65nko   146366 Feb  3 05:03 2010_Feb_03_05:03:54_serial.log
-rw-r--r--  1 j65nko  j65nko        4 Feb  4 04:47 2010_Feb_04_04:47:17_serial.log
-rw-r--r--  1 j65nko  wheel   1093167 Jun 12  2009 serial.log.old.txt
-rw-r--r--  1 j65nko  j65nko   433313 Jan 26 17:45 serial.log.old
For other examples of using awk and printf to manipulate directory listing information see http://www.daemonforums.org/showthread.php?t=2611
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote