Bash History: How To Show A Timestamp (Date / Time) When Each Command Was Executed

Timestamps Bash history

Want to add a timestamp (date and time) next to each command from your Bash history? This can be done by using the HISTTIMEFORMAT Bash variable.

Bash keeps a history of the commands you type, which can be accessed by typing history. By default you see a number followed by the commands you've used recently:

$ history

1889  cd Cloaker/
1890  ./Cloaker.run 
1891  sudo apt upgrade
1892  sudo apt autoremove
1893  history

With the help of the HISTTIMEFORMAT Bash variable you can show the date and time when each command was executed. This can be useful in various occasions, including to remember which commands you ran in a specific time-frame, to undo various operations, and so on.

It's worth noting that if this variable is set, the time stamps are written to the history file, so they are preserved across shell sessions. So the first time you enable it, you won't see the correct date and time for your previously used commands.

Another Bash history enhancement you might like: HSTR Makes Searching Your Bash Or Zsh Command History Easy

Set the Bash history to show a timestamp for your command history (for the current terminal session only) by using this command:

HISTTIMEFORMAT="%F %T "

This command is only for this session, so you can see how it looks and optionally configure the date and time format (see below).

Now type history and you should see timestamp for your Bash history commands:

$ history

2027  2019-07-12 13:02:31 sudo apt update
2028  2019-07-12 13:02:33 history
2029  2019-07-12 13:03:35 HISTTIMEFORMAT="%F %T "
2029  2019-07-12 13:03:38 man date
2030  2019-07-12 13:03:55 history

We've exported HISTTIMEFORMAT with the following timestamp:

  • %F: full date (year-month-date)
  • %T: time (hour:minutes:seconds)

There is a space after %T and before ", so that there's a space in your Bash history between the time the command was executed and the command itself. Without it, the two wouldn't be separated.

You can customize the date and time. Use HISTTIMEFORMAT="%Y/%m/%d %T " to show the date as year(4 digits)-month-day, and time as hour(00..24):minutes:seconds. Show the date as month/day/year(2 digits) with %m/%d/%y. Want to show the time as 00..12 followed by AM / PM, instead of 00..24? Use %I:%M:%S %p as the time format. See the date command for how to further format the date and time.

It's now time to export HISTTIMEFORMAT from your ~/.bashrc file to make it default for all new terminal sessions for your user. You can either open your ~/.bashrc file with a text editor and paste export HISTTIMEFORMAT="%F %T " (or some other date and time format) at the bottom, then save the file, or you can run this command to write it to your ~/.bashrc:

echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc

Run this command only once, because it adds export HISTTIMEFORMAT="%F %T " to ~/.bashrc each time you run it.

After this, all that's left is to source the ~/.bashrc file so the current terminal session uses the new settings (or you can open a new terminal and it will automatically pick up the new HISTTIMEFORMAT settings):

source ~/.bashrc

Remember: the first time you enable timestamps for your Bash history, you won't see any date / time for your previously used commands. Only commands executed after you enable timestamps for your Bash history will show a date and time at the beginning of the line.

I think you'll also like: z.lua - A Faster Way Of Changing Directories (cd Command That Learns As You Use It)