मैंने अपने .bashrc में कुछ पंक्तियाँ लिखी हैं जो निम्नलिखित को पूरा करती हैं: प्रत्येक कमांड को प्रत्येक कमांड के बाद फाइल में सेव करें। उतने ही इतिहास की फाइलें होंगी जितनी आपने कभी टर्मिनल शुरू की हैं। सबसे हाल के इतिहास फ़ाइल से एक नया टर्मिनल शुरू करने से पहले इतिहास के सभी फाइलों को इतिहास बफर में एक पंक्ति गणना सीमा तक पहुंचने तक लोड करें।
HISTCONTROL=''
HISTFOLDER=~/.bash_histories
HISTFILEEXT=history # only files in $HISTFOLDER with this extension will be read
shopt -s histappend # append when closing session
mkdir -p $HISTFOLDER
HISTFILE=$HISTFOLDER/$(date +%Y-%m-%d_%H-%M-%S_%N).$HISTFILEEXT # create unique file name for this session. Nanoseconds seems to be unique enough, try: for ((i=0; i<=10; i++)); do date +%Y-%m-%d_%H-%M-%S_%N; done
# if HISTFILE unset, history is not saved on exit -> not really necessary if we save after each command, but its a double net safety
HISTSIZE=-1 # maximum number of commands to hold inside bash history buffer
HISTFILESIZE=-1 # maximum number of lines in history file
# history -a $HISTFILE # bash saves the total history commands entered since startup or since the last save and saves that amount of commands to the file. This means reading a history file after typing commands will trip up bash, but this won't be a problem if the history file is only loaded in the beginning. This means that only new commands are saved not all the old loaded commands, thereby we can load as many history files into the buffer as we want and still only save newly thereafter typed commands
PROMPT_COMMAND="history -a $HISTFILE; $PROMPT_COMMAND" # This command is executed after very typed command -> save history after each command instead after only closing the session
# Load old histories from last 5 files/sessions
HISTLINESTOLOAD=2000
# --reverse lists newest files at first
names=($(ls --reverse $HISTFOLDER/*.$HISTFILEEXT 2>/dev/null))
toload=()
linecount=0
# Check if is really file and count lines and only append to $toload if linecount under $HISTLINESTOLOAD
for fname in ${names[*]}; do
if test -f $fname; then
linecount=$((linecount+$(wc -l < $fname) ))
if test $linecount -ge $HISTLINESTOLOAD; then
break
fi
toload+=($fname)
fi
done
# Beginning with the oldest load files in $toload into bash history buffer
for (( i=${#toload[*]}-1; i>=0; i-- )); do
history -r ${toload[$i]}
done