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.
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:
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 "
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.
Do leave a comment if you have any doubts or suggestions.
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.