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.
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.
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.
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.
Don’t forget to delete the backup/bad file from the machine that you created in first step.
You may also like:- Top 40 Useful Linux Command Snippets
- Essential Commands For Process Management in Kali Linux
- A Comprehensive Guide to File System Commands in Linux
- Essential File Compression Commands in Linux
- Secure Shell (SSH) Protocol – A Comprehensive Guide
- Monitoring Active Connections in Kali Linux Using Netstat
- Manage Time and Date in Linux with timedatectl
- How to Add a User to Sudoers on Ubuntu
- 25 Popular Linux IP Command Examples
- Top 11 Nmap Commands for Remote Host Scanning