Most of us know that history command on Ubuntu shows the list of previously executed commands. This command is really handy either to examine the previously executed command or re-execute the previous command. The default configuration just lists out the entry number and the commands. It doesn’t display the time of execution.

  />history

  495  fg
  496  ls
  497  rm smcsparklenotification_100_66.jtl 
  498  jmeter -n -t Sparkle-notification-test-plan.jmx -l smcsparklenotification_100_66.jtl
  499  lock
  500  locate AttDevice
  501  locate *.sql
  502  cat /sys/block/sda/size
  503  udisks --show-info /dev/sda | egrep "^[[:space:]]*size" | awk '{print $2}'
  504  sudo blockdev --getsz /dev/sda
  505  fdisk -l
  506  sudo fdisk -l
  507  date
  508  man history
  509  date

This post provides a tweak to display the time of execution along with the executed command. It is really helpful while debugging. When this command is executed it tries to find HISTTIMEFORMAT variable, If it is defined then it displays the date and time as defined by this variable. Otherwise it doesn’t display the date-time.
If .bash_profile file is present in the home directory then execute the following command.

echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bash_profile
source ~/.bash_profile

Otherwise you can include the HISTTIMEFORMAT in .bashrc file by executing the following command.

echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
source ~/.bashrc

Here %F displays date whereas %T displays time.
Once the above commands are executed then execute the history command. You will get the output similar to shown below.

  480  2013-08-26 16:24:08 vi ~/.bash_profile
  481  2013-08-26 16:24:35 cd ~/.bash_history 
  482  2013-08-26 16:24:42 vi .bash_history 
  483  2013-08-26 16:25:06 grep bash_history *
  484  2013-08-26 16:25:15 grep bash_history \*
  485  2013-08-26 16:25:17 ls
  486  2013-08-26 16:25:23 vi .bashrc 
  487  2013-08-26 16:27:15 sourc .bashrc 
  488  2013-08-26 16:27:19 source .bashrc 
  489  2013-08-26 16:27:22 history
  490  2013-08-26 16:28:27 exit
  491  2013-08-26 16:28:31 history
  492  2013-08-26 16:28:43 echo Rakesh
  493  2013-08-26 16:28:46 history
  494  2013-08-26 16:29:04 exit
  495  2013-08-26 16:37:59 vi .bash_profile 
  496  2013-08-26 16:38:29 source .bash_profile 
  497  2013-08-26 16:38:33 history
  498  2013-08-26 16:38:37 exit
  499  2013-08-26 16:38:54 history
  500  2013-08-26 16:38:59 exit
  501  2013-08-26 19:17:33 vi .bash_profile 
  502  2013-08-26 19:22:09 history
,
Trackback

no comment untill now

Add your comment now