skip to main bit
a man slumped on his desk, from 'The Sleep of Reason Produces
      Monsters'

Oblomovka

Currently:

the most useful simple script i have

Lee’s comment that mentions having a folder for items you’re about to delete reminds me of probably the script that has most contributed to sanity in my filing system. It’s pig simple, albeit a bit scary to write and enact. All it does is delete everything in a given folder that’s over a week old.

I’ve had bad experience with handing “delete file” powers to an automatic script before, so I’ll disclaim any warranty (“TO THE EXTENT PERMITTED BY APPLICABLE LAW” as the GPL shouts), but it’s pretty straightforward, and works for me: I have it in a cronjob. The tmp folder it cleans up is my default save folder on Firefox, and where I generally download everything. If I want to save anything longer than a week, I find it a place in the rest of my filing system. It’s sort of like having a cleaner come around every week: occasionally you go “Garr! Where’s that coffee-stained, have torn copy of last month’s New Yorker! I was going to eventually get around to reading that!”, but mostly your cruft just silently disappears without you noticing a thing.

You’ll need to replace /home/danny/tmp with your own dumping ground. If you run it like this:

It’ll tell you what it’s planning to delete. Run it without the -d and it really will delete those things, blam.

11 Responses to “the most useful simple script i have”

  1. Waider Says:

    So you’ve basically rewritten (some of) tmpwatch in python. Of course, tmpwatch appears to be a linux-only thing (it’s not available in fink, either, from a cursory glance), so I guess having a version for Mac or whatever is useful.

  2. Waider Says:

    gah, neglected to close that a tag. oh well.

  3. manu Says:

    Er, that’s probably a stupid question, but why don’t you just use find? That’d be a one-liner:

    find "$tmpdir" -atime +7 -exec rm -r {} \;

  4. Christian Neukirchen Says:

    A different approach, that works well for me who is paranoid (or romantic?) about keeping old stuff: http://chneukirchen.org/blog/archive/2006/01/keeping-your-home-clean-with-mess.html

  5. Don Says:

    @manu – my career has taken me away from hands-on scripting/programming but I was able to do so much with ksh and the sed/awk family that perl & python were rarely necesary (for the sort of tasks I was trying to accomplish). I always like simple.

  6. Bluefoo Says:

    The find example above is reasonable, but instead of the -exec it may prove safer (with strange filenames) to use the extensions of GNU find.

    find “$tmpdir” -type f -mtime +7 -delete
    find “$tmpdir” -type d -exec rmdir “{}” \;

    That should delete files not modified for 7 days and remove all old directories that are empty (or made empty by this process) since rmdir should only touch empty directories. You may want to experiment with the -p option of rmdir to make sure you get all the parents otherwise depending on the find order you may have some left.

  7. Danny O'Brien Says:

    Bluefoo gives pretty much the reason why it’s in Python. After several iterations of writing it in find (and having spaces and strange characters mess things up), I just thought, screw it, this is ridiculous, I’ll just spell it out s-l-o-w-l-y and methodically in Python, so at least I’ll be able to understand what went wrong when it messes up. It’s all part of my sinister plan to get good at one language, rather than know a little bit about a lot of them.

    Of course, the best option of all is Waider’s — find somebody who has already done this for you. Thanks!

  8. Danny O'Brien Says:

    It looks like tmpwatch has been replaced (in fink and in Debian) with the package-most-like-an-EC-Comic, tmpreaper.

  9. Jake Says:

    You can mitigate funny characters in find with :
    find “$tmpdir” -type f -mtime +7 -print 0 | xargs -0 rm -fv

    … which changes the seps in the find output to be nulls, and makes xargs expect null sep’d items.

  10. Waider Says:

    Hey, this horse won’t move no matter how hard I beat it!

    All the find variants are missing a pretty important flag: -depth. This avoids the need for fancy flags to rmdir, error messages about trying to traverse directories you just deleted, etc. But really, using someone else’s already-written tool is the correct (and Henry Spencer-approved, by implication (commandment #7)) way of doing things.

  11. L. Says:

    “Lee’s comment that mentions having a folder for items you’re about to delete”

    …called the Trash on the Mac.

    “But really, using someone else’s already-written tool is the correct way of doing things”

    …which is why Waider and I use MacBooks.

    Shouldn’t -d be what does the delete, rather than the other way around, to help avoid accidental deletion? Imagine if you had to do rm rather than rm -fr to silently nuke everything… looking at the semantics of rm via its man page, we already have -v, but wouldn’t -i be nice?

                                                                                                                                                                                                                                                                                                           

petit disclaimer:
My employer has enough opinions of its own, without having to have mine too.