3 ms·
I've been doing something similar for years. Originally I tried to keep everything in .bash_history, but over the years and different machines it inevitably get
by amoss 3y ago
I've been doing something similar for years. Originally I tried to keep everything in .bash_history, but over the years and different machines it inevitably gets wiped sooner or later.
This helps (I use bash rather than zsh):
# Sort out history$
export HISTIGNORE="&:ls:vi:bg:fg:history"
export HISTCONTROL=ignoredups:ignorespace
export HISTSIZE=100000
export HISTFILESIZE=100000
shopt -s histappend
shopt -s checkwinsize
But the long-term (multi-machine) solution is to keep another append-only copy, one per machine:
function storehist() {
# Bug in mac version of bash stops HISTCMD getting set in function called from PROMPT_COMMAND...
CMDCNT=`history 1 | sed -e 's/\w* *\(.*\)/\1/'`;
if [ "$CMDCNT" != "$LASTCMDIND" ] && [ -n "$LASTCMDIND" ] # First time, no prev, do not log random
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";
}
export PROMPT_COMMAND='storehist; ....
A little bit over the top and crusty but it has worked like that for years so I've not updated it in a while. There is a separate sync script that puts the .custom_history from each machine / container into a folder on each machine with the orginating machine name in the file-name.
This keeps ^-R working over a long period of time, but eventually that stops being a good way of find obscure commands from long ago and then something like this works:
cat .histories/* | grep CMD | grep WERID_ARG | grep RANDOM_RELEVANT_STR | sort -u | less
The overall cost of running some script on each prompt is negliable on a modern machine and as the OP notes the disk space does not amount to much. It becomes very useful over time. I have about 20MB of shell history that stretches back to '06. It is one of things that is easy to setup, and yet the value accrues steadily over time. Across the dozen or so machines involved I doubt that the archive would have survived long-term without the step to export it outside of the shell's real history file.
- riffraff 3y agoFWIW, I have export HISTCONTROL=ignoreboth which I think is shorthand for ignoredups:ignorespace