LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-27-2014, 10:06 AM   #1
RayLui20
LQ Newbie
 
Registered: Jul 2013
Posts: 16

Rep: Reputation: Disabled
Writing Bash script to delete logs files > 2 days


Hi all,

I am still learning linux and Bash. At my job, we usually clear logs that are older than 1-2 days old. I wrote a very simple script a months ago to clear all logs from 2013, but now I want to modify it to clear log files that are older than 1 or 2 day old. The following is my script. The variable SL and i are cluster and server #, and servicehost is the name of the servers

#!/bin/bash

#servicehost
for SL in 0 1 2 3 4 5
do
for i in 1 2 3 4 5
do
echo sl1"$SL"servicehost"$i"
ssh sl1"$SL"servicehost"$i" "rm -rf /path/to/base/dir/log1/*.*.2013*;
rm -rf /path/to/base/dir/log2/*.*.2013*;
rm -rf /path/to/base/dir/log3/*.*.2013*;
rm -rf /var/log/*.[1-9]
rm -rf /var/log/httpd/*-2013*"
done
done
 
Old 01-27-2014, 10:34 AM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,365
Blog Entries: 11

Rep: Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333Reputation: 2333
find with mtime maybe?
Example I use:
Code:
find . -name '*ScriptedDump_*' -mtime +2 -delete
Hope that's helpful.
 
Old 01-27-2014, 12:32 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Habitual beat me to it. use the find command much less mucking around... i prefer the following end to my find line though:

Code:
find ${PWD}/logs/*.log -mtime +15 -exec rm '{}' \;
you can also add the -rf flags if they are required for your system, but dont forget the \; at the end of the line or it will not work with the -exec argument.
 
Old 01-27-2014, 12:36 PM   #4
RayLui20
LQ Newbie
 
Registered: Jul 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
I have more than one server and more than 1 path that I want to clear...any way to add those lines into my script? I am fairly new to BASH

Last edited by RayLui20; 01-27-2014 at 03:29 PM.
 
Old 01-27-2014, 07:01 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
you can make an array with all of the paths/servers populated or you can make 1 line for each with its own variable.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Find out timestamp line and delete if it is older than particular/specific days using bash script nitya Linux - Newbie 10 09-26-2014 01:30 PM
How to delete two days older files through script azheruddin Linux - Newbie 4 01-08-2013 12:18 PM
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
bash: delete files older than 7 days in a folder and subfoldes lupomanaro Programming 1 05-07-2008 10:22 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM


All times are GMT -5. The time now is 02:52 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration