Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 916
meisel Avatar asked Dec 08 '14 01:12

meisel


People also ask

How do you get all history commands on a Mac?

To view your entire Terminal history, type the word “history” into the Terminal window, and then press the 'Enter' key. The Terminal will now update to display all the commands it has on record.

How do I get all history in Terminal?

Simple Command History Navigation The easy way to get started with your command history is to simply navigate with the up and down arrow keys. Using the up key, you can scroll through previous commands one command at a time. To scroll back down the list, you can use the down arrow key.

How do I find old command history on Mac?

In most cases, you want to see the most-recent commands. One way to do this is to run the history command with an argument that says how many commands you want to display. For example, history 5 tells the shell to display the commands starting with the fifth one in the history list.

How do I find previous Terminal commands?

Open a terminal application on your Linux or Unix and type history to list all commands. To search for a command in the history, press ctrl+r multiple times. For instance, you can hit the ctrl+r and type string to search.


2 Answers

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!

like image 87
Sean Gibbons Avatar answered Sep 23 '22 22:09

Sean Gibbons


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.

like image 37
Chris Page Avatar answered Sep 20 '22 22:09

Chris Page