Read all of the terminal command history in OS X

It would be great to access all my commands run in terminal on OS X so I can review and use as a running reminder sheet.

I'm using Reverse-I-Search prompt, and have updated my .bash_profile to store all of my history as mentioned here on Mactoids: How to search Terminal command history.

  1. Start Terminal.

  2. Navigate to the Home folder by entering:

    cd ~/
    
  3. Create .bash_profile by entering:

    touch .bash_profile
    
  4. Now, either edit .bash_profile in your favorite text editor or type this in the Terminal window to automatically open the file in the default TextEdit:

    open -e .bash_profile
    
  5. Lastly, add this to the .bash_profile file:

    HISTFILESIZE=1000000000 HISTSIZE=1000000
    
  6. Save and exit.

Do you have any ideas how I could access in order to output the terminal command history in OS X?


All of your history is stored in ~/.bash_history, where both reverse-i-search and the up/down keys use. That file is regularly pruned, but if you followed the guide in your link, the .bash_history file will practically never be pruned.


For newer versions of MacOS (Catalina and up) you'll find your bash history in ~/.zsh_history by default:

cat ~/.zsh_history

Personally, I would prefer to do it in a simpler way and print everything, instead of checking the latest session which doesn't cover all Terminal windows and all commands.

Get a full history

cd ~/.bash_sessions
cat *.historynew *.history

If you want to sort by session date

cd ~/.bash_sessions
cat `ls -tr *.historynew *.history`

In case you still need a fix for this, here's how I did mine. With this, I can SAVE AND ACCESS history across all tabs (i.e. if you enter a command on one tab, then open a new tab and press up, it will suggest the command you just entered in the previous tab)

You'll need 2 things: 1. Enter this command in your terminal to make sure histappend is turned on:

shopt -s histappend && shopt histappend

2. You'll also need to know where your history commands are being stored.

My history files are stored in ~/.bash_sessions so that's what my code will reflect. If yours are stored in ~/.bash_history, or another directory, just swap that for ~/.bash_sessions when we source it into our bash_profile.

Once you've figured that out, open your bash_profile and add the following code:

source ~/.bash_sessions/*.history        #<--sources prev sessions through your bash_profile. If you don't use ~/.bash_sessions to store your history, replace it with whatever you use (i.e. source ~/.bash_history/*.history

export HISTCONTROL=ignoredups:erasedups #<-- auto-erases duplicates in your history
export HISTSIZE=1000                    #<-- assigns # of results to return
export HISTFILESIZE=100000              #<-- assigns # of results to store in your .bash_history
shopt -s histappend                     #<-- appends & saves history throughout all tabs

export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"  <--appends history from all tabs, clears & uses appended history file as current