DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[fman] tags out of date for ports BSDKaffee Feedback and Suggestions 1 12th June 2009 07:36 AM
strange "~" directory in home directory gosha OpenBSD General 5 23rd February 2009 06:12 PM
Using Date variable? cwhitmore FreeBSD General 9 13th August 2008 07:16 AM
Jails - mount: /usr/home: No such file or directory chris FreeBSD General 6 6th August 2008 10:47 PM
HOWTO: Always install an up-to-date port chris Guides 8 28th May 2008 11:53 AM


All times are GMT. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick