promptFunc() {
# right before prompting for the
# next command, save the previous
# command in a file.
echo "$(date +%Y-%m-%d--%H-%M-%S) \
$(hostname) $PWD $(history 1)" \
>> ~/.full_history
}
PROMPT_COMMAND=promptFunc
This is in addition to standard bash history, and is much more reliable. Being able to see not only what command I ran but what directory I was in and when I ran it is very useful.
I do something very similar (just saved in files by month) and it has saved my bacon a few times. I was recently asked to re-run a process that I ran years ago. It would have taken me hours to re-discover how to run the process but instead took a few minutes after grepping the commands.
Yeah, this is how I do it - although I append the hostname to the end of the filename and sync the collection between my machines so that I can grep for history on any machine.
Edit: went to check and apparently I've overcomplicated this somewhat (it's about ten years old) but it is nice if empty lines are not logged. It's an old habit to ctrl-c then hit return repeatedly if a terminal is not responding.
PROMPT_COMMAND=storehist
storehist ()
{
CMDCNT=`history 1 | sed -e 's/\w* *\(.*\)/\1/'`;
if [ "$CMDCNT" != "$LASTCMDIND" ] && [ -n "$LASTCMDIND" ]; then
DATE=`date '+%Y%b%d %H%M'`;
if [ "$LASTCMDIND" != "$CMDCNT" ]; then
echo "$DATE $HOSTNAME:$BASHTTY $CMDCNT" >> ~/.custom_history;
fi;
LASTCMDIND="$CMDCNT";
fi;
LASTCMDIND="$CMDCNT"
}
(I wrote about this here: https://www.jefftk.com/p/you-should-be-logging-shell-history)