March 06, 2017

om – the most recently modified file in a directory

In somewhat shocking news, I switched away from zsh a few months ago. The complexity of a shell which takes 369 pages to tell its tale finally got the better of me. (man -t zshall | ps2pdf - man_zshall.pdf will create the aforementioned pages.) I missed three things about zsh: plain text shell history; case-insensitive completion; and vi *(.om[1]) to edit the most recently modified file. I’ve learned to work around the first two, and I used to use the following shell script – which I called ‘latest’ and put in ~/bin – to recreate the third.

#!/usr/local/bin/dash

if [ -d "$1" ] ; then
    dir=$(cd "$1" || exit ; pwd)
    file=$(ls -1AFt "$1" | sed '/[/*@=%|]$/d' | sed q)
    echo "${dir}"/"${file}"
else
    echo "$1" "is not a directory"
fi

Well now I’ve gone a bit further, and written my first bit of code in C. This was an unexpected turn of events, and involved a lot of time on Stack Overflow, some wailing and gnashing of man pages, heaps of looking at Other Peoples’ Code and an awful lot of ‘rip it up and start again’. Anyway: it compiles, and – so far – it hasn’t done anything horrible to my machine, that I know of. You can find it here, in a shiny new git repository. It’s a tiny thing, bordering on frivolous, but it works great and brought me no small measure of satisfaction.

om is surprisingly handy! Some examples:

    vi "$(om notes)"
    cp $(om ~/Downloads) ~/src
    tail -f $(om /var/log)
    scp `om .` frobzbox:
    mpv `om dl`