Bash history: how to display the timestamp (date / time) when executing each command

Want to add a timestamp (date and time) next to each of your commands Smash history ? This can be done by using HISTTIMEFORMAT Bash variables.
Bash keeps a history of the commands you type and can be accessed by typing history . By default, you see a number followed by the command you recently used:

                        $ history

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

With the help of HISTTIMEFORMAT Bash variables, you can display the date and time each command was executed. This is useful in a variety of situations, including remembering which commands were run during a specific time frame, undoing various actions, and more.
It’s worth noting that if this variable is set, timestamps will be written to the history file, so they will be retained in the shell session. Therefore, when you enable it for the first time, you will not see the correct date and time of the previously used command. Another Bash history enhancement you may like: HSTR enables you to search Bash or Zsh command history Easy Set Bash history to show the timestamp of the command history with the following command (only for current terminal session):

                        HISTTIMEFORMAT="%F %T "

The command applies only to this session, so you can see what it looks like and you can choose to configure the date and time format (see below).
Enter now history And you should see the timestamp of the Bash history command:

                        $ 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 have exported HISTTIMEFORMAT With the following timestamps:

  • %F : Full Date (dd / mm / yy)
  • %T : Time (hour: minute: second)

Followed by a space %T And before " , So in the Bash history, there is a gap from the command execution to the command execution to the command itself. Without it, the two would not be separated.
You can customize the date and time. Adopt HISTTIMEFORMAT="%Y/%m/%d %T " Display the date as year (4 digits)-month-day and time as hour (00..24): minutes: seconds Display the date as month / day / year (2 digits) %m/%d/%y . Want to display the time as 00..12 and then AM / PM instead of 00..24? Adopt %I:%M:%S %p As time format. See Date command For how to further format the date and time. It’s time to export HISTTIMEFORMAT From your ~/.bashrc File to make it the default file for all new terminal sessions for users. You can open your ~/.bashrc Paste the file with a text editor export HISTTIMEFORMAT="%F %T " (Or other date and time format) at the bottom and 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 as it adds export HISTTIMEFORMAT="%F %T " to ~/.bashrc Every time you run it, all that remains is the purchase ~/.bashrc File so that the current terminal session uses the new settings (or you can open a new terminal and it will automatically select the new terminal HISTTIMEFORMAT Settings):

                        source ~/.bashrc

Remember: the first time you enable timestamps for Bash history, you won’t see any dates / times of previously used commands. Only commands executed after timestamping is enabled for Bash history will display the date and time at the beginning of the line. I think you will also like: z.lua- the way to change directories faster (cd command, you can learn it as you use it)


Related Posts