meisel
meisel

Reputation: 2439

OS X terminal history missing commands

I know that I entered a command the other day, but now I can't find it using history or .bash_history. Any ideas why this happens? I suspect it has to do with using multiple tabs in my OS X terminal, and somehow the history doesn't carry over between tabs, but I don't know.

Upvotes: 17

Views: 16852

Answers (6)

Tim Siwula
Tim Siwula

Reputation: 983

echo 'alias history="history 1"' >> ~/.zshrc && source ~/.zshrc && history

Upvotes: 0

jmgonet
jmgonet

Reputation: 1261

In my case, when I type history it only shows me the latest 16 commands. However, I can recall older commands via Ctrl+R, so I know that the command history is actually longer.

As of 2021, the command history is located in a file called .zsh_history:

  • Open your command terminal.
  • Navigate to your home directory: cd ~
  • List all files: list -al
  • You should find it there.

You can open it with vi: vi .zsh_history, and search for your command.

Upvotes: 2

StevieD
StevieD

Reputation: 7443

I'm pretty certain this is a bug. I logged in on a guest account so the configuration files are pristine. In the home directory, I did:

touch .bash_history and then tail -f .bash_history to see when commands got saved.

Commands are supposed to save to the global .bash_history file when the terminal session is closed. I found that at least half of the time, the commands were not saved to the file after closing out the terminal. It seemed pretty random as to when it would happen and when it wouldn't. It also seemed to come in streaks where it would many times in a row and then get flaky again. It seemed to work every time if I quit the terminal session with exit instead of Command-W.

Upvotes: 3

Sean Gibbons
Sean Gibbons

Reputation: 371

I had this issue with my terminal. It turns out it was saving to newly generated files in the ~/.bash_sessions folder. This is part of a new method of managing bash sessions introduced with El Capitan outlined in the /etc/bashrc_Apple_Terminal file. This is confirmed by the outpit of echo $HISTFILE. To disable this behaviour, simply run touch ~/.bash_sessions_disable. Then quit the Terminal app and restart it. It should now save the history properly, as can be confirmed again via echo $HISTFILE. I hope this helps!

Upvotes: 27

Chris Page
Chris Page

Reputation: 18693

As of OS X El Capitan 10.11, by default Bash is configured to save separate command histories for each terminal, so they can be restored separately for Resume.

Each individual history is also appended to the global history in ~/.bash_history when the shell is exited. If you quit Terminal and then re-open it—with or without Resume enabled—you should find that commands from every terminal are in ~/.bash_history. If you have Resume enabled, each restored terminal will only contain its restored history, but when you create a new terminal it will start with the latest global history.

Because all the command histories are appended to the global ~/.bash_history file, you may wish to increase the number of commands stored by setting the HISTFILESIZE environment variable so the latest terminal histories don't push the other terminal histories out of the file too soon. The default value is 500. I have mine set to 10,000. I also set HISTSIZE to 10,000 so I can navigate through the entire history (otherwise, only the last 500 will be read from the history file).

The script that arranges for separate command histories is in /etc/bashrc_Apple_Terminal in OS X El Capitan 10.11 and later. It contains extensive comments describing how the mechanism works, and how to customize or disable it.

Upvotes: 8

Andras
Andras

Reputation: 3055

this happens to me all the time. I open multiple command windows, and history is saved in the order they're closed. The commands you're looking for might have gotten overwritten by another command window.

Upvotes: 3

Related Questions