Sunday, February 25, 2007

Unix System Administration Task: Delete Log Files

We routinely delete old log files. Why? Some of them are very large and take up valuable space on the hard drive. These old logs were generated by workstations/servers, web servers, databases, and many other ubiquitous applications.

Note: When I stated old log files, I meant terminated files--not ones with open file handles.

# cd /where_your_logs_are
# find . -name '*.deadlog' -exec rm {} \;


Luke said...

The suggested command will usually not delete the log file, because the daemon which is logging to that file will still have an open file handle to it. The file will just be unlinked from its directory, rendering it invisible (along with any subsequent log messages).
Most daemons have a mechanism for requesting the log file be closed and re-opened, eg sending a SIGHUP signal to the daemon. Still it is not ideal to remove the most recent log messages... what about log rotation?

esofthub said...


Most of the logs we delete have been terminated by a daemon. In fact, some of our logs end with a *.dead extension or something else other than *.log. I should have been clearer about that. Thanks for adding your valuable comment.

Falso said...

Is there a way that the logs can be deleted in one command?