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?

up vote 152 down vote accepted

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
fc -R .zsh_history
  • 7
    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
  • Anyone knows what this error could have been caused by? – hugronaphor Jan 15 at 21:31
  • 1
    @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 at 20:59
  • 1
    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 at 4:51

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)

  • I also had a bunch of @s in one line. I removed those and voila, the error is gone! – illusionist Jul 10 at 3:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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