In the grand tradition of the UNIX Grymoire, Unix for the Beginning Mage, Veghead’s Spell Book, and Ars Technica’s guide to command-line wizardry1… I present to you some shell spells I have found handy on occasion.

I’m not promising that these aren’t awful. I’ll promise some are. I’m sure most can be done more elegantly, but each of these was polished to work well enough for its sometime purpose.

exit if most recent git commit in repo isn’t more recent than a certain length of time

# 5 * 60 is for five minutes
TIME_THRESHOLD=`echo "$(date +%s) - (5 * 60)" | bc`
LATEST=`git reflog -1 --date=unix | grep -o "[0123456789]\{10\}"`
if [ $LATEST -lt $TIME_THRESHOLD ]; then exit 0; fi

given a list of filenames, filter to those whose most recent relevant git commit was within a certain time threshold

This doesn’t run quick. It could be made much faster if you also filtered on file modification time first, but then you have to commit to some assumptions on whether your files are modified locally or from the git remote first.

# 5 * 60 is for five minutes
TIME_THRESHOLD=`echo "$(date +%s) - (5 * 60)" | bc`
find whatever_dir \ # or ls, anything that lists files
| xargs -I blah bash -c 'echo $(git reflog -1 --date=unix --pretty="format:%gd" blah) blah' \ 
| cut -d'{' -f2- \ # now we're just inelegantly mushing input into shape
| sed 's/\}//' \
| grep "[0123456789]\{10\} .*" \ 
| awk -v tt="$TIME_THRESHOLD" '$1>tt' \ # is it more recent? 
| cut -d' ' -f2 # trim off the time

with a URL as the sole command-line argument, create an HTML redirect page matching its folder structure

#!/bin/bash
URL=$1
TIME=`date +%Y-%m-%dT%H:%M:%S%z`
FMT='<html><head><meta charset="UTF-8"><meta http-equiv="refresh" content="0; URL=%s"/><meta property="article:modified_time" content="%s"/></head><body><p>redirecting you...</p></body></html>'
cd $WHEREVER_YOU_WANT_THIS_STUFF_STORED

# I have more checks exiting out under certain
# conditions because I'm too lazy to filter before
# invoking the script

FNAME=`echo $1 | cut -d'/' -f4-`
if [[ ! $FNAME == *.html ]]
then
	if [[ ! $FNAME == */ ]]
	then
	FNAME="$FNAME/index.html"
	else
	FNAME="$(echo $FNAME)index.html"
	fi
fi
FOLDER=`echo $FNAME | rev | cut -d'/' -f2- | rev`
if [[ ${#FOLDER} -ge 2 ]] # this isn't right but w/e
then
	mkdir -p $FOLDER
fi
printf "$FMT" "$URL" "$TIME" >./$FNAME
cd -

use awk to reorder fields

I keep figuring this out from scratch, so let’s just get it down.

In -v OFS='' the -v signposts there’s going to be an assignment, and OFS stands for Output Field Separator. This is relevant for some kinds of processing that… I don’t do, mostly, because I am always taping things together manually per case.

awk -F'separator' -v OFS='' '{print $2 " " $1}'
  1. I am all too happy to expand this list as I find or remember more.