Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Import bash history to zsh history.
#################################################################
# = This script transfers bash history to zsh history
# = Change bash and zsh history files, if you don't use defaults
#
# = Usage: ruby bash_to_zsh_history.rb
#
# = Author: Ankit Goyal
#################################################################
# change if you don't use default values
BASH_HISTORY_FILE_PATH="#{ENV['HOME']}/.bash_history"
ZSH_HISTORY_FILE_PATH="#{ENV['HOME']}/.zsh_history"
# Read the bash history file
bash_hist_file = File.read(BASH_HISTORY_FILE_PATH)
# Get the list of commands from bash history hile
command_list = bash_hist_file.split("\n")
# Open the zsh history file
zsh_hist_file = File.open(ZSH_HISTORY_FILE_PATH, "a")
# Get timestamp required for zsh history file format and update the history file
time = Time.now.to_i
command_list.each do |command|
time += 1
zsh_hist_file.write(": #{time}:0;#{command}\n")
end
# Close the file
zsh_hist_file.close
@Janitza-AlAt

This comment has been minimized.

Copy link

@Janitza-AlAt Janitza-AlAt commented Oct 26, 2016

Thx the script is fast and good

@agoree407

This comment has been minimized.

Copy link

@agoree407 agoree407 commented Nov 1, 2016

Worked phenomenally, thank you!

@AmbreB

This comment has been minimized.

Copy link

@AmbreB AmbreB commented Dec 1, 2016

Just what I was looking for ! Thank you !!

@toleabivol

This comment has been minimized.

Copy link

@toleabivol toleabivol commented Dec 19, 2016

Thanks !

@osahner

This comment has been minimized.

Copy link

@osahner osahner commented Mar 16, 2017

Perfect!

@vonHabsi

This comment has been minimized.

Copy link

@vonHabsi vonHabsi commented Apr 29, 2017

This command seems to ignore the timestamps in the ~/.bash_history file?

@victorhcm

This comment has been minimized.

Copy link

@victorhcm victorhcm commented Jun 26, 2017

I'm getting:

zsh: corrupt history file /home/user/.zsh_history

Did anyone else have this issue?

My .bash_history has 23k lines, I think this might be an issue. I tried to increase ZSH history limit and rerun the converter, but still didn't work.

EDIT: Solution:

mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history

I tried this, but I think it was before I increased ZSH history limit, so it was mangling my history to 10k lines. From my .zshrc:

HISTSIZE=1000000  # how many lines of history to keep in memory
SAVEHIST=1000000  # how many lines to keep in the history file
@alejoberman

This comment has been minimized.

Copy link

@alejoberman alejoberman commented Aug 9, 2017

Works fine and fast! Thank you!

@leggiero

This comment has been minimized.

Copy link

@leggiero leggiero commented Aug 9, 2017

Awesome script! Worked like a charm. Thank you for sharing!

@objectuser

This comment has been minimized.

Copy link

@objectuser objectuser commented Sep 1, 2017

Thanks! 🥇

@jjinking

This comment has been minimized.

Copy link

@jjinking jjinking commented Nov 24, 2017

Thank you! For anyone else who uses this, if you get utf-8 errors, try upgrading ruby

@kietoparao

This comment has been minimized.

Copy link

@kietoparao kietoparao commented Apr 8, 2018

Thank you for the script, it worked!

Only thing I had to modify is the time = Time.now.to_i line of your script, because it was giving me future times for the commands transfered from the old ~/.bash_history:

19070* 2018-04-08 15:45  git pull
19071* 2018-04-08 15:45  makepkg -sci    # <--- old .bash_history
19072* 2018-04-08 12:55  bash            # <--- new .zsh_history
19073* 2018-04-08 12:52  echo $SHELL

Just changing it to time = Time.now.to_i - (nr. of lines in old .bash_history) (e.g.: time = Time.now.to_i - 11469) fixed this and now I get the following view with history from zsh:

10534  2018-04-08 12:58  git pull
10535  2018-04-08 12:58  makepkg -sci    # <--- old .bash_history
10536  2018-04-08 13:14  bash            # <--- new .zsh_history
10537  2018-04-08 13:14  echo $SHELL
@szotsaki

This comment has been minimized.

Copy link

@szotsaki szotsaki commented Apr 17, 2018

If you have invalid UTF-8 sequences in your .bash_history, just remove those characters with iconv like

iconv -f UTF-8 -t UTF-8//IGNORE .bash_history > .bash_history-utf8
@tkrishnanite

This comment has been minimized.

Copy link

@tkrishnanite tkrishnanite commented Aug 14, 2018

Thank you.

@bigplum

This comment has been minimized.

Copy link

@bigplum bigplum commented Oct 11, 2018

one line command: awk '{print " :0:;"$1}' .bash_history > .zsh_history

@gimpf

This comment has been minimized.

Copy link

@gimpf gimpf commented Dec 2, 2018

one line command: awk '{print " :0:;"$1}' .bash_history > .zsh_history

Almost... $1 needs to be $0, or there won't be any args, and > should be >>, to append instead of replace. Also, at least for my history config, there's a : missing at the beginning. Finally, I'd not import duplicates, ergo sort ~/.bash_history | uniq | awk '{print ": :0:;"$0}' >> ~/.zsh_history.

@ldakhoa

This comment has been minimized.

Copy link

@ldakhoa ldakhoa commented Mar 2, 2019

Thanks, it's work

@CNG

This comment has been minimized.

Copy link

@CNG CNG commented Mar 5, 2019

I was surprised to see the top Google results for this are Ruby and Python scripts, huh! Then I missed the awk command at the bottom here and did it with Perl. First I checked the format of both my histories:

$ head -n1 ~/.zsh_history
: 1551826967:0;ls -al ~
$ head -n2 ~/.bash_history
#1532234168
exit

Then wrote a Perl command that I ran under Bash so Zsh would not be running and conflict (from Zsh, I ran exec bash and confirmed Zsh was not running with pgrep zsh):

perl -0777 -pe 's/^#(\d+)\n(.*?)$/: \1:0;\2/gm' ~/.bash_history >> ~/.zsh_history
@panelladavide

This comment has been minimized.

Copy link

@panelladavide panelladavide commented Mar 19, 2019

Thanks, it's work fine!

@theposey

This comment has been minimized.

Copy link

@theposey theposey commented May 7, 2019

awesome dude

@tkainrad

This comment has been minimized.

Copy link

@tkainrad tkainrad commented Jun 21, 2019

Thanks, works perfectly.

@davgilso

This comment has been minimized.

Copy link

@davgilso davgilso commented Jun 21, 2019

Worked perfectly.

Worth noting that you should remember to increase your SAVEHIST value first.

@marchrius

This comment has been minimized.

Copy link

@marchrius marchrius commented Aug 29, 2019

@robertstrom

This comment has been minimized.

Copy link

@robertstrom robertstrom commented Nov 6, 2019

getting this error message - anyone have a solution?

./bash_to_zsh_history.rb: line 15: syntax error near unexpected token (' ./bash_to_zsh_history.rb: line 15: bash_hist_file = File.read(BASH_HISTORY_FILE_PATH)'

Ruby version is ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

@goyalankit

This comment has been minimized.

Copy link
Owner Author

@goyalankit goyalankit commented Nov 11, 2019

getting this error message - anyone have a solution?

./bash_to_zsh_history.rb: line 15: syntax error near unexpected token (' ./bash_to_zsh_history.rb: line 15: bash_hist_file = > File.read(BASH_HISTORY_FILE_PATH)'

Ruby version is ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

@robertstrom You probably copy-pasted the script wrong. There's an extra ' at the end of line 15.

@danielcomas25

This comment has been minimized.

Copy link

@danielcomas25 danielcomas25 commented Nov 20, 2019

Worked perfectly! Thanks!

@mscalora

This comment has been minimized.

Copy link

@mscalora mscalora commented Nov 23, 2019

Thanks bigplum & gimpf,

sort ~/.bash_history | uniq | awk '{print ": :0:;"$0}' >> ~/.zsh_history

just what I needed on a VM without ruby installed

@fharper

This comment has been minimized.

Copy link

@fharper fharper commented Dec 2, 2019

Thanks for the script

@Crow-head

This comment has been minimized.

Copy link

@Crow-head Crow-head commented Jan 19, 2020

Awesome work! Thank you!

@cellcoresystems

This comment has been minimized.

Copy link

@cellcoresystems cellcoresystems commented Jan 29, 2020

clean, simple and fast - thx

@cad-asura

This comment has been minimized.

Copy link

@cad-asura cad-asura commented Mar 18, 2020

Personally I found awk and PERL to be to heavy weight :) So I went for

cat ~/.bash_history | cut -d' ' -f1- | sort | uniq | xargs -I % echo ": $(date +%s):0;" % >> ~/.zsh_history

@frostyandy2k

This comment has been minimized.

Copy link

@frostyandy2k frostyandy2k commented Jul 1, 2020

Personally I found awk and PERL to be to heavy weight :) So I went for

cat ~/.bash_history | cut -d' ' -f1- | sort | uniq | xargs -I % echo ": $(date +%s):0;" % >> ~/.zsh_history

This worked like a charm on OSX Catalina from bash to zsh.

@r72cccp

This comment has been minimized.

Copy link

@r72cccp r72cccp commented Aug 6, 2020

Script is working for me. Ubuntu 20.20. History moved. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.