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

| |

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.

| |
  • 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.) – boehj May 5 '11 at 8:41
  • @boehj: You might need to log out and log in again – Siim K May 5 '11 at 8:51
  • OK I'll give that a go and report back. Cheers. But for now it's beer-o'clock. :) – boehj May 5 '11 at 9:30
  • looks great but unfortunately I'm seeing the same as @boehj on Centos – Jonathan Day May 5 '11 at 10:04
  • 1
    @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 – Siim K May 5 '11 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.