I have a file with my bash history and want to "import" the commands on a new machine. The file contains a history output formatted as HISTTIMEFORMAT="%F%t%T%t":

$ history > history.txt

So the result is:

    1  2018-01-01   17:56:36    foo
    2  2018-01-01   18:01:21    bar
    3  2018-01-01   18:02:02    buz

When I replace the ~/.bash_history with the content of this file, the counter and the date&time become part of the command. So the output of the history looks then like

    1  2018-06-13   18:10:15    1  2018-01-01   17:56:36    foo
    2  2018-06-13   18:10:15    2  2018-01-01   18:01:21    bar
    3  2018-06-13   18:10:15    3  2018-01-01   18:02:02    buz

I cleaned up the commands list before importing it. Just opened it in a text editor and replaced "^[\s]+[0-9]+[\s]+[0-9]{4}-[0-9]{2}-[0-9]{2}[\s]+[0-9]{2}:[0-9]{2}:[0-9]{2}[\s]+" by an empty string. Looks better. But then I lose the information about the date&time of the commands:

    1  2018-06-13   18:10:15    foo
    2  2018-06-13   18:10:15    bar
    3  2018-06-13   18:10:15    buz

How to import the history and keep the date&time meta information?

  • 2
    Why not just scp .bash_history user@newhost:.bash_history?
    – DopeGhoti
    Jun 13, 2018 at 16:35
  • @Jeff Schaller In my case I didn't have the original .bash_history file anymore. So I had to start by an export file. See the solution.
    – automatix
    Jun 13, 2018 at 17:39
  • Just in case you wanted to @ ping @DopeGhoti instead of me...
    – Jeff Schaller
    Jun 13, 2018 at 18:35

2 Answers 2


The output of history is for you to read. Actually If you look into your ~/.bash_history file, you will find something like

vim .bash_history 
man history

So that's how the timestamps are stored.

Same as DopeGhoti, why not scp ?


@Charles Thanks a lot for the valuable hint! It leads me to the solution.

In order to manipulate the date&time of the bash history entries, I just have to replace the ~/.bash_history with a correctly formatted raw file. The correct format for an entry is


So in my case I needed to make following steps:

(step 0: Export the history into a text file: history > history.txt.)

step 1: Open the file in a text editor and clean up the file. The result should be, that every row consist of date + space time + tab + command. Since in my case the entries were formatted as %F%t%T%t, I needed this regex-based replacement:

FROM: ^([\s]+[0-9]+[\s]+)([0-9]{4}-[0-9]{2}-[0-9]{2})[\s]+([0-9]{2}:[0-9]{2}:[0-9]{2}[\s]+)
TO: $2 $3

step 2: Copy the result to Calc (or Excel). Now we have two columns.

step 3: Convert the values in the date&time column to timestamps:

=(A1 - DATEVALUE("1/1/1970"))*86400

step 4: Copy the columns with the timestamps and the commands back to the text editor.

step 5: Convert the result to the target format:

FROM: ^([0-9]{10})\t
TO: #$1\n

step 6: Replace the ~/.bash_history by the result text file:

$ cp history.txt > ~/.bash_history
  • That's an incredibly roundabout way of just copying the .bash_history that already exists onto the new host which was suggested twice. Why put yourself through this?
    – DopeGhoti
    Jun 13, 2018 at 17:38
  • @DopeGhoti Your're right, but In my case I didn't have the original .bash_history file anymore. So I had to start by an export file.
    – automatix
    Jun 13, 2018 at 17:40
  • That was not at all clear as in each case you describe starting with exporting the existing history with the history command.
    – DopeGhoti
    Jun 13, 2018 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .