How to see time stamps in bash history?
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.
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
Related videos on Youtube
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, 2022Comments
-
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 over 7 yearsI'm amazed how poorly documented this is both on Linux and BSD in the manpages.
-
Admin about 6 yearsCheck this blog out - sanctum.geek.nz/arabesque/better-bash-history .
-
Admin over 5 yearsif you're using zsh:
history -E
-
Admin over 5 yearsNot sure why two answers below feel the need to tell you how to open a terminal..
-
Admin over 5 yearsAccording 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 over 10 yearscan you expand it for more clarity ?
-
mx7 over 10 yearsThanks for answering , \will it be permanent change ?
-
LasagnaAndroid over 8 yearsHey, 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 over 8 yearsCorrection 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 about 8 yearsNote 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 about 8 years@Jamil: for people who follow ISO standards, it's "%y-%m-%d" for the date part ;)
-
Sridhar Sarnobat over 7 yearsJust a note to zsh users out there - unlike many builtins it does not mirror bash. See this instead: unix.stackexchange.com/questions/103398/…
-
Michael over 7 yearsuses 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ş over 6 yearsI set $HISTTIMEFORMAT and I got the times for today even before the commands of this set.
-
muru about 6 yearsThis 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 almost 6 years@Gauthier - actually, just use %F for ISO date ;-) man7.org/linux/man-pages/man3/strftime.3.html
-
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 almost 5 yearsHello 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 almost 5 yearsHi, 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 nameexisting-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 usingsudo
. 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 about 4 years
HISTTIMEFORMAT="%d/%m/%y %T " history
works -
Alex78191 about 4 years
HISTTIMEFORMAT="%d/%m/%y %T " history
works -
sjethvani over 3 yearsMake sure permission of /etc/profile.d/timestamp.sh is '644' , so that history command shows timestamp for all users .
-
sjethvani over 3 yearsJust 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 over 3 yearsThanks @sjethvani
-
ijoseph over 3 yearsWeirdly, 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 over 3 yearsQuick and dirty for ZSH Timestamp
-
Mark about 2 yearszsh: 1, bash: 0