History command with date and time

In this tutorial, we will setup and configure bash so that when we run history command in linux, it will display history command with date and time. This tutorial can be used for CentOS / Rhel / Arch Linux.

When we run history command, it displays output in below format where there is no date and time information available when the command was ran. History command looks for a variable called HISTTIMEFORMAT for displaying the information. Since there is no value set for HISTTIMEFORMAT , history command displays the output in below format.

sudo history
  
  449  ip addr sh
  450  systemctl stop NetworkManager
  451  ip addr sh
  452  wpa_cli 
  453  reboot 
  454  ip addr sh
  455  iwconfig
  456  sudo iwconfig
  457  sudo wifi-menu
  458  iw
  459  ipconfig
  460  iwconfig
  461  exit
  462  reboot 
  463  sudo iwconfig
  464  reboot 
  465  lvscan 
  466  sudo lvscan
  467  sudo lvm lvscan
  468  sudo lvscan
  469  sudo lvs
  470  pvs
  471  sudo pvs
  472  sudo vgs

Modify bashrc to display history command with date and time

For centos / rhel

Open /etc/bashrc and add below line

sudo vi /etc/bashrc

export HISTTIMEFORMAT="%h%d-%H%M%S "

For Arch linux

Open /etc/bash.bashrc and add below line

sudo vim /etc/bash.bashrc

export HISTTIMEFORMAT="%h%d-%H%M%S "

After editing the bashrc file, logout and login back or just close the terminal and open a new terminal and then run the history command

  sudo history

  497  Mar21-143437 sudo vim /etc/bash.bashrc 
  498  Mar21-143518 sudo vim /etc/bash.bashrc 
  499  Mar21-143553 history 
  500  Mar21-143601 sudo vim /etc/bash.bashrc 
  501  Mar21-143643 history 
  502  Mar21-143723 history 
  503  Mar21-143813 history  |more
  504  Mar21-143852 clear
  505  Mar21-143856 clear
  506  Mar21-143900 ip addr sh
  507  Mar21-143906 history 
  508  Mar21-143918 ls
  509  Mar21-143924 sh kvm-start.sh 
  510  Mar21-143945 sh kvm-start.sh 
  511  Mar21-143952 history 
  512  Mar21-144016 clear
  513  Mar21-144020 sh kvm-stop.sh 
  514  Mar21-144027 sh kvm-stop.sh 
  515  Mar21-144038 df -h
  516  Mar21-144041 lsblk 
  517  Mar21-144055 sh kvm-stop.sh 
  518  Mar21-144114 sh kvm-stop.sh 

Conclusion

We have modified bashrc file to display History command with date and time in Linux.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments