126

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?

gak
  • 8,737

2 Answers2

215

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
Flow
  • 1,428
gak
  • 8,737
  • 12
    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 05 '16 at 05:15
  • 3
    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
  • 3
    @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 http://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 04:51
  • in my case the file name was .zhistory make a note of that before copy-pasting. – markroxor Feb 20 '18 at 05:32
  • I kind of got curious about what these commands actually do. I've updated the main answer but added more details here – blr Apr 07 '18 at 23:56
  • This caused me to lose my entire history :\ – nlml Jan 25 '19 at 10:27
  • @nlml Just move the .zsh_history_bad file back, and maybe edit the file directly to see what's wrong with it, like the answer from @aksh1618 below. – gak Jan 25 '19 at 21:10
  • already +1'd some time ago, but please be careful, this is only working when you're not using the EXTENDED_HISTORY option, which writes also timestamps in the history file: : 1546079587:0;foo bar baz – mpy Feb 19 '19 at 08:24
  • Does someone know why does it happen? I had this issue twice a week. – Rajesh Chaudhary Jul 09 '20 at 05:47
  • strings was not available in my Ubuntu distro. I had to install it by running apt install binutils. Putting it here in case someone else ran into this same problem. – NMS Jul 29 '20 at 02:10
  • isn't it wiser to use ~/.zsh_history instead of .zsh_history? – alper Jan 23 '21 at 15:51
30

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)

aksh1618
  • 442