[Solution] Fix and Recover a corrupt history file in zsh – Linux

If you ever see a message like this upon opening a new shell – “zsh: corrupt history file /root/.zsh_history“. You can easily fix your history file by extracting only the valid strings.

zsh history fix error techhyme

The first step is to move the original .zsh_history file to another renamed file (.zsh_history_backup). It simply move your original file into another.

zsh history fix error techhyme

Next write all printable strings into a new .zsh_history file by typing the following command:

strings .zsh_history_backup > .zsh_history

It seems strings is a nice way to actually get the printable characters in a file. In our case, this means if the .zsh_history was corrupted due to non-printable characters, this will help parse them out. It also means if you ran any commands with non-printable characters, you’ve now lost them from your history as well.

zsh history fix error techhyme

Reload the history by using fc command as shown below:

fc -R .zsh_history

In this case, we are reading from the file and, since no second file was mentioned, putting it back into the same history file. i.e. re-writing history by parsing the same file.

zsh history fix error techhyme

Don’t forget to delete the backup/bad file from the machine that you created in first step.

Related Posts

File Permissions Linux Techhyme

Understanding Common File Permissions in Linux

File permissions are a fundamental aspect of file management in Unix-based operating systems like Linux. They play a crucial role in determining who can access, modify, or…

Linux System Key Components Techhyme

7 Major Key Components That Make Up A Linux System

Linux, an open-source operating system, has become one of the most widely used platforms due to its stability, security, and flexibility. It is comprised of various components…

File Finding Commands Linux Techhyme

Top 4 File Finding Commands in Linux

In the vast world of Linux, one of the most common tasks is finding files and directories scattered throughout the file system. Fortunately, Linux provides several powerful…

Informational Commands Linux Techhyme

Top 9 Essential Informational Commands in Linux

In the world of Linux, knowledge is power, and understanding the status of your system and its processes is crucial for efficient management and troubleshooting. Thankfully, Linux…

Linux File Tree Techhyme

Exploring the Linux File Tree: A Comprehensive Guide

In the world of operating systems, Linux stands out as a powerful and versatile option, favored by system administrators and users alike. One of the distinctive features…

Bash Shell Variables Techhyme

12 Most Commonly Used Bash Shell Variables

In the Linux shell environment, variables play a vital role in controlling and customizing the behavior of the system. Shell variables are placeholders that store information such…

Leave a Reply