This site is now 100% read-only, and retired.

XML logo

how to activate bash history timestamps
Posted by rossen on Mon 14 Jul 2008 at 09:27
Tags: , ,
While re-reading the man page for bash 3.1 (in etch) I discovered that one can have timestamps in the output of "history". Just set the HISTTIMEFORMAT environment variable like so:

HISTTIMEFORMAT='%F %T '
export HISTTIMEFORMAT

...and "history" will give you output like:


536 2008-07-14 10:13:02 echo $HISTTIMEFORMAT
537 2008-07-14 10:13:50 cat /etc/profile.local
538 2008-07-14 10:14:52 history

The .bash_history will contain hash-commented timestamps as seconds since the epoch:

#1216023182
echo $HISTTIMEFORMAT
#1216023230
cat /etc/profile.local
#1216023292
history

 

Comments on this Entry

Re: how to activate bash history timestamps
Posted by ajt (204.193.xx.xx) on Mon 14 Jul 2008 at 14:04
[ View Weblogs ]

Very interesting. I have the following function which I find useful:

function h() {
    if [ -z "$1" ]
    then
        history | grep -v "  h" | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
    else
        history | grep -v "  h" | grep $1 | sed 's/[ \t]*$//' | sort -k 2 -r | uniq -f 1 | sort -n
    fi
}

--
"It's Not Magic, It's Work"
Adam

[ Parent ]

Re: how to activate bash history timestamps
Posted by atrixnet (64.39.xx.xx) on Thu 17 Jul 2008 at 21:52
[ View Weblogs ]
You find it useful... for what?

[ Parent ]

Re: how to activate bash history timestamps
Posted by ajt (195.112.xx.xx) on Thu 17 Jul 2008 at 22:01
[ View Weblogs ]

Finding things in my history. I know there are better ways of doing things, but I've found an alias of h=history on a number of systems, so I'm use to using it. With this function I can grep bits of the history out easily without having to change my use of "h" to get my history.

--
"It's Not Magic, It's Work"
Adam

[ Parent ]

Re: how to activate bash history timestamps
Posted by atrixnet (64.39.xx.xx) on Thu 17 Jul 2008 at 22:22
[ View Weblogs ]
Somewhat related is the very cool ability to put timestamps in your bash prompt:

export PS1="\n[\[\e[1;37m\]\u\[\e[0m\]@\[\e[1;34m\]\H\[\e[0m\]] [\[\e[1;33m\]\d, \t\[\e[0m\]] [\[\e[1;31m\]\!\[\e[0m\]]\n\[\e[1;31m\]\[\e[0m\][\[\e[1;37m\]\w\[ \e[0m\]]\n\[\e[1;37m\]\\$\[\e[0m\] "

put that in your pipe and smoke it, errr, in your bash_profile and source it.

[ Parent ]

Re: how to activate bash history timestamps
Posted by Anonymous (77.28.xx.xx) on Wed 24 Sep 2008 at 11:12
THANKS A LOOOOT!!!

[ Parent ]

Re: how to activate bash history timestamps
Posted by Anonymous (80.34.xx.xx) on Mon 2 Mar 2009 at 11:47
Thanks for the tip, it was very useful, especially when you're in a true multi-user environment (so, more suers messin^W touching things)

[ Parent ]

Re: how to activate bash history timestamps
Posted by Anonymous (83.253.xx.xx) on Wed 29 Apr 2009 at 18:21
Thanks! Best first blog post I've seen, that I know of. ;)

[ Parent ]