ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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:
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?
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
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.
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?
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.