The history -r ensures that all terminals are always synced with the latest history commands from all other terminals,
because the read from file only occurs when a terminal is started.
You are right, it can be slow if the history file grows too large; in which case just settle for "history -a" - everything is kept but recent commands typed in other terminals are not available.
#Backup history so far
if [[ ! -e ~/bash_history.bck.1 ]]; then
cat ~/.bash_history > bash_history.bck.1
else #Only backup new bits.
while [[ -e ~/bash_history.bck.$count ]]; do
let "count += 1"
done
let "last=count-1"
new_line=`nl -n rz ~/.bash_history | grep "==== bck" | tail -n 1 | cut -c1-6`
#make sure it is interpreted in decimal
new_line=$(( 10#$new_line ))
echo $new_line
split -a1 -l $new_line ~/.bash_history ~/bash_history.bck.$count
rm bash_history.bck.${count}a
mv bash_history.bck.${count}b bash_history.bck.${count}
fi
#Remove duplicates from history but retain ordering
nl -n rz ~/.bash_history | sort -k2 -u | sort | cut -f2- > ~/.bash_history
#history | sort -k2 -u | sort -n | cut -f2-
# Add a marker line to separate new history from compressed history.
echo ===================================== bck.$count === `date` >> ~/.bash_history
The history -r ensures that all terminals are always synced with the latest history commands from all other terminals,
because the read from file only occurs when a terminal is started.
You are right, it can be slow if the history file grows too large; in which case just settle for "history -a" - everything is kept but recent commands typed in other terminals are not available.
I like to keep the full history for analysis purposes, see www.oreillynet. com/onlamp/ blog/2007/ 01/whats_ in_your_ bash_history. html
http://
but in order to keep the file size small for quicker loading, I am using the following script as a workaround to archive and remove duplicate entries:
#!/bin/bash
let count=1
#Backup history so far history. bck.1 ]]; then history. bck.$count ]]; do history. bck.$count bck.${count} a bck.${count} b bash_history. bck.${count}
if [[ ! -e ~/bash_
cat ~/.bash_history > bash_history.bck.1
else #Only backup new bits.
while [[ -e ~/bash_
let "count += 1"
done
let "last=count-1"
new_line=`nl -n rz ~/.bash_history | grep "==== bck" | tail -n 1 | cut -c1-6`
#make sure it is interpreted in decimal
new_line=$(( 10#$new_line ))
echo $new_line
split -a1 -l $new_line ~/.bash_history ~/bash_
rm bash_history.
mv bash_history.
fi
#Remove duplicates from history but retain ordering ======= ======= ======= ======= == bck.$count === `date` >> ~/.bash_history
nl -n rz ~/.bash_history | sort -k2 -u | sort | cut -f2- > ~/.bash_history
#history | sort -k2 -u | sort -n | cut -f2-
# Add a marker line to separate new history from compressed history.
echo =======
exit