Read all of the terminal command history in OS X

10

4

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?

Cameron McGrane

Posted 2011-07-10T21:25:23.737

Reputation: 215

4I presume you've read man bash, so you should know that the history is stored in the file ~/.bash_history. What else do you need to know, can you clarify? – None – 2011-07-10T21:46:18.157

Yes, because I new the 'man' command existed then read the manual found the answer and decided to ask the question above just for the fun of it. Thanks fideli for quick, clear and unpretentious answer. – Cameron McGrane – 2011-07-11T01:15:36.817

Answers

6

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.

fideli

Posted 2011-07-10T21:25:23.737

Reputation: 13 618

2

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`

laimison

Posted 2011-07-10T21:25:23.737

Reputation: 151

1

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  

sharpharp

Posted 2011-07-10T21:25:23.737

Reputation: 11

1be aware that running source ~/.bash_sessions/*.history executes all the commands – panchicore – 2018-03-21T08:08:58.920