Skip to content

Instantly share code, notes, and snippets.

@musale
Last active May 23, 2026 21:50
Show Gist options
  • Select an option

  • Save musale/751cfb132fe6ad05d3a5cc306d72465c to your computer and use it in GitHub Desktop.

Select an option

Save musale/751cfb132fe6ad05d3a5cc306d72465c to your computer and use it in GitHub Desktop.
How to fix and recover a “corrupt history file” in zsh
# move to home directory
cd ~

# move the .zsh_history file into another .zsh_history_bad file
mv .zsh_history .zsh_history_bad

# write all printable strings into a new .zsh_history file
strings .zsh_history_bad > .zsh_history

# reload the history
fc -R .zsh_history

If you found this helpful, don't hesitate to buymeacoffee

@ediljeberson

Copy link
Copy Markdown

Thank's for the cmd's

@premanuj

Copy link
Copy Markdown

Thanks

@F13X12

F13X12 commented Aug 29, 2021

Copy link
Copy Markdown

nice :)

@michahell

Copy link
Copy Markdown

still works! 👍

@joaofranciscopiresluiz

Copy link
Copy Markdown

tanks so much

@tinspham209

Copy link
Copy Markdown

Thanks

@RayanneFerreiraRibeiro

Copy link
Copy Markdown

Muchas gracias !

@LikDev-256

Copy link
Copy Markdown
cd ~/                                                                                       
mv .zhistory .zhistory_bad
strings .zhistory_bad > .zhistory
fc -R .zhistory
exit

Had to tweak in Manjaro
If someone need it.

@abzrg

abzrg commented Nov 30, 2021

Copy link
Copy Markdown

Thanks!

You can also check the env var HISTFILE to see if the user has changed the default history file location.
Slightly less readable but more portable

#!/bin/sh
set -e

mv ${HISTFILE:-$HOME/.zsh_history} ${HISTFILE:-$HOME/.zsh_history}_bad
strings ${HISTFILE:-$HOME/.zsh_history}_bad > ${HISTFILE:-$HOME/.zsh_history}
fc -R $_

@RaphaelRodrigues-costa

Copy link
Copy Markdown

muito obrigado

@imsaxenashivam

Copy link
Copy Markdown

Thanks sir ,It was very helpful

@ShrishailSGajbhar

Copy link
Copy Markdown

Thanks.

@hackerkamal

Copy link
Copy Markdown

thanks .. it was helpful

@makamo

makamo commented Jun 9, 2022

Copy link
Copy Markdown

thx

@git-rafi

Copy link
Copy Markdown

thanks

@lucas-code42

Copy link
Copy Markdown

Thanks bro!

@bala0406

Copy link
Copy Markdown

thanks!

@Blackspiritlove

Copy link
Copy Markdown

right on

@strwbzzy

strwbzzy commented Dec 3, 2022

Copy link
Copy Markdown

thanks a lot <3

@z2687737

Copy link
Copy Markdown

thank you. problem solved.

[ # move to home directory cd ~ ]
after 2022 no more CD command?
I used [ $ sudo su > kali ] to get to the root directory

@wallace-sf

Copy link
Copy Markdown

Thanks bro 💪

@Ivcannons00420

Copy link
Copy Markdown

works in 2023 thanks

@msucevan

Copy link
Copy Markdown

Thanks!

@CharlesNkdl

Copy link
Copy Markdown

Still works !

@Arios509

Copy link
Copy Markdown

Thanks, but i wonder how does this happened? Does anyone know why?

@astyd256

Copy link
Copy Markdown

🔥

@jgproc

jgproc commented May 31, 2024

Copy link
Copy Markdown

Thanks!

@anormalmeatperson

Copy link
Copy Markdown

still works, thanks

ghost commented Aug 5, 2024

Copy link
Copy Markdown

Thanks!

@Kayo4life

Copy link
Copy Markdown

@Arios509

Thanks, but i wonder how does this happened? Does anyone know why?

Typically you didn't shut down your computer correctly. Another way you can fix this is just opening your zsh history file in a text editor and removing the corrupted parts. Corrupted parts usually look like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment