Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-23-2012, 11:04 PM   #1
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Script - Remove old log file

I have a script which runs every 10 min. and updates a log file named /home/jack/logsfile.
if [ condition true ];
echo "Working fine." >> /home/jack/logsfile
Since this script runs every 10 min and keep appending the result to /home/jack/logsfile, so this log file is getting larger day by day, so I have to manually remove it everyday. I want to add some condition in my script so that, at end of day (like at 11:50 PM or at beginning of that day like at 12:00 midnight) once it remove the old existing file (last day's logsfile) and then when it runs again, it generates a new logsfile and keep appending the result for whole day and so on...

I am not getting a clue that how to do it? If I add a
rm /home/jack/logsfile
at the begining of the script, then it will remove the logsfile everytime it will run. But I want to keep logs (only) for at least a day.

Last edited by shivaa; 12-07-2012 at 01:06 PM.
Old 11-23-2012, 11:27 PM   #2
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
These the script im running everyday once to backup my log


#Script -Purpose:Backup script
# Exits with zero if no error.

# Step -1 Function to archieve file using date and time

date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`

# Step -2 Function to create folder on /root/logs with Timestamp

mkdir -p /root/logs/$date

# Step -3 Function to find & move logs from /data to /root/logs

find /data/ -type f -iname *.logs -mtime +30 -print | xargs -I {} mv {} /root/logs/$date

#Step -4 Function to compress the log Backup Content

tar -cvzf /root/log/$date /root/logarchive/$date.tar.gz

#Step -5 Function to remove the Log

rm -rf /root/log/*

#Step -4 Function to Print the logs Backup status

echo "$(date) log Backuped successfully ">>/root/logs/logbackup-status-$date.log

#Step -5 Function to call the Mail trigger to user


#Step -5 Function to trigger mail after log backup status

mail -s '$(date) Logs Moved Successfully $(hostname) - Successful' $mailid

Last edited by jsaravana87; 11-23-2012 at 11:30 PM.
Old 11-24-2012, 10:37 AM   #3
LQ Newbie
Registered: Oct 2012
Location: Toronto, ON
Posts: 6

Rep: Reputation: Disabled
You can just redirect the black hole to the log file. It won't delete the file but will just clear the contents of the file.
cat /dev/null > filename.log
Add a condition to run the above code when a file size goes larger than, or number of lines go above x or everyday at midnight.

Last edited by HaydeezPluto; 11-24-2012 at 10:39 AM.
Old 11-24-2012, 01:16 PM   #4
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
#!/usr/bin/perl -w


die("Usage: $0 filename-in-cwd") unless (defined($f));
die("filename contains improper chars") unless ($f =~ /^\w[\w\.-]+$/);

if ( (defined($ARGV[1])) && ($ARGV[1] =~ /^\d+$/) && ($ARGV[1] > 5000) ) {


if ($size > $max_size) {
    rename("$f.old1", "$f.old2");
    rename("$f.old", "$f.old1");
    rename($f, "$f.old") or die("could not rename file $f to $f.old");
    open(F, ">$f");
Old 11-26-2012, 02:15 AM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,949

Rep: Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634Reputation: 2634
Why not use the built-in tool 'logrotate' ?
4 members found this post helpful.
Old 12-07-2012, 01:03 PM   #6
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800

Original Poster
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Used a simple workaround, side-by-side created one more script & added it to cron, which moves such log file (although I have more such scripts) once in a day at some point of time.
mv /home/jack/archive_logs/logfile /home/jack/archived_logs/logfile.$(date +%Y%m%d)
Old 12-08-2012, 02:22 PM   #7
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,945

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
use the find command paired with -mtime:


find /path/to/logfile.log -mtime +1 -exec rm '{}' \;
put that as the last line right before the exit, this will look for any log that is older then 24hr and remote it from the system. As mentioned above setting the DATE as a timestamp for the logs is also a great idea. If you only want to keep one days worth then you might use something like this in the log name:

### Setting up variables. 
dow=`date +%A`

### find command to clear log files older then one day

find ${LOG} -mtime +1 -exec rm '{}' \;
Old 12-25-2012, 10:55 PM   #8
Registered: Oct 2012
Posts: 96

Rep: Reputation: Disabled
hi but this is sh file.
which means once i run it it wont quit even it done the job?
or should i cron this?
Old 12-26-2012, 12:00 AM   #9
Senior Member
Registered: Mar 2012
Posts: 1,815

Rep: Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627
At top of script you could:
tail -n 23 /home/jack/logsfile > tmpfile && mv tmpfile /home/jack/logsfile
Then you always have the last 24 hour log.
1 members found this post helpful.


logs, rotation, script

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
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 09:23 AM
script to remove text from file brixtoncalling Programming 19 10-04-2009 02:04 AM
How to remove first 2 lines of a file in a script nazs Programming 16 02-19-2007 08:08 AM
remove text from file with script paul_mat Linux - Software 3 11-17-2005 01:21 PM
How to remove ssh headers from log file? sswaminath Programming 1 07-25-2005 12:43 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 PM.

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