Here's my scenario: I have a folder with several thousand writings (.txt, .rtf, and .doc files) that span many years of my doing daily freewriting exercises. These freewritings are the basis for rough-drafts, which are the basis for short stories, etc. However, the hardest part of having so many bits and pieces is reading them over to revise them.
What I wanted was to have my Mac occasionally just open a random bit of writing I've done. With some googling and some referring to the man pages, I came up with the following. As with all bash scripts, once saving it, you have to do chmod +x scriptname. Personally, I call this from cron, so that each morning I have a random writing to read when I wake up.
Read on for the script...
#!/bin/bash
# File locations
WritingsPath=/Users/me/Documents/Writings
TempLog=/tmp/randomwriting.log
# Create a temporary logfile of all matches
find $WritingsPath -iregex ".*.txt" > $TempLog
find $WritingsPath -iregex ".*.rtf" >> $TempLog
find $WritingsPath -iregex ".*.doc" >> $TempLog
# Choose a random line number (any number from 1 to the length of the file)
LowerBound=1
RandomMax=32767
UpperBound=$(cat $TempLog | wc -l)
RandomLine=$(( $LowerBound + ($UpperBound * $RANDOM) / ($RandomMax + 1) ))
# Use sed to grab the random line
Command=$(sed -n "$RandomLine{p;q;}" "$TempLog")
# open the random line in TextEdit
open -e "$Command"
If anyone knows how to combine the find statements onto one line, I'd love to hear it, as I could then significantly shorten this script.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20051108193636341