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?
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?
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
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)
fc -R .zsh_historymean? I've triedman fcand it tells me thatNo manual entry for fc, and neither could thefc -htell me more than the usage. Thanks. – kenshinji Aug 05 '16 at 05:15-R -- read history from file– Jeffrey Lebowski Aug 23 '16 at 11:41fcdocumentation, seeman 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.zhistorymake a note of that before copy-pasting. – markroxor Feb 20 '18 at 05:32.zsh_history_badfile 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:10EXTENDED_HISTORYoption, which writes also timestamps in the history file:: 1546079587:0;foo bar baz– mpy Feb 19 '19 at 08:24stringswas not available in my Ubuntu distro. I had to install it by runningapt install binutils. Putting it here in case someone else ran into this same problem. – NMS Jul 29 '20 at 02:10~/.zsh_historyinstead of.zsh_history? – alper Jan 23 '21 at 15:51