Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use multiple Konsole terminals. And I want all the commands I type in every terminal to be saved in command history, so that next konsole i open will have all of them. To prevent each terminal from over writing the other terminal's command history, I gave the following settings in my .bashrc

# avoid duplicates and commands starting with space
export HISTCONTROL=ignoredups:erasedups:ignorespace
# append history entries..
shopt -s histappend
#My machine reboots without warning sometimes.Hence to save commands instantaneously.
export PROMPT_COMMAND="history -a"  
export HISTSIZE=1000
PS1="\[\e[1;34m\]\! \[\e[0m\]"$PS1

I gave the last line to see the command number in my prompt. The command no. has never gone above 600, but still some of my old commands are disappearing from the history. There are many commands which are given repeatedly, but as expected from ignoredups, it never increases the command no in prompt. Yet old commands are still disappearing.And the number of commands in history is always remaining slightly more than 500.

The .bash_history file still contains a lot of duplicates in spite of ignoredups.

PS: The output of echo $HISTSIZE and $HISTFILESIZE are both =1000


Update: I found the problem in the above entry to .bashrc. Just calling history -a in PROMPT_COMMAND simply appends the last new command to the .bash_history. So the ignoredups and erasedups have no effect.

Is there any way, I can still write to the .bash_history without duplicates from every terminal? I don't want to load the entire history at each command prompt by history -r and write it again back with history -w, because the commands I issued in one terminal will appear in another parallel running terminal also. I want the combined commands to appear only in a new terminal.

The puzzle, why my history was getting trimmed to 500 is solved. I noticed it happened each time I ssh into this machine. Creating a .bash_profile with the following entry solved this problem.

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

Now my .bashrc is executed each time I ssh too. And the history file size is now monotonically increasing.

share|improve this question

migrated from stackoverflow.com Sep 9 '12 at 13:16

This question came from our site for professional and enthusiast programmers.

    
Is this what you are after? unix.stackexchange.com/questions/1288/… –  jasonwryan Sep 9 '12 at 18:58
    
No. I want to avoid the second requirement in Oli's list. I don't want my different terminal's history to get mixed up. Until I start a new terminal. This is what naturally happens when i properly shutdown my machine. But sometimes proper shutdown is not possible and unless i save the history instantaneously, all will be lost in a sudden power off. –  indiajoe Sep 9 '12 at 19:14
add comment

2 Answers 2

I have concluded that it is not possible to append the new commands directly and at the same time to remove any duplicates. So my solution is to run the following command ones in a while. or to put it in my crontab.

tac $HOME/.bash_history | awk '!seen[$0]++' | tac > $HOME/.hist_Temp 
mv $HOME/.hist_Temp $HOME/.bash_history

The above command will keep the last occurrence of a command and remove every other repetitions above it from the history file without messing up the order.

share|improve this answer
add comment

It's ugly, and I'm sure that sed or awk would be better, but I tried to fix the same problem by adding this in my .bashrc:

export HISTFILE=.bash_history_`tty | python -c 'import sys ; sys.stdout.write(sys.stdin.read().strip().replace("/", "_"))'`

This will create a separate history file for each terminal that you open, however if you run bash within bash, it will use the same history, but shouldn't overwrite. I will warn you with a caveat from my experience. While this does work and keeps the history from being overwritten, you have to grep multiple files to try and find the history you are looking for, which can sometimes be time consuming.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.