120

After a reboot, I started seeing a message when loading the shell:

zsh: corrupt history file /home/myusername/.zsh_history

How can I recover from this situation and potentially recover some of the history?

202

Found a blog post describing a fix that appears to work for me, while restoring my missing history:

mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history

Afterwards, you may want to instruct zsh to re-read the history form the recovered history file

fc -R .zsh_history
13
  • 11
    May I ask what does the command fc -R .zsh_history mean? I've tried man fc and it tells me that No manual entry for fc, and neither could the fc -h tell me more than the usage. Thanks. – kenshinji Aug 5 '16 at 5:15
  • 2
    For more details about the fc command, see here and here. The -R option is not present there, but zsh completion lists, among the options, -R -- read history from file – Jeffrey Lebowski Aug 23 '16 at 11:41
  • 1
    Anyone knows what this error could have been caused by? – hugronaphor Jan 15 '18 at 21:31
  • 2
    @hugronaphor In my case it was that I had to do a hard shutdown of my laptop. This caused some issues for me as I did this during a disk write operation and one of the things that got corrupted was my zsh file among other things. – RyanNerd Jan 26 '18 at 20:59
  • 2
    For the correct fc documentation, see man zshbuiltins, also available at zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html. As suggested by Jeffrey, "‘fc -R’ reads the history from the given file". – Martin Jan 29 '18 at 4:51
27

Simply removing random characters may also work:

  • vim .zsh_history

  • Remove any strange characters, which would most probably be near the end. (In my case I had a string of @ in the second last line, following a forced shutdown)

  • :x (save and exit)

2
  • Of course, you can also use any other editor like ` emacs` or ` nano` … 😜 – rugk Jun 13 '20 at 13:10
  • 1
    @rugk Over my dead body. /s – aksh1618 Jun 14 '20 at 9:56

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