views:

91

answers:

2

Hi guys,

I am trying to create a shell script/command that find error_log files created by PHP, check their file sizes and remove them if over a certain size.

I have only gotten as far as printing the files and file sizes with the code below

for i in `locate -r 'error_log$'`;do echo "$i|" `stat -c %s $i`;done

Can anyone help?

Thanks in advance!

+1  A: 
find $DIR -type f -name error_log -size +${BYTES_MIN}c -print0 |xargs -0 rm

For example:

find . -type f -name error_log -size +500k -print0 |xargs -0 rm

This will quietly remove any error log file anywhere under the current directory and larger than 500k (c for bytes, k for kilobytes, M for megabytes, ...). If you wish to see the destruction done then add -v to rm.

wilhelmtell
Substitute ` |xargs -0 rm` with ` -delete` and we have a winner.
Wrikken
Thank you Wilhelm, but can it be done with 'locate'?Find will take a long time to traverse into hundreds of thousands of folders.
Roy
+1  A: 

I would recommend using logrotate, but that presupposes that you know where the log files are. I wouldn't use locate since that uses a database that may be stale or even not updated at all.

Dennis Williamson
We can't use logrotate because this error_log file is not the apache error_log that is usually found in the /logs/ directory but it is present in hundreds of directories and is created whenever a PHP script in that directory runs into some kind of errors, even harmless ones. And let's assume that we are not allowed to turn the error_log file generation feature in PHP. Anyway locate is the right tool for the job as my server is a cPanel server, it updates the locate DB probably once a week anyway.And I plan to run this cleanup script once every 2 weeks.
Roy