In Ubuntu
, whenever I have a few terminals open, I close my current session and open a new one, the history of the commands typed in those terminals won't show up with history
. The history of only one such terminal will show up.
What exactly does history
keep track of?
The history is stored in a file specified by HISTFILE
.
You can find the information kept by the history in the manual of history (man history
):
typedef struct _hist_entry {
char *line;
char *timestamp;
histdata_t data;
} HIST_ENTRY;
For bash usually the variable HISTFILE
is set to .bash_history
which is common to all shells.
Have a look at this nice history guide for more hacks:
The Definitive Guide to Bash Command Line History. There you can find also details to the histappend
parameter commented by hek2mgl:
Option 'histappend' controls how the history list gets written to HISTFILE, setting the option will append history list of current session to HISTFILE, unsetting it (default) will make HISTFILE get overwritten each time.
For example, to set this option, type:
$ shopt -s histappend
And to unset it, type:
$ shopt -u histappend
I'm using the the approach outlined here
Basically, its using python and sets
to handle an unique list of all commands entered in all you shells. Result is stored in .my_history
. Using this approach, all commands entered in every open shell is immediately available in all other shells. Every cd
is stored so the file needs some manual cleaning sometimes but I find this approach better suited for my needs.
Necessary updates below.
.profile:
# 5000 unique bash history lines that are shared between
# sessions on every command. Happy ctrl-r!!
shopt -s histappend
# Well the python code only does 5000 lines
export HISTSIZE=10000
export HISTFILESIZE=10000
export PROMPT_COMMAND="history -a; unique_history.py; history -r; $PROMPT_COMMAND"
*unique_history.py*
#!/usr/bin/python
import os
import fcntl
import shutil
import sys
file_ = os.path.expanduser('~/.my_history')
f = open(file_, 'r')
lines = list(f.readlines())
f.close()
myset = set(lines)
file_bash = os.path.expanduser('~/.bash_history')
f = open(file_bash, 'r')
lines += list(f.readlines())
f.close()
lineset = set(lines)
diff = lineset - myset
if len(diff) == 0:
sys.exit(0)
sys.stdout.write("+")
newlist = []
lines.reverse()
count = 0
for line in lines:
if count > 5000:
break
if line in lineset:
count += 1
newlist.append(line)
lineset.remove(line)
f = open(file_, 'w')
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
newlist.reverse()
for line in newlist:
f.write(line)
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
f.close()
shutil.copyfile(file_, file_bash)
sys.exit(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With