Roy
Roy

Reputation: 309

Shell scripting : Find file with the name 'error_log' and check sizes, remove if over a certain size

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!

Upvotes: 0

Views: 2545

Answers (2)

Dennis Williamson
Dennis Williamson

Reputation: 360685

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.

Upvotes: 1

wilhelmtell
wilhelmtell

Reputation: 58715

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.

Upvotes: 1

Related Questions