MQ-Jr
MQ-Jr
unixcop Admin

Set Date and Time for Each Command You Execute in Bash History

Hi guys, In this article, we will show you how you can configure time stamp information when each command in the history was executed to be displayed.

All commands executed by Bash on the command line are stored in history or in a file called ~/.bash_history.

Also you can list all of the commands executed by users on the system or a user can view the command history using the history command as shown below.

history

As shown above, the date and time  is not provided when a command was executed . This is the default setting on most if not all Linux distributions.

- Advertisement -

In computing, various shells maintain a record of the commands issued by the user during the current session.

  • The history command works with the command history list. When the command is issued with no options, it prints the history list.
  • Users can supply options and arguments to the command to manipulate the display of the history list and its entries. The operation of the history command can also be influenced by a shell’s environment variables.

For example, an environment variable can be set to control the number of commands to retain in the list.

NOTE: The date and time associated with each history entry can be written to the history file, marked with the history comment character by setting the HISTTIMEFORMAT variable.

Set Date and Time for Each Command You Execute in Bash History

  • Set HISTTIMEFORMAT variable temporarily through command line:
export HISTTIMEFORMAT='%F %T'
  1. %F – expands to full date same, as %Y (year)-%m(month)-%d(day) 
  2. %T – expands to time. the same as %H:%M:%S (hour:minute:seconds).
  • Then check the command history again
history
Bash history
Bash history
  • Also to set this variable permanently, open the file ~/.bashrc
  • then add the below lines:
- Advertisement -
export HISTTIMEFORMAT='%F %T'
  •  Then run the command below to effect the changes on the ~/.bashrc file
source ~/.bashrc

Conclusion

That’s all…

In this small article, we showed you how to set date and time for each command you execute in Bash History.

history man page

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook