How can I transfer my bash history to a new system?

My bash history is my memory, it contains commands that I don't want to loose when I update to a newer version of Ubuntu or change my computer.

Is it possible to transfer the commands database to a fresh installation?


Your bash history is saved in a plain text file, called by default .bash_history.

Unless you modified the $HISTFILE variable you should find it in your home directory:

$ echo $HISTFILE
/home/sylvain/.bash_history

If you keep your home partition when updating or reinstalling your system and if $HISTFILE is saved on this partition, there's nothing else to do.

To transfer the file to a new computer, just save it on a USB drive and replace the existing .bash_history of the target system.

If HISTTIMEFORMAT was set on the "old" system, don't forget to set it again otherwise the timestamps of the new commands won't be saved as comments in the "new" .bash_history file.

In case you would like to merge both old and new with new being saved at the end of the history file, you can use this method:

sudo apt-get install moreutils
cat .bash_history.old $HISTFILE | sponge $HISTFILE