Half of bash history is missing

25,417

Solution 1

I can't know what happened without access to your machine but here is a short explanation of how the history system works which might help you figure out what happened.

Each open terminal has its own history buffer. These buffers are appended to your $HISTFILE when the terminal is closed (maybe also whenever the buffer is filled, but I don't know how often that happens). Now, the way to search for a command in your history is to simply run:

history | grep command

But if the command was run in a different shell, you won't see it in the history of your current one. To fix that, you close all open shells, open a new terminal window and search your history again.

If that still doesn't help, you've probably passed the threshold of commands stored in the $HISTFILE. The behavior of the $HISTFILE is controlled by various environment variables (see man bash for the full list), but the relevant ones here are:

   HISTSIZE
          The  number  of commands to remember in the command history (see HISTORY below).  If the value is 0, commands are not saved in the history list.  Numeric values less than
          zero result in every command being saved on the history list (there is no limit).  The shell sets the default value to 500 after reading any startup files.

   HISTFILESIZE
          The maximum number of lines contained in the history file.  When this variable is assigned a value, the history file is truncated, if necessary, to contain no  more  than
          that number of lines by removing the oldest entries.  The history file is also truncated to this size after writing it when a shell exits.  If the value is 0, the history
          file is truncated to zero size.  Non-numeric values and numeric values less than zero inhibit truncation.  The shell sets the default value to the value of HISTSIZE after
          reading any startup files.

The higher values you set these to, the more commands you'll keep in your $HISTFILE. For example, I use:

HISTSIZE=999999
HISTFILESIZE=999999

If you want to import the history from one shell into another, you can use the history command:

$ help history | grep -E -- '-a|-r'
      -a    append history lines from this session to the history file
      -r    read the history file and append the contents to the history

So, run history -a to write the history from one terminal and then history -w to read it from the another. Now, running history will show you the history of both shells.

Finally, you can make all your terminals share the same history by adding these lines to your ~/.bashrc:

## history -a causes the last command to be written to the
## history file automatically and history -r imports the history
export PROMPT_COMMAND='history -a;history -r'

I also suggest you add this:

## Make Bash append rather than overwrite the history on disk:
shopt -s histappend

Solution 2

Your command worked for me...

$ cat .bash_history | grep inxi
inxi -b
sudo apt install inxi
inxi -b
inxi -Fxz -c 0 > inxi_list.txt
inxi -Fxz
inxi -Fxz
sudo inxi -Fxz

I also periodically copy history to a file and save it.

history > history_$(date '+%Y-%m-%d_%H:%M:%S')_$(hostname).txt
Share:
25,417

Related videos on Youtube

WinEunuuchs2Unix
Author by

WinEunuuchs2Unix

Software development is my main hobby. Check out the new websites created in October 2021: www.pippim.com and pippim.github.io

Updated on September 18, 2022

Comments

  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 1 year

    The other night I was reading AU Q&A and used a bash command:

    inxi -????
    

    Problem is today I don't remember characters comprising ????. I want to put the command and parameters into my documentation spreadsheet. Based on an this answer (How to recall history in teminal) I used this command:

    $ cat .bash_history | grep inxi
    inxi -b
    sudo apt install inxi
    inxi -b
    

    However the command I want isn't there even though the history goes far back. I've used the inxi commands many times in the terminal since that old history but none of is showing up.

    I've also tried Ctrl+R+inxi without any luck. Because I open multiple terminal windows all the time is history tied to a specific window?

    Is there a different way to grep bash history file(s)?

    Note that I do not prefix terminal commands with a Space Bar such that they are supressed from history.

    • Zanna
      Zanna about 7 years
      I think it's not your search methods that are flawed, but the mysterious way that some commands aren't saved when running multiple shells. The same thing annoys me plenty and if I weren't so lazy I'm sure I'd find a brilliant & highly detailed explanation for it probably written by Eliah Kagan
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    @Zanna and I were discussing this in AU general chat room after she read this question and the same things happen to her. We thought of filing a bug report but decided we were both too lazy :) I suggested we could test this by opening two terminal windows, entering commands and seeing which window updated .bash_history and which didn't. But we were too lazy to carry out experiment. I floated the idea that it's a locking issue and only one terminal gets write access to .bash_history. I like your suggestion of updating ~/.bashrc and have just done that on my machine. Thank You :)
  • terdon
    terdon about 7 years
    @WinEunuuchs2Unix The history -a and history -r commands are really useful when you want to test that sort of thing. You can run history | tail, then history -a from the other terminal, history -r from the original one and your new commands magically appear if you now run history | tail again.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    Funny thing is I've often noticed history was missing when pressing Up-Arrow but today was the first time I really cared about it. Hopefully implementing your suggestions for ~/.bashrc means it won't happen again.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    I renamed the question to "Half of bash history is missing" to reflect what is really happening here. I hope you don't mind.
  • terdon
    terdon about 7 years
    @WinEunuuchs2Unix ah, hang on, you might also be hitting another problem. Did you log in from a tty recently? Although that probably won't be an issue since Ubuntu's default .profile sources .bashrc.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    I do use Ctrl+Alt+F1 every now and then for fun to try stuff out but not very often and never to run inxi command from the console. Although I've added aliases to .bashrc I haven't dabbled in .profile yet.
  • terdon
    terdon about 7 years
    @WinEunuuchs2Unix yes, but if you log in from a tty, that could cause your history file to be truncated. See the explanation in the answer linked to in my previous comment. That is probably not an issue in your case since Ubuntu's .profile reads .bashrc, but it's something to look into.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 7 years
    I honestly don't think tty has anything to do with it. I hardly use Terminal Teletype (right phrase?) at all....maybe like once every month and only to run some obscure command that I won't look for in history in the first place.
  • David M. Perlman
    David M. Perlman over 6 years
    Copy and paste this to automatically add the right lines to your .local.bash [note: it's up to you to determine if you want .bashrc, .profile, etc.] ... { echo '## history -a causes the last command to be written to the'; echo '## history file automatically and history -r imports the history'; echo "export PROMPT_COMMAND='history -a;history -r'"; echo '## Make Bash append rather than overwrite the history on disk'; echo 'shopt -s histappend'; } >> ~/.local.bash
  • kristopolous
    kristopolous almost 5 years
    My history is constantly missing and it's an absolute crapshoot whether anything gets saved at all, anywhere. I've been trying to fix this problem literally since 1997 and nothing works. My only solution up to this point is to redirect the logs to have a PID based naming scheme with logrotate running on them and then a subsequent custom-built toolchain to search them. It's the only solution I've found where large swaths of history doesn't go missing. Why on earth does it have to be this hard, I have no idea. It's bonkers.
  • terdon
    terdon almost 5 years
    @kristopolous please post a separate question for this. Include the output of grep -i hist ~/.bashrc ~/.bash_profile ~/.profile /etc/bash.bashrc /etc/profile. Also have a look at this Q&A where I was facing a similar issue.
  • kristopolous
    kristopolous almost 5 years
    @terdon. Thanks I used to keep around manual patches of the bash source code to fix this problem up until about 10 years ago when I moved over to my current solution. I really don't think posting some .bashrc file will be productive.
  • terdon
    terdon almost 5 years
    @kristopolous well, what you describe isn't normal, so the likeliest scenario is some sort of weird combination of history settings. I assume you've been carrying the same config files around if you're facing the problem for so long. Seriously though, that's precisely the kind of issue the folks over at Unix & Linux like solving. Try posting a question there, you never know, someone might figure it out!
  • kristopolous
    kristopolous almost 5 years
    I often have 50+ shells open across xterms/tmuxes etc, that's the problem. There's file locks which often failed and bailed instead of creating a buffer of the backlog of failed entries to write when successful. multiple handles open on a single file and this is what happens. There's ways to sacrifice performance and make sure things work by asking bash to do a bunch of overhead between commands but the lag is quite noticeable. My old solution used unix sockets to send history to a "master writer". But keeping things separate then tooling over them seems to solve all this. Still crazy though.
  • terdon
    terdon almost 5 years
    @kristopolous seriously, please ask a question, preferably on Unix & Linux. Sounds like precisely the kind of problem we enjoy there and there are regulars there who know the shell inside out. I'm guessing you've tried history -a; history -r as part of PROMPT_COMMAND but there may be more tricks. I also often have that many shells open but don't have the issue you mention.
  • Albert G Lieu
    Albert G Lieu about 3 years
    adding export PROMPT_COMMAND='history -a;history -r' to .bashrc does combine historyes in different terminals, however, I can't use ! history_number to run the command anymore. Is there a bad side effect of adding the line?
  • terdon
    terdon about 3 years
    @AlbertGLieu I'm sorry but I don't understand what you mean. Please post a new question where you can explain exactly what you are trying to do.
  • Albert G Lieu
    Albert G Lieu about 3 years
    @terdon, thank you very much for your quick reply. Sorry, please allow me to rephrase it: Adding export PROMPT_COMMAND='history -a;history -r to .bashrc does merge the history from different terminals, it works great. But when I try a command from history, you would type history in a terminal, then run the command with ! followed by the number before the command in history. Let's say the number of the command I am trying to run is 4009, then when I do !4009, the terminal returns bash: !4009: event not found
  • terdon
    terdon about 3 years
    @AlbertGLieu please post a new question. Comments are hard to read, easy to miss and can be deleted without warning. We would need to know details about what you have done, steps to reproduce etc. Posting a new question lets other people see the issue who might also be able to help and will help future visitors with similar problems. I cannot reproduce what you describe so it will require more debugging and that cannot be done in the comments.