How to see time stamps in bash history?

564,468

Solution 1

Press Ctrl+Alt+T to open a terminal, then run one of the commands below:

HISTTIMEFORMAT="%d/%m/%y %T "  # for e.g. “29/02/99 23:59:59”
HISTTIMEFORMAT="%F %T "        # for e.g. “1999-02-29 23:59:59”

To make the change permanent for the current user run:

echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc  # or respectively
echo 'HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
source ~/.bashrc

To test the effects run:

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.

Solution 2

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/

Solution 3

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.

Solution 4

Changing HISTIMEFORMAT didn't work for me, because I'm using zsh.

If you want to make it work with zsh, you just have to type : history -i

Solution 5

You 'll see changes on next login.

echo 'HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
Share:
564,468

Related videos on Youtube

mx7
Author by

mx7

LinkedIn Profile: https://in.linkedin.com/in/raja-genupula-05205531 Endorse me If I helped you. Blog : http://thelinuxmen.blogspot.com/ Ubuntu - CentOS - Fedora - Windows - Severs - IIS - FTP - Security Thank you.

Updated on September 18, 2022

Comments

  • mx7
    mx7 over 1 year

    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

    • Admin
      Admin over 7 years
      I'm amazed how poorly documented this is both on Linux and BSD in the manpages.
    • Admin
      Admin about 6 years
    • Admin
      Admin over 5 years
      if you're using zsh: history -E
    • Admin
      Admin over 5 years
      Not sure why two answers below feel the need to tell you how to open a terminal..
    • Admin
      Admin over 5 years
      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.
  • mx7
    mx7 over 10 years
    can you expand it for more clarity ?
  • mx7
    mx7 over 10 years
    Thanks for answering , \will it be permanent change ?
  • LasagnaAndroid
    LasagnaAndroid over 8 years
    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?
  • Admin
    Admin over 8 years
    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. :)
  • Louis Maddox
    Louis Maddox about 8 years
    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
  • Gauthier
    Gauthier about 8 years
    @Jamil: for people who follow ISO standards, it's "%y-%m-%d" for the date part ;)
  • Sridhar Sarnobat
    Sridhar Sarnobat over 7 years
    Just a note to zsh users out there - unlike many builtins it does not mirror bash. See this instead: unix.stackexchange.com/questions/103398/…
  • Michael
    Michael over 7 years
    uses the time format of strftime, which unfortunately doesn't appear to have any way to display milliseconds... (at least not portably, %f is claimed to work on some systems???)
  • Yasin Okumuş
    Yasin Okumuş over 6 years
    I set $HISTTIMEFORMAT and I got the times for today even before the commands of this set.
  • muru
    muru about 6 years
    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.
  • brianmearns
    brianmearns almost 6 years
    @Gauthier - actually, just use %F for ISO date ;-) man7.org/linux/man-pages/man3/strftime.3.html
  • jbobbins
    jbobbins almost 5 years
    @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.
  • mx7
    mx7 almost 5 years
    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.
  • Shashank Agrawal
    Shashank Agrawal almost 5 years
    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.
  • Alex78191
    Alex78191 about 4 years
    HISTTIMEFORMAT="%d/%m/%y %T " history works
  • Alex78191
    Alex78191 about 4 years
    HISTTIMEFORMAT="%d/%m/%y %T " history works
  • sjethvani
    sjethvani over 3 years
    Make sure permission of /etc/profile.d/timestamp.sh is '644' , so that history command shows timestamp for all users .
  • sjethvani
    sjethvani over 3 years
    Just a note : Make sure permission of /etc/profile.d/timestamp.sh is '644' , so that history command shows timestamp for all users . In my case permission of '/etc/profile.d/timestamp.sh' was '640' , because of which users other than owner was not seeing timeformatted history .
  • Shashank Agrawal
    Shashank Agrawal over 3 years
    Thanks @sjethvani
  • ijoseph
    ijoseph over 3 years
    Weirdly, this did appear to work for me retrospectively - I was able to see the timestamps of history items prior to my explicit setting of this environment variable. That may be because HISTTIMEFORMAT was set to an empty string before, or something similar?
  • Timo
    Timo over 3 years
    Quick and dirty for ZSH Timestamp
  • Mark
    Mark about 2 years
    zsh: 1, bash: 0