Tôi đã viết một số dòng trong .bashrc của mình để thực hiện các thao tác sau: Lưu mỗi phiên sau mỗi lệnh vào một tệp. Sẽ có nhiều tệp lịch sử như bạn đã từng bắt đầu thiết bị đầu cuối. Khi bắt đầu một thiết bị đầu cuối mới bắt đầu từ tệp lịch sử gần đây nhất, tải tất cả các tệp lịch sử từ các phiên trước vào bộ đệm lịch sử cho đến khi đạt đến ngưỡng đếm dòng.
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