View the history of commands along with time

Generally when we try to view the history of the commands in Linux then we only see all the commands which are used but not the time when they were used. Here i will show you how you can view the time and date when the commands were used.

# history | less
1 service network restart
2 ping google.com
3 vi /etc/resolv.conf
4 ping google.com
5 vi /etc/sysconfig/network-scripts/ifcfg-eth0
6 service network restart
7 ping google.com
8 yum -y install samba
9 yum -y install quota
10 service iptables stop
Here above you can see that you are only able to view the commands which were run

Now to view the history along with time and date

# export HISTTIMEFORMAT="%d:%m:%Y %T "

# history | less
1 08:10:2012 11:31:31 service network restart
2 08:10:2012 11:31:31 ping google.com
3 08:10:2012 11:31:31 vi /etc/resolv.conf
4 08:10:2012 11:31:31 ping google.com
5 08:10:2012 11:31:31 vi /etc/sysconfig/network-scripts/ifcfg-eth0
6 08:10:2012 11:31:31 service network restart
7 08:10:2012 11:31:31 ping google.com
8 08:10:2012 11:31:31 yum -y install samba
9 08:10:2012 11:31:31 yum -y install quota
10 08:10:2012 11:31:31 service iptables stop
But the export command will be session dependent so as soon as you change session or reboot the changes won't work. So add the export line in your .bash_profile file for permanent changes.
# vi /root/.bash_profile
export HISTTIMEFORMAT="%d:%m:%Y %T "

Related Articles

Follow the below links for more tutorials