Saturday, January 22, 2011

Bash them files

I had to regularly clean up a folder of old files in one of the mounts on a Linux box. Being a Python programmer mostly doing his development in Windows, the quickest solution I could think of was writing a Python script using
shutil.rmtree
.

The pseudo-code is like this:
import os
import shutil

from datetime import datetime

# save time now
# list all folders in mount
# for each folder 4 hours older than the saved time, call shutil.rmtree

Without posting all of the code you can see that I also had to handle any exception. The whole script was about 20 lines -- which I was pretty proud of...until I discovered I can do the same thing using:

find /mnt/Archive -mindepth 1 -depth -mmin +240 -delete

:/...I should have remembered the rule I was following years ago which made me somewhat master DOS batch programming.

Use the right tool for the job...and shell commands (and shell scripts) more often than not perform a stellar job at system maintenance.

*sigh* I've been trying to put off having to learn
awk, sed, xargs, etc
which are often used in conjuction with other commands in shell scripting. It has finally caught up with me.

No comments: