How to fix a corrupt zsh history file
Occasionally you may find you have a corrupt zsh history file preventing you from using CTRL+R or from using the `fc` command. Here's how to fix it.
Corrupt ZSH history file ¶
If you use zsh
for your shell very occasionally you may find the
following message appearing indicating a corrupt history file.
zsh: corrupt history file /home/go/.zsh_history
This prevents searching back through the history with CTRL+R
and editing
previous commands with fc
.
How to fix it ¶
To fix it run the following commands
cd ~
mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history
rm ~/.zsh_history_bad
What’s happening? ¶
- The
zsh_history
file gets corrupted somehow and the shell is unable to read it. - The corrupted file is moved to a new file
zsh_history_bad
. - The
strings
command is used to extract strings (or text) from thezsh_history_bad
file and the output is written to a new filezsh_history
. - The zsh builtin command
fc
is used to read the history from the fixedzsh_history
file. - Finally the corrupted file
zsh_history_file
can be removed. - All done!
Making it a script ¶
Once this happened more than twice I made a script to save some typing. The
following is saved in my ~/bin
folder as zsh_history_fix
and this folder is
in my $PATH
.
#!/usr/bin/env zsh
# George Ornbo (shapeshed) http://shapeshed.com
# License - http://unlicense.org
#
# Fixes a corrupt .zsh_history file
mv ~/.zsh_history ~/.zsh_history_bad
strings ~/.zsh_history_bad > ~/.zsh_history
fc -R ~/.zsh_history
rm ~/.zsh_history_bad
Now if I see the zsh: corrupt history file
error again I just run the command
get back to work.
zsh_history_fix
If you’d like the script you can download it from Github, put it somewhere
in your $PATH
and make it executable.
cd ~/bin # or somewhere in your $PATH
wget https://raw.githubusercontent.com/shapeshed/dotfiles/master/bin/zsh_history_fix
chmod +x zsh_history_fix
source ~/.zshrc
Tags
Can you help make this article better? You can edit it here and send me a pull request.
See Also
-
Linux and Unix ls command tutorial with examples
Tutorial on using ls, a UNIX and Linux command for listing directory contents. Examples of listing a directory, showing hidden files, showing long listings, sorting on various items and showing recursive listings. -
Linux and Unix tail command tutorial with examples
Tutorial on using tail, a UNIX and Linux command for outputting the last part of files. Examples of outputting the last ten lines of a file, limiting the number of lines, limiting the number of bytes, showing multiple files, watching a file for changes and using pipes. -
Linux and Unix fc command tutorial with examples
Tutorial on using fc, a UNIX and Linux command for editing and re-executing commands previously entered into an interactive shell. Examples of editing and re-executing the last command, editing and executing a previous command, setting the text editor to be used, listing previous commands and executing a command without editing it.