Last Updated: February 25, 2016
·
1.44K
· rdosser

Automatically clean out development log files

Development can leave lots of debug log files lying around, and if you don't occasionally clean up those files, they can gradually grow into real monsters, especially if you're working at a highly detailed logging level.

Unix environments offer the "logrotate" utility, but that requires editing the configuration for every new file you need to manage. However, Unix also provides the "crontab" utility that allows you to define shell commands to be run at specific times. So if you don't want to rotate and store the old log files, it can be easier just to add an entry to your crontab that executes a shell command to find all files whose names match a given pattern and empty them out.

For example, if you want to launch a process at midnight every day to find all files under a project directory that end in debug_log.txt and empty their contents, add this to your crontab (using the "crontab -e" command):

0 0 * * * find /Users/myname/Projects/ -name *debug_log.txt -exec cp /dev/null {} \;

And every morning, you'll find those log files empty but otherwise unchanged.