Is there any way I can see at what time the commands were executed from the bash history? We can see the order but is there any way I can get the time also?

Bottom-Line: Execution time in the Bash history

up vote 237 down vote accepted

Press Ctrl+Alt+T to open Terminal, then run the command below:

HISTTIMEFORMAT="%d/%m/%y %T "

Or, to make the change permanent for the current user:

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

Then

history

For more info see man bash or An A-Z Index of the Bash command line for Linux.

For commands that were run before HISTTIMEFORMAT was set, the current time will be saved as the timestamp. Commands run after HISTTIMEFORMAT was set will have the proper timestamp saved.

  • 4
    Why declare it with export in .bash_profile? It is a variable only read by bash, not by commands launched from it, so it should not be exported. – Alvaro Gutierrez Perez Oct 19 '15 at 21:36
  • 51
    Note that this will only record timestamps for new history items, after HISTTIMEFORMAT is set for sessions, i.e. you can't use this retrospectively. Some answers here give the impression the history command immediately shows timestamped entries – Louis Maddox Feb 20 '16 at 20:58
  • 6
    @Louis - Actually this worked for me retroactively for me?!?! – stephenmm Mar 29 '16 at 15:24
  • 11
    @Jamil: for people who follow ISO standards, it's "%y-%m-%d" for the date part ;) – Gauthier Mar 31 '16 at 11:03
  • 3
    @Gauthier - actually, just use %F for ISO date ;-) man7.org/linux/man-pages/man3/strftime.3.html – brianmearns Jul 11 at 19:34

Open terminalCtrl+Alt+T and run,

HISTTIMEFORMAT="%d/%m/%y %T "

then,

history

To make the changes permanent follow the below steps,

gedit ~/.bashrc

you need to add the below line to .bashrc file and then save it,

export HISTTIMEFORMAT="%d/%m/%y %T "

run the below command to source .bashrc file,

source ~/.bashrc

After that run history command.

enter image description here

source:http://www.cyberciti.biz/faq/unix-linux-bash-history-display-date-time/

  • Thanks for answering , \will it be permanent change ? – rɑːdʒɑ Dec 15 '13 at 12:06
  • Hey, I'm doing this in OS X and Windows (through MINGW), and I'm adding it into .bash_profile, what's the diff between profile and rc? – LasagnaAndroid Oct 1 '15 at 17:28

Yes, you can: if you set $HISTTIMEFORMAT, the .bash-history will be properly timestamped. That doesn't help with existing .bash-history content, but will help in the future.

  • 1
    can you expand it for more clarity ? – rɑːdʒɑ Dec 15 '13 at 12:06
  • 3
    Correction to previous post: Setting HISTTIMEFORMAT enables the display of the timestamps...even existing. My favorite is:HISTTIMEFORMAT='%F %T ' as it matters not from which country you reside...everyone knows immediately what time it is. :) – user491029 Jan 8 '16 at 22:43
  • 2
    I set $HISTTIMEFORMAT and I got the times for today even before the commands of this set. – Yasin Okumuş Sep 18 '17 at 13:40

To enable history timestamps for all users, create a script in /etc/profile.d :

echo 'HISTTIMEFORMAT="%Y%m%d %T "' >> /etc/profile.d/timestamp.sh
  • This will likely not work for users logging in via the GUI, since the default terminal will not run login shells. /etc/bash.bashrc would be a better place. – muru Feb 22 at 10:12

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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