LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 05-18-2012, 03:52 AM   #1
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Rep: Reputation: Disabled
Sript to delete older logs in a FS


Hi

I need some help with the scipt for deleting the older logs in a FS.

I was using this below script previously where the FS was simple.

find /var/sitelogs/httpd -mtime +1 -exec rm {} \;

But now what we do normally with the current FS is :

1.cd /etc/httpd/application/
2.du -sh *

plication]# du -sh *
16K lost+found
302M ukwwwsit01
233M ukwwwsit04
1.7M ukwwwsit05
27M ukwwwsit05.working
152M ukwwwsit06

3.cd <dir which occupies more space>
4.Find older logs and delete.
I am not sure how to go ahead with this as a script.

Any suggestion would be appreciated.

Thanks
Madhu
 
Old 05-18-2012, 04:14 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, the main problem is to find the name of the directory with the larger size. After that, you can use the same find command. To easily parse the output of du, first remove the -h option and try something like this (using command substitution):
Code:
large_dir=$(du -s * | sort -n | awk 'END{print $2}')
This will sort numerically the size of the directories in ascending order and the awk command will extract the directory name from the last row of the output (the largest one). After that:
Code:
find $large_dir -mtime +1 -exec echo rm {} \;
Please, notice the echo command after -exec. This is for testing purposes: you cannot risk to remove your precious data before being sure it works as expected, therefore the echo just displays the commands without actually run them. Hope this helps.
 
Old 05-18-2012, 04:26 AM   #3
madhumithamohan
LQ Newbie
 
Registered: Jul 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks for that.

So firstly i will use this below command to find the larger dir:
large_dir=$(du -s * | sort -n | awk 'END{print $2}')

[root@ngmlx494 application]# ll
total 36
drwxr-x--- 2 apache apache 16384 Feb 10 2011 lost+found
drwxr-xr-x 9 wwwsit01 apache 4096 Sep 14 2011 ukwwwsit01
drwxr-xr-x 9 wwwsit04 apache 4096 Jan 13 16:32 ukwwwsit04
drwxr-xr-x 8 wwwsit05 apache 4096 Sep 29 2011 ukwwwsit05
drwxr-xr-x 8 wwwsit05 apache 4096 May 8 2011 ukwwwsit05.working
drwxr-xr-x 9 wwwsit06 apache 4096 Oct 14 2011 ukwwwsit06
[root@ngmlx494 application]# du -s * | sort -n | awk 'END{print $2}'
ukwwwsit01
[root@ngmlx494 application]#


Secondly i need to get in to Logs path (sorry i missed initially)

cd logs

Thridly use this find cmd to delete older logs:
find $ logs -mtime +1 -exec echo rm {} \;



Not sure how to link all the commands as a single script.
 
Old 05-18-2012, 06:12 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
#!/bin/bash
#
# ---------------------------------------------------------------------
#  Change to local working directory
# ---------------------------------------------------------------------
cd /etc/httpd/application

# ---------------------------------------------------------------------
#  Find the name of the largest directory
# ---------------------------------------------------------------------
large_dir=$(du -s * | sort -n | awk 'END{print $2}')

# ---------------------------------------------------------------------
#  Remove log files older than 1 day (echo for testing)
# ---------------------------------------------------------------------
find $large_dir/logs -mtime +1 -exec echo rm {} \;
This assumes the sub-directory containing the log files is named logs (all lowercase). It was not clear from your post. Hope this helps.
 
  


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
Command to delete anything older than ... rbalaa Linux - Software 4 08-17-2011 02:19 PM
delete files when it's older 30 days Madison00 Linux - Newbie 8 01-07-2011 02:07 AM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
removing logs older than 30 days linuxN3w8ie Linux - Newbie 3 11-21-2006 01:27 PM
delete files older then a month? Red Squirrel Linux - Software 1 10-05-2005 10:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:32 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