How to display the timestamp in your bash history

Jeremy Jeremy (12)
0

It's nice to have quick access to your command history on the command line, but sometimes it's valuable to know exactly when you typed a command. This guide will show you how to customize the output of the history command.

Posted in these interests:

bash
PRIMARY
6 subscribers
linux
81 subscribers
sysadmin
19 subscribers

Right from the command line you can set the HISTTIMEFORMAT variable that determines the format of your history.

To set this variable simply type:

HISTTIMEFORMAT="%d/%m/%y %T "

Now check your history:

history

You should see an output like this:

 2027  20/08/15 14:32:05 top -u
 2028  20/08/15 14:32:16 ps -ax|grep goats
 2029  20/08/15 14:32:17 uptime
 2030  20/08/15 14:32:22 HISTTIMEFORMAT="%d/%m/%y %T "
 2031  20/08/15 14:32:24 history

You can set this variable in your .bashrc file so that it takes effect during every shell session.

Open .bashrc to edit:

vim ~/.bashrc

And add the following:

export HISTTIMEFORMAT="%d/%m/%y %T "

Now save, quit, and source this file:

source ~/.bashrc

Here's a bit more on HISTTIMEFORMAT from gnu.org documentation: If this variable is set and not null, its value is used as a format string for strftime to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be preserved across shell sessions. This uses the history comment character to distinguish timestamps from other history lines.