bash - How can I see a timestamp for when a command was executed using history?
2014-07
I'd like to check at what time/date a command in bash history was executed. Is this possible?
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.
Possible Duplicate:
Can history files be unified in bash?
I use Ubuntu Server 9.10 and I would like to be able to see my bash history for more than one terminal sessions. I.e. my last 200 commands or so, even if I have been logged out in between.
When I use the history
I just see all commands from my actual terminal session. How can I see more command history from Bash? Is there any specific settings for bash that I should change from the default values in Ubuntu?
I don't have a ~/.bash_history
file. But I have an ~/.bashrc
with HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
and HISTCONTROL=ignoreboth
echo $HISTFILE
/home/sanoj/.bash_history
echo $HISTSIZE
500
echo $HISTFILESIZE
500
echo $HISTCONTROL
ignoreboth
UPDATE: I am now trying Ubuntu Server 10.10 in VirtualBox. If I just turn off VirtualBox without the shutdown command, then next time when I boot, the commands from the last session is not saved in the history file.
The commands are only saved if I shutdown the machine with the shutdown command. E.g. shutdown -P 0.
This must be the reason to my problem. So I have to figure out how to save the command-history more often. E.g. after each command.
You can use
history -a
to immediately append the in-memory history to the history file. One terminal session can't see another's unless this is done or the other is exited.
You can use
history 200 | less
to see that number of entries.
In addition to HISTSIZE
see the entry in the Bash man page concerning HISTFILESIZE
.
Once you log out the history gets appended to the file ~/.bash_history
. Have a look in there.
By default it will remember your last 500 commands. If you want to save more set the variable HISTSIZE
in ~/.bashrc
.
I also do add ignoredups
to HISTCONTROL
(with HISTCONTROL=$HISTCONTROL:ignoredup
). This makes duplicate consecutive commands to be save only once.
Have a look at man 1 bash
for what else you can tune about the history.
bash history is usually loaded when the shell begins running, and is saved when it is exited normally. You can use history -a
and history -n
to override this, but not automatically unless you abuse $PROMPT_COMMAND
or something similar.
Sanoj, this really should "just work"; I don't think there is supposed to be anything special you need to do to enable this. It sounds like something is interfering with the normal course of events.
I would look in /etc/profile, /etc/bashrc (or perhaps /etc/bash.bashrc according to some sources), ~/.bash_profile, ~/.bashrc, ~/.bash_login, ~/.bash_logout, to see if there is anything that might be affecting the history (perhaps grep -i hist
on each of the above files).
... In particular I wonder if you have something in ~/.bash_logout that removes the file.
Alternatively, is it possible that something is overriding the $HOME
variable?