259

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

  • 7
    I'm amazed how poorly documented this is both on Linux and BSD in the manpages. – Sridhar Sarnobat Aug 30 '16 at 21:52
  • Check this blog out - sanctum.geek.nz/arabesque/better-bash-history . – abhicantdraw Feb 22 '18 at 10:31
  • 4
    if you're using zsh: history -E – Vahid Nov 6 '18 at 16:58
  • 4
    Not sure why two answers below feel the need to tell you how to open a terminal.. – mwfearnley Dec 24 '18 at 8:39
  • 2
    According to the blog referenced by @dog0 , timestampse apparently do not get saved unless the HISTTIMEFORMAT variable was set when commands were issued. In other words, if you didn't have it set, setting it now will not help you retrieve timestamps of previously issued commands. – Tomislav Nakic-Alfirevic Jan 17 at 15:14
318

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 '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.

  • 7
    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
  • 71
    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
  • 11
    @Louis - Actually this worked for me retroactively for me?!?! – stephenmm Mar 29 '16 at 15:24
  • 19
    @Jamil: for people who follow ISO standards, it's "%y-%m-%d" for the date part ;) – Gauthier Mar 31 '16 at 11:03
  • 13
    @Gauthier - actually, just use %F for ISO date ;-) man7.org/linux/man-pages/man3/strftime.3.html – brianmearns Jul 11 '18 at 19:34
73

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
  • 1
    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
18

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
  • 6
    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
  • 4
    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
  • @user491029 "...even existing". This is true, but misleading. On ubuntu 14.04, at least, it did start showing timestamps for all history entries once I had set HISTTIMEFORMAT, but it looks like the timestamps for any command run before the current session were the login timestamp of the current session. – jbobbins Jun 27 at 19:52
1

You'll see changes on next login.

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

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
  • 1
    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 '18 at 10:12
0

I know, I'm answering it very late, but to do this for all the users, you can create any .sh file in /etc/profile.d and add the following line to it:

echo 'HISTTIMEFORMAT="%Y-%m-%d %T "' >> /etc/profile.d/existing-foo-file.sh

If you not logged in as root or superuser, you need to use the tee command to do this:

echo 'HISTTIMEFORMAT="%Y-%m-%d %T "' | sudo tee /etc/profile.d/mytimestamp.sh

If you want to append to any existing file, pass -a flag to tee command:

echo 'HISTTIMEFORMAT="%Y-%m-%d %T "' | sudo tee -a /etc/profile.d/mytimestamp.sh

enter image description here

  • Hello Shashank, Thanks for adding the answer but how this answer is different from others ? If your answer is completely different then we are glad to have it, but if not you can improve existing answers. – rɑːdʒɑ Jul 22 at 5:03
  • Hi, thanks for commenting. This is different from others in the following way: 1. Some of them have mentioned about creating for all users but no one mentioned about creating/explaining any file name in /etc/profile.d. timestamp.sh looks very close to technical (so it creates confusion) so I used the name existing-foo-file.sh (foo-bar concept). 2. No one has mentioned about using it with non-root users. So I have given an example of using sudo. 3. The 3rd example improves the 2nd example of appending to an existing file. Since linux/unix beginners can hit this issue, I elaborated. – Shashank Agrawal Jul 22 at 5:32

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.