Black Friday Deals for Linux Users are Now Live!
Learn more
Nov 22, 2022 2 min read

Enable Timestamp in History Command in Linux

Table of Contents

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.

Linux Foundation Training
Sagar Sharma
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.