Skip to content

Instantly share code, notes, and snippets.

@musale
Last active April 25, 2024 03:16
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save musale/751cfb132fe6ad05d3a5cc306d72465c to your computer and use it in GitHub Desktop.
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

@akash-basavaraju
Copy link

Thanks

@szkamens
Copy link

Thanks for the pseudo

@rayXkid
Copy link

rayXkid commented Jan 5, 2021

Thanks

@DausLokal
Copy link

thanks bro

@ediljeberson
Copy link

Thank's for the cmd's

@premanuj
Copy link

Thanks

@F13X12
Copy link

F13X12 commented Aug 29, 2021

nice :)

@michahell
Copy link

still works! 👍

@joaofranciscopiresluiz
Copy link

tanks so much

@tinspham209
Copy link

Thanks

@RayanneFerreiraRibeiro
Copy link

Muchas gracias !

@LikDev-256
Copy link

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

Had to tweak in Manjaro
If someone need it.

@abzrg
Copy link

abzrg commented Nov 30, 2021

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

muito obrigado

@imsaxenashivam
Copy link

Thanks sir ,It was very helpful

@ShrishailSGajbhar
Copy link

Thanks.

@hackerkamal
Copy link

thanks .. it was helpful

@makamo
Copy link

makamo commented Jun 9, 2022

thx

@git-rafi
Copy link

thanks

@lucas-code42
Copy link

Thanks bro!

@bala0406
Copy link

thanks!

@Blackspiritlove
Copy link

right on

@strwbzzy
Copy link

strwbzzy commented Dec 3, 2022

thanks a lot <3

@z2687737
Copy link

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

Thanks bro 💪

@Ivcannons00420
Copy link

works in 2023 thanks

@msucevan
Copy link

Thanks!

@CharlesNkdl
Copy link

Still works !

@Arios509
Copy link

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

@astyd256
Copy link

🔥

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