LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sript to delete older logs in a FS (https://www.linuxquestions.org/questions/linux-newbie-8/sript-to-delete-older-logs-in-a-fs-945673/)

madhumithamohan 05-18-2012 03:52 AM

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

colucix 05-18-2012 04:14 AM

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.

madhumithamohan 05-18-2012 04:26 AM

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.

colucix 05-18-2012 06:12 AM

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.


All times are GMT -5. The time now is 11:10 PM.