- 
            
      
        
      
    Star
      
          
          (122)
      
  
You must be signed in to star a gist  - 
              
      
        
      
    Fork
      
          
          (22)
      
  
You must be signed in to fork a gist  
- 
      
 - 
        
Save goyalankit/a1c88bfc69107f93cda1 to your computer and use it in GitHub Desktop.  
| ################################################################# | |
| # = 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 | 
Awesome script! Worked like a charm. Thank you for sharing!
Thanks! 🥇
Thank you! For anyone else who uses this, if you get utf-8 errors, try upgrading ruby
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 $SHELLJust 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 $SHELLIf 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
    Thank you.
one line command: awk '{print " :0:;"$1}' .bash_history > .zsh_history
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.
Thanks, it's work
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
    Thanks, it's work fine!
awesome dude
Thanks, works perfectly.
Worked perfectly.
Worth noting that you should remember to increase your SAVEHIST value first.
Cool! This is a node script version
https://gist.github.com/marchrius/71f858c2b032f037d286cbf2394ad8a7
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]
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.
Worked perfectly! Thanks!
Thanks bigplum & gimpf,
sort ~/.bash_history | uniq | awk '{print ": :0:;"$0}' >> ~/.zsh_history
just what I needed on a VM without ruby installed
Thanks for the script
Awesome work! Thank you!
clean, simple and fast - thx
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
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.
Script is working for me. Ubuntu 20.20. History moved. Thank you!
Guys ;)
cp ~/.bash_history ~/.zsh_history works
Probably zsh wrote some import from just cmdlines in history
Since I didn't see trusty old sed, here's one I made with my friend sed
cat ~/.bash_history | uniq | sed "s/^/\: $(date +%s)\:0;/" >> ~/.zsh_history
Since I didn't see trusty old
sed, here's one I made with my friendsed
cat ~/.bash_history | uniq | sed "s/^/\: $(date +%s)\:0;/" >> ~/.zsh_history
Works a treat. Cheers.
Thanks a lot brother.
Works fine and fast! Thank you!