Skip to content

Instantly share code, notes, and snippets.

@muendelezaji
Forked from op/bash-history-to-zsh-history.py
Created October 5, 2016 14:18
Show Gist options
  • Save muendelezaji/c14722ab66b505a49861b8a74e52b274 to your computer and use it in GitHub Desktop.
Save muendelezaji/c14722ab66b505a49861b8a74e52b274 to your computer and use it in GitHub Desktop.
Convert Bash history to Zsh history
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This is how I used it:
# $ cat ~/.bash_history | python bash-to-zsh-hist.py >> ~/.zsh_history
import sys
import time
def main():
timestamp = None
for line in sys.stdin.readlines():
line = line.rstrip('\n')
if line.startswith('#') and timestamp is None:
t = line[1:]
if t.isdigit():
timestamp = t
continue
else:
sys.stdout.write(': %s:0;%s\n' % (timestamp or time.time(), line))
timestamp = None
if __name__ == '__main__':
main()
@fbraz3
Copy link

fbraz3 commented Mar 11, 2020

OSX Catalina, works fine! =D
Python 2.7.16

@Rezwanul-Kabir
Copy link

Nice!! Worked beautifully!

@Kl0ven
Copy link

Kl0ven commented Jun 18, 2020

Thanks !!!! worked on WSL - python3.8

@chris-dare
Copy link

You're awesome @muendelezaji. Thanks for this. Worked so well.

@ahmedhr
Copy link

ahmedhr commented Oct 6, 2020

Works!!

@mc256
Copy link

mc256 commented Dec 10, 2020

awesome!

@evanjt
Copy link

evanjt commented Feb 8, 2021

Great, thanks :)

@mrkeuz
Copy link

mrkeuz commented Jun 22, 2021

Armbian 21.02.3 Focal (Orange Pi) worked! Thanks!

@piranha87
Copy link

works well on ubuntu 20.04

@bernardolm
Copy link

🙏

@blooddrainer
Copy link

Works fine on Manjaro! Thanks!

@gunturaf
Copy link

THANK YOU FOR THIS!!!

@au8st
Copy link

au8st commented Nov 18, 2021

Working on Debian 11 (stock) python, either run the script as:

cd ~/ && cp .bash_history .bash_history.bak && cat ~/.bash_history | python3 bash-to-zsh-hist.py >> ~/.zsh_history

Or, symlink python3 > python:

sudo ln -s /usr/bin/python3.9 /usr/bin/python
cat ~/.bash_history | python bash-to-zsh-hist.py >> ~/.zsh_history

@PeterPerhac
Copy link

nice, worked like a charm on my new MacBook Pro. 👍 thank you

@RicHincapie
Copy link

Perfect. Thanks!

@mbency
Copy link

mbency commented Jan 9, 2022

thanks, worked on a new M1 MBP

@devrimbaris
Copy link

Thanks, worked on Catalina with python 2.7.16

@Zackhardtoname
Copy link

To get it to work with hstr, I had to use int(time.time())
Thanks for saving my history!!

@mzpwr
Copy link

mzpwr commented Jun 1, 2022

On WSL had pretty big history and was using HSTR, so after importing the history had 2 problems:

  1. Error zsh: corrupt history file /home/user/.zsh_history
  2. HSTR showing timestamps
    So the solution for me was modified script with thanks to @Zackhardtoname and then several additional steps:
  3. Write date to some other file .zsh_history_tmp
  4. strings .zsh_history_tmp > .zsh_history
  5. fc -R .zsh_history

@Tamas-Toth-ebola
Copy link

Works fine in Ubuntu 22.04, thanks for it!

@muriloloffi
Copy link

Works fine in Linux Mint Debian Edition 5 (LMDE 5). Thank you.

@thizmo
Copy link

thizmo commented Oct 5, 2022

Works fine on RockyLinux 9 (Workstation) with zsh 5.8, ohmyzsh and python3.9.10.
Many thx. Great timesaver!

@johnflan
Copy link

Merci!

@attson
Copy link

attson commented Dec 8, 2022

if zsh_history format is
: 1670471184:0;cat ~/.zsh_history
need use int(time.time()) at line 21

@ZhymabekRoman
Copy link

Thanks!

@dhensen
Copy link

dhensen commented Mar 30, 2023

Thanks

@pedrobartiaga
Copy link

gracias!!!

@anthonyray
Copy link

Worked for me, thanks.

@joseslneto
Copy link

Thanks!!! Helped a lot!

@omkardeekonda
Copy link

Worked like a charm !! Thank you 👍
Wondering why zsh framework doesn't have a feature to import the bash history as part of initial setup. 🤔

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