27

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?

2
  • 4
    I 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?
    – nobody
    Jul 10, 2011 at 21:46
  • 1
    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. Jul 11, 2011 at 1:15

6 Answers 6

13

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.

3
  • FIFO or LIFO pruning?
    – Pacerier
    May 3, 2020 at 19:35
  • @Pacerier, I can't be completely sure as I no longer use macOS but if I had to guess, FIFO pruning.
    – fideli
    May 8, 2020 at 2:57
  • 9
    sometimes not ~/.bash_history but ~/.zsh_history
    – Shiva
    Mar 23, 2021 at 15:09
31

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

cat ~/.zsh_history
2
  • 7
    This is the one. This returns the complete history, where as "history" only returns the current session history. M1 Mac. Jun 1, 2021 at 12:29
  • Well, you'll find your zsh history in ~/.zsh-history, and that will probably be what you want. You probably won't find your bash history in ~/.zsh-history unless you've done some very odd things with your configuration. (bash and zsh are both shell programs. bash used to be the default, but Apple switched to zsh being the default. bash is still installed and, if you use it, its history will still be in ~/.bash_history)
    – 8bittree
    May 19, 2023 at 15:56
5

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`
4

per https://apple.stackexchange.com/questions/423463/how-to-display-the-full-command-history-in-terminal -

history 0

Will output the full history

2

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  
1
  • 3
    be aware that running source ~/.bash_sessions/*.history executes all the commands
    – panchicore
    Mar 21, 2018 at 8:08
0

history ~ works for me on Ventura 13.4.1.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 5, 2023 at 13:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .