Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
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

3 Answers 3

up vote 9 down vote accepted

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
}
share|improve this answer

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.

share|improve this answer
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
    
Thanks for answering, this worked great! :-) –  Mitch Feb 6 '13 at 4:06

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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