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

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


Solution 1:

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.