Skip to content

Instantly share code, notes, and snippets.

@bewt85
Last active July 8, 2023 08:09
Show Gist options
  • Save bewt85/992c463e2d76f4d88979e3be11940409 to your computer and use it in GitHub Desktop.
Save bewt85/992c463e2d76f4d88979e3be11940409 to your computer and use it in GitHub Desktop.
here-here - what did I run here

here-here

here-here is like history but it saves the time you ran a command and the directory you were in when you ran it. Running here tells you all the commands you ran in this directory even if you were on a different server.

I commonly wonder how I made a file (and maybe why). Sometimes I try looking in my history (using the history command). This makes that easier by clearing out some of the noise.

Installation

If you want to use this, be my guest but, as much as I can, I deny all liability if this does any damage.

This seems to work on Mac OSX and Ubuntu using bash and zsh.

  • Copy save_history and here somewhere in your PATH (e.g. ~/.local/bin)
  • Make them executable (e.g. chmod u+x ~/.local/bin/here ~/.local/bin/save_history)
  • Do one of the following:
    • If you use bash (you probably do if you're not sure), run echo 'PROMPT_COMMAND="history | tail -1 | save_history $OLDPWD"' >> ~/.bashrc
    • If you use zsh, run echo 'precmd() { history | tail -1 | save_history $OLDPWD }' >> ~/.zshrc
  • Restart you terminal (e.g. close the window and open another one)

Advanced

History logs are stored in ~/.here-history with a new file per host (i.e. server) and day. Each line contains:

  • The time you ran the command
  • The server (or laptop) you ran the command on
  • The terminal you ran the command in (if you have multiple windows open)
  • The directory you ran the command in
  • The git commit reference at the time (if the directory is a git repo, otherwise -)
  • The command you wrote

You can download all the history from different servers into one place reasonably safely because the files include a hash of the server hostname. You can then analyse the logs to work out roughtly what version of the code you were running when you made your great discovery and where you were running it at the time.

Acknowledgements

I wrote this 4 years ago, stopped using it when I changed job and have just resurrected it. I have no idea where I got the idea or the basis of the code from.

That said, I worked with some really smart people at the Sanger Institute at the time so I suspect Andrew Page, Simon Harris (Simon has disclaimed all responsibility) or Martin Hunt were involved somehow because this looks like their level of geekiness. Then again, maybe not.

Licence

MIT

Improvements

If you want to make this better then please do. Please let me know if you have any suggestions for how it can be improved. I'd like it to remain compatible with OSX and Ubuntu and Bash and ZSH as a minimum.

Here are some of the improvements I like the sound of but don't have time to implement:

  • Show when you ran the command
  • Show commands in subdirectories
  • Sometimes it gets confused and tells you that you ran the last command more times than you did
  • Be more efficient (I'm surprised this works and there are reports history | tail -1 gets slow when you have a lot of history)
  • Collect history from other servers automagically
#!/bin/bash
set -eu
for f in $(ls ~/.here-history/history.*); do
awk "\$4 ~ \"^$PWD$\"" $f | cut -f 6-
done
#!/bin/bash
history -w
set -e
MD5SUM="md5sum"
if [[ "Darwin" == "$(uname -s)" ]]; then
MD5SUM="md5";
fi
DATE=`date +%Y%m%d`
TIME=`date +%s`
LINE=$(cat /dev/stdin | sed -E 's/^[ ]*[0-9]+. //')
HOST=`hostname`
HOST_HASH=`hostname | $MD5SUM | cut -c -8`
GIT_REF=$(git rev-parse --short HEAD 2>/dev/null || echo -)
if [ -z "$(echo $LINE | egrep '\bcd\b')" ]; then
DIR=$PWD
else
DIR=${1-"unknown"}
fi
HISTORY_DIR="$HOME/.here-history"
if [ ! -d "$HISTORY_DIR" ]; then
mkdir "$HISTORY_DIR"
chmod 0700 "$HISTORY_DIR"
fi
HISTORY_FILE="$HISTORY_DIR/history.${DATE}.${HOST_HASH}"
if [ ! -f "$HISTORY_FILE" ]; then
touch $HISTORY_FILE;
chmod 0600 $HISTORY_FILE;
fi
echo "${TIME} ${HOST} ${PPID} ${DIR} ${GIT_REF} ${LINE}" >> $HISTORY_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment