How can I see a timestamp for when a command was executed using history?

11,276

It is possible. The help history command says:

If the $HISTTIMEFORMAT variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each displayed history entry. No time stamps are printed otherwise

I set the variable for my user like this (on Ubuntu):

echo 'export HISTTIMEFORMAT="%d.%m.%y %T "' >> ~/.bashrc

If you want it globally then add the line to /etc/bash.bashrc:

echo 'export HISTTIMEFORMAT="%d.%m.%y %T "' >> /etc/bash.bashrc

See man strftime for all possible formatting options


The ouput of history on my box:

  ...
  132  05.05.11 10:45:11 ls
  133  05.05.11 10:45:14 cd ..
  134  05.05.11 10:45:17 history

P.S. When you set the variable the first time then the entire history will get the time stamp of the moment the variable was set.

Share:
11,276

Related videos on Youtube

Jonathan Day
Author by

Jonathan Day

Lover of patterns, hater of hacks, aficionado of elegant solutions

Updated on September 18, 2022

Comments

  • Jonathan Day
    Jonathan Day over 1 year

    I'd like to check at what time/date a command in bash history was executed. Is this possible?

  • boehj
    boehj almost 13 years
    I'll find this really useful but I haven't had any success yet. I've appended [export HISTIMEFORMAT="%d.%m.%y %T "] to my ~/.bashrc but I'm not yet getting the extra time data when I do history. (On Ubuntu 10.10 for what it's worth.)
  • Jonathan Day
    Jonathan Day almost 13 years
    looks great but unfortunately I'm seeing the same as @boehj on Centos
  • Siim K
    Siim K almost 13 years
    @Jonathan: I think on CentOS you need to edit /etc/bashrc
  • Siim K
    Siim K almost 13 years
    @Jonathan and @boehj: of course, there was a T missing from the variable name :( I blame my fingers...I've updated the answer, please try now
  • boehj
    boehj almost 13 years
    @Slim K - Works a charm now. Thanks for clearing that up. This is going to more than a little useful for me. Many thanks. :)