The
history command is very helpful to show the last commands that were entered on the command line, but it rolls over, so the commands you entered a while ago are gone.
This is a solution I came up with which I use everywhere I have access. I have directory
$HOME/bin on my path, and this script named
log in that directory:
Code:
#!/bin/sh
LOG="$HOME/logs/cmdlog.txt"
test $# -lt 1 && echo "Log what?" && exit
test -f $LOG || { mkdir -p ${LOG%/*} && touch $LOG; } || exit 127
date "+%D $*" >> $LOG
After I get a command to work right that I know I'm going to want to reference, I hit up arrow, CTRL-A to get to the front of it, type "log " and hit enter. (I may need to add quotes and/or backslashes if it's a complicated command.) Now it's a permanent entry in my personal log file.
I also have these two functions declared in my standard
.bashrc file, which I copy to all machines I have access to. They make it easy to find things in the log file.
Code:
greplog() { test -n "$1" && grep "$@" "$HOME/logs/cmdlog.txt"; }
taillog() { tail $* "$HOME/logs/cmdlog.txt"; }