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.
Basically, this bit of code calls find against a folder for each of three extensions. find returns a list with the full path of each file, which is dumped to a temporary file. This file is then counted for the number of lines (wc -l), which is used as the upper bound when choosing a random line. The randomly-chosen line number is then used by sed to get just that line from the file, which is then passed to open -e to open in TextEdit.

