1

I have been working with two terminal windows in my Mac (dev/prod), both had shown the commands and results history (scrolling up) for the last 6 months of work, which was very helpful to run periodic commands and to check past errors.

Yesterday I turn off my mac and closed both terminal windows manually, but today when opening the terminal it has no history at all. There is a way to recover the windows with all the history for past commands and their results?

I know there is a .bash_history file but it only shows commands typed but not the results.
Thanks in advance.

1
  • As you mentioned only command history is stored in bash_history. Their responses are not stored anywhere. Do you use time machine ? If yes, maybe give a try to go back few days to see if you can see this history.
    – manishg
    Commented Feb 11, 2017 at 23:37

2 Answers 2

2
+50

Bash's history only store executed commands, not their output, and only a limited number of them (usually 500, as defined by environment variable HISTFILESIZE). This won't help in your situation.

From what I can see, it appears that Terminal store save window's state (including console history) inside directory /Users/<user>/Library/Saved Application State/com.apple.Terminal.savedState/. Files in this directory are modified in real time whenever new events occurs in the terminal window, and unless I am mistaken, should be included in Time Machine backups. Therefore, it seems that if you can restore files in this directory from some former backup, you should get back your history. You could even try some "file undelete" tools in that directory, though these tools are rather rare on OS X.

The procedure for this should be that you first quit Terminal, then restore the whole directory (for example using Time Machine), then simply launch Terminal. These saved state files use a custom binary format, that you can't be read otherwise than by the Terminal program itself.

By the way, it might be worth mentioning that you can, at anytime, save the content of a Terminal window to a text file, from the Shell menu. You might consider doing it periodically, in the future, given that your terminal's history appears to have some significant value...

4
  • Thanks. Do you know how to load a 'window.data' file in a terminal window or any other application? I have found 3 files in the local directory you mentioned ('data.data', 'window_1.data', 'window_4.data'). I noticed anytime I open a new terminal window a new 'window_X.data' file is created and it is removed when I close that terminal. Since I only have 1 terminal window open now and there are 3 data files, maybe the other ones have my previous history.
    – Ruben
    Commented Feb 15, 2017 at 19:25
  • These data files appears to use a custom binary format. I have made some attempt at reverse engineering it, but had no success. I do not recognize a common compression scheme. There is no parser that I know of for it.
    – James
    Commented Feb 15, 2017 at 21:14
  • The only way I know to effectively load one of those file is to have it there, and launch Terminal...
    – James
    Commented Feb 15, 2017 at 21:15
  • I have solid reasons to believe that the data files are produced using a subclass of Apple's NSCoder, probably some "Archive" coder. If you are willing to write some Objective C or Swift code, you might have some success decoding the files. Should you want to investigate this way, search Google for unarchiveObjectWithData...
    – James
    Commented Feb 15, 2017 at 21:53
1

if using zsh check your /etc/zshrc for the history file location, mine has

# Save command history
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=2000
SAVEHIST=1000

also check your ~/.zshrc incase the defaults for these have been changed

Not the answer you're looking for? Browse other questions tagged or ask your own question.