LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2006, 05:09 PM   #1
matttail
Member
 
Registered: Jan 2004
Posts: 36

Rep: Reputation: 15
shell scripting and 'date'


hi,

I was going to sit down and write a simple shell script (bash), that once I thought about it wasn't so simple. This will end up in a shared web-hosting enviroment in the end - a user wants to copy log files out of a directory and store them elswhere in his webspace so that they don't get deleted perodicly as they would if left in the log direcotry.

What needs to happen is that each day (cronjob) the file access.log.2006-04-26 and error.log.2006-04-26 needs to get copied elswhere. I thought I'd just use the date command to create the file name, but the problem is that each day the previous log must be copied out. So today, May 1, the Apr 30 log needs to be copied.

My question is how can I set up a script to easly grab these files? there's lots of other files in the folder so I don't want to just grab the entire thing. These two files are the only plain files in the folder - there's other folders and compressed .gz files.

Any sigguestions much apprechated.
 
Old 05-01-2006, 05:25 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
would using a wildcard work? as in:

cp *.log.* ../my_new_folder/*.log.*
 
Old 05-01-2006, 05:37 PM   #3
matttail
Member
 
Registered: Jan 2004
Posts: 36

Original Poster
Rep: Reputation: 15
no, becuase then I'll pick up all of the pervious day's log files that are named access.log.2006-04-25.gz as well. There's abotu 5 days worth of logs stored, as well as data for analog (web-based stats program) and I want to keep the destination clean and free of duplicates.

Thanks for the sigguestion though. I apprechate it.
 
Old 05-01-2006, 06:28 PM   #4
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Something like this might be what your looking for:


#!/bin/bash
DATE=`date +%Y-%m-%d`
find /var/log/messages -type f -name "access.log.$DATE.gz" -mtime 1 -exec cp /new/location {} \;
find /var/log/messages -type f -name "error.log.$DATE.gz" -mtime 1 -exec cp /new/location {} \;
exit 0


Just edit the /var/log to the real location of the files and the cp /new location to where you want the files to go

You may need to play around with it a bit, haven't tested it so I'm not too sure if it will work properly, but it may be a start.

Hope this helps

Last edited by fotoguy; 05-01-2006 at 06:31 PM.
 
Old 05-01-2006, 07:18 PM   #5
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
I been looking this over myself and finally come up with what I think you might be after. I am assuming you only want to copy the previous day log like you named above to a new location?

#!/bin/bash
DATE=`date +%F -d "1 days ago"`
cp /var/log/messages/access.log.$DATE.gz /new/location
cp /var/log/messages/error.log.$DATE.gz /new/location
exit 0

Run the command to make sure the date is correct in structure.
date +%F -d "1 days ago"

Change the 1 to a 2 if you want 2 days earlier.

Let me know how it goes. Thanks there fotoguy on the script. I kept messing up the DATE variable. I had it backwards. Not the best at scripting but learning. I was about to resort to the awk and sed commands. I new there must be a simply way.

If it works you can change the cp command to the mv command.
mv /var/log/messages/error.log.$DATE.gz /new/location/error.log.$DATE.gz

Brian1

Last edited by Brian1; 05-01-2006 at 07:37 PM.
 
Old 05-01-2006, 10:31 PM   #6
matttail
Member
 
Registered: Jan 2004
Posts: 36

Original Poster
Rep: Reputation: 15
awasome "1 days ago" thing. I looked all through the manual for date and couldn't fine anything documenting that. It works perfectly, I tested the script, and after fixing my typos it runs without problem. Thanks for all the help!

What is the point of exit 0 at the end of the script? I've never used/seen that before.

Thanks for all the help again!
 
Old 05-02-2006, 02:39 AM   #7
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: Custom Debian Live ISO's
Posts: 1,291

Rep: Reputation: 62
Quote:
Originally Posted by Brian1
I been looking this over myself and finally come up with what I think you might be after. I am assuming you only want to copy the previous day log like you named above to a new location?

#!/bin/bash
DATE=`date +%F -d "1 days ago"`
cp /var/log/messages/access.log.$DATE.gz /new/location
cp /var/log/messages/error.log.$DATE.gz /new/location
exit 0

Run the command to make sure the date is correct in structure.
date +%F -d "1 days ago"

Change the 1 to a 2 if you want 2 days earlier.

Let me know how it goes. Thanks there fotoguy on the script. I kept messing up the DATE variable. I had it backwards. Not the best at scripting but learning. I was about to resort to the awk and sed commands. I new there must be a simply way.

If it works you can change the cp command to the mv command.
mv /var/log/messages/error.log.$DATE.gz /new/location/error.log.$DATE.gz

Brian1

Glad to help out, I'm only learning as well. Funny that, I was thinking awk, grep and sed myself.

The exit 0 just means the script exits with out any errors, you don't need to have it, the script will exit normally. I just throw it in as a bit of programming standard for myself.
 
Old 05-02-2006, 05:44 PM   #8
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Gald to be of help as well. I made a note of this but not where I found it a while back. I think I found the one I seen this from. http://www.tldp.org/LDP/GNU-Linux-To...calendars.html

Brian1
 
Old 05-02-2006, 10:56 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The common convention on Unix like systems is that programs (inc scripts) exit with value zero if ok, else another positive int if not.
This enables you to check the result via the std lines
Code:
<your_prog_here>
if [[ $? -eq 0 ]]
then
   ok
else
    error handler
fi
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
Shell scripting: Doing MySQL Dump base on date Swakoo Linux - General 2 09-21-2005 10:23 PM
date manipulation in shell peal_ss Programming 4 03-29-2004 04:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration