CentOS / RHEL : How to get the date and time of executed command in the history command output

The default history command output is always sorted by date unless there are any changes made to the configuration. The output is not in date order because session writes their history at different times. History file is written when the session gets over. Follow the steps given below to the date of execution in the history command output.

The default output of history command is as shown below without the date and time details:

  195  ls
  196  uname -a
  197  cat /etc/redhat-release

Getting date and time of executed command in the history command output

1. Edit file /etc/bashrc and add the below entry. Make sure to have the space after %T.

# vi /etc/bashrc
export HISTTIMEFORMAT='%F %T  '

2. Login in to the system.

# su - [username]

3. Check if variable has been exported.

# echo $HISTTIMEFORMAT

4. Type history command and check if time is getting displayed.

# history

5. To have output sorted according to date run on use the sort command along with the history command.

# history | sort -n
  195  2017-09-11 10:34:42 ls
  196  2017-09-11 10:34:43 uname -a
  197  2017-09-11 10:34:48 cat /etc/redhat-release 
Related Post