Skip to main content
Tips

Enable Timestamp in History Command in Linux

You can easily see which commands you ran in the past. But do you know when did you run it? The history command can tell you that as well.

Sagar Sharma

You are familiar with the history command in Linux. It lets you see which commands you ran in the past.

But there is one issue. By default, the history command does not show when the command was executed (with date and time).

This could be helpful in some cases to know the time when a certain command was executed last.

And in this quick tip, I will show you how you can enable timestamps in the history command.

Enable timestamp in history command

To enable timestamps in the history command, you have to export the HISTTIMEFORMAT environment variable.

Use the export command like this:

export HISTTIMEFORMAT="%F %T "

Here,

  • %F will show the date in YYYY-MM-DD format.
  • %T will show time in HH:MM:SS format.

The modifications should now be reflected when you use the history command again:

enable timestamp in history command
History now shows the timestamps of the commands

However, as the modifications are only effective during the current session, the history command will return to its previous appearance after a reboot.

And if you want to apply timestamp permanently, here's how you do it.

Enable history timestamp permanently

To have a timestamp in the history command permanently, you'll have to make changes in .bashrc file.

Yes, no surprises there. If you want to make an environment variable permanent, it goes in your bashrc.

Open the .bashrc file in your favorite text editor. I am using Nano here.

nano ~/.bashrc

You can use Alt + / to jump to the end of the file in Nano and add the following line:

export HISTTIMEFORMAT="%F %T "
enable timestamp in history command permanently

Save the changes and exit nano.

Now, to take those changes into effect, you will have to source the .bashrc file using the source command:

source ~/.bashrc

And now, whenever you will use the history command, it will show the timestamps with every command.

Wrapping Up

This was a quick tutorial on how you can enable timestamps in the history command. I hope you find this quick history command tip helpful in your Linux journey. Here are a few more.

5 Simple Bash History Tricks Every Linux User Should Know
Effectively using bash history will save you plenty of time in the Linux terminal.

Do leave a comment if you have any doubts or suggestions.

Sagar Sharma