1

I have two directories (~/www/ and ~/client-sites/) that contain a bunch of sites, each with access.log and error.log files.

What is the easiest way to find all of these files and empty their contents? They grow quickly so I'd like to clear out logs once every month or so.

2
  • 13
    Why? You could just use logrotate. – Michael Hampton Jan 23 '13 at 22:47
  • 2
    No need to reinvent the wheel. Use logrotate. Custom tools will only confuse your coworkers and future sysadmins at your workplace. – Stefan Lasiewski Jan 25 '13 at 19:13
9

Install logrotate (Windows see http://sourceforge.net/projects/logrotatewin):

# CentOS/RHEL
yum install logrotate
# Debian/Ubuntu
apt-get install logrotate

Create/Edit /etc/logrotate.d/httpd, Example is taken from CentOS, adjustment is needed for other Distros.

/var/log/httpd/*_log {
    weekly
    missingok
    rotate 5
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /etc/init.d/httpd reload > /dev/null
    endscript
}
2

Toa ctually do what you requested

find ~/www/ and ~/client-sites/ \( -name 'access.log' -o -name 'error.log' \) \
    -exec sh -c '> "$1"' -- {} \;

Which will truncate each file matching either file name in any sub directories to 0 bytes.

1
  • 1
    Instead of running sh, truncate (part of coreutils) could be used, i.e. -exec truncate --size 0 {} \; or -exec truncate --size 0 {} \+ – Cristian Ciupitu Jan 29 '13 at 13:13
0

The apache web server comes with a utility called rotatelogs, which can be used to control log files.

Yes, hardly any distros use it.
No, they don't know better - they're just ignorant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.