Register
It is currently Fri Sep 19, 2014 1:51 pm

How do get certain files, based on date-coded filenames?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Nov 12, 2008 8:01 am   

Joined: Wed Nov 05, 2008 6:19 am
Posts: 7
Hi all,

I'm in need of a script that gets me the log files of today & yesterday, as in the following example:
Code:
/usr/local/bin/consolidate.pl /SERVER/exYYMMDD-1.log /SERVER/exYYMMDD.log > /tmp/logfile.log

(where YYMMDD is today's date)

so for today it would be:
Code:
/usr/local/bin/consolidate.pl /SERVER/ex081111.log /SERVER/ex081112.log > /tmp/logfile.log


How do I do this...?


Top
 Profile WWW ICQ  
 PostPosted: Wed Nov 12, 2008 10:26 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
This is something I've never really figured out the best way to do... It's pretty easy to get today's date in the format that you want with something like:

Code:
date +%y%m%d


The problem is getting yesterday's date. Since the "day" is the last field, you could take today's date and subtract 1, but that won't work on the first of the month. I've seen a lot of stuff in *nix that uses Unix time (great article on wikipedia: http://en.wikipedia.org/wiki/Unix_time) to make it easy to do math on it, but converting back and forth can be a pain.

Thanks to you, I have learned something new today, and it's so simple! To get yesterday's date, try this:

Code:
date --date "$(date) 1 day ago" +%y%m%d


So to match those two files with yesterday's date and today's, you can do something like this:

Code:
#!/bin/sh

today=$(date +%y%m%d)
yesterday=$(date --date "$(date) 1 day ago" +%y%m%d)

echo "Today's date is: $today"
echo "Yesterday's was: $yesterday"

echo "Your command might look like:"
echo "/usr/local/bin/consolidate.pl /SERVER/ex${yesterday}.log /SERVER/ex${today}.log > /tmp/logfile.log"

## Output should look like this:

# Today's date is: 081112
# Yesterday's was: 081111
# Your command might look like:
# /usr/local/bin/consolidate.pl /SERVER/ex081111.log /SERVER/ex081112.log > /tmp/logfile.log


Hope this helps!


Top
 Profile YIM  
 PostPosted: Thu Nov 13, 2008 1:26 am   

Joined: Wed Nov 05, 2008 6:19 am
Posts: 7
It sure does! Thank you! 8)

Now my server logs are totally automatically processed in awstats :-)

I had already started to look into using php, but bash is so much easier! :-)


Top
 Profile WWW ICQ  
 PostPosted: Fri Nov 14, 2008 3:37 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
Quote:
date --date "$(date) 1 day ago" +%y%m%d


You do not need the internal date and also 'yesterday' can be used instead of '1 day ago' the following is good;

Code:
date -d yesterday +%y%m%d


Top
 Profile  
 PostPosted: Fri Nov 14, 2008 5:16 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Shiny!


Top
 Profile YIM  
 PostPosted: Mon Nov 17, 2008 7:38 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
You could also just use find to do this. I believe something like this should work fine:
Code:
find -mtime +1 /path/to/files/ \*.log -exec cat {}; >> /tmp/logfile.tmp


Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP