Tuesday, September 15, 2015

history command with Date & Time


> How to show date and time in your history command using environment  variable HISTTIMEFORMAT

commands > history

It usually happens with me that I am trying out a cli based tool or learnt some new trick, it works fine then, but later I can hardly recall what I did . In such cases, all I remember is either a part of the command or the time while I was doing it. I always wondered if we could get more out of the history command - days, time etc. After searching a little, I found that it is actually possible and could be done pretty easily.

Its all in the genius of Bash . All you have to do is set the environment variable, HISTTIMEFORMAT, appropriately. Lets take an example where I just want to so the timestamp.

[shredder12]$ export  HISTTIMEFORMAT="%T "

[shredder12]$ history

 568  13:53:01 clear
 569  13:53:03 top
 570  13:53:07 sudo fdisk -l
 571  13:53:16 free -m
 572  13:53:17 cd
 573  13:53:18 ls
 574  13:53:22 cd Downloads/

Similary, for date you can use the following parameters

%d for Day
 %m for Month
 %y for Year

So, if you want both date and time stamp

[shredder12]$ export HISTTIMEFORMAT="%d/%m/%y %T "

[shredder12]$ history

568  25/10/10 13:53:01 clear
 569  25/10/10 13:53:03 top
 570  25/10/10 13:53:07 sudo fdisk -l
 571  25/10/10 13:53:16 free -m
 572  25/10/10 13:53:17 cd
 573  25/10/10 13:53:18 ls
 574  25/10/10 13:53:22 cd Downloads/

Please note that, for a permanent setting you will have to append the "export" line in ~/.bashrc file.

Great tip. For better sort-ability, I used year/month/day:
export HISTTIMEFORMAT="%y/%m/%d %T "

No comments:

Post a Comment