Linux Command History with date and time
Asked Answered
W

8

91

I wants to check on my linux system when which command was fired - at which date and time.

I fired commands like this:

history 50 

It shows me the last 50 commands history, but not with date and time at which it was fired. Does any one knows how to do it?

Wahhabi answered 22/7, 2016 at 12:35 Comment(11)
askubuntu.com/questions/391082/…Rawdon
superuser.com/questions/397527/…Rawdon
and even here: #11988261Rawdon
@Rawdon I have already followed this link and when I fire command, HISTTIMEFORMAT="%d/%m/%y %T " and then history it shows todays date in all command not the one date and time when it was actually fired.Wahhabi
This does not show date and time of command when it was fired. Lets say command was fired yesterday but if I write as you suggested it will show todays date. Which I do not want.Wahhabi
Setting HISTTIMEFORMAT changes format for future commands, so that timestamp will be saved for them. Timestamps for commands fired before setting HISTTIMEFORMAT were not saved in histfile, so there's no way to show them.Rawdon
@Rawdon . Ok thank for replying.Wahhabi
if you're using zsh: history -EFunches
@Rawdon I know this is an old comment but I just executed the export HITSTTIMEFORMAT as suggested in the accepted solution and I can see the date/time for previous commands. I am also quite surprised but apparently this info was stored somewhere already.Rickrickard
@MicheleAncis Good catch. Some testing showed this: 1) When you start bash, it loads histfile into memory. If histfile doesn't have timestamps, it assigns the startup time to each history entry. 2) In memory, bash always stores history entries with timestamps. When a new entry is added, its timestamp is added too. 3) When you invoke history command, bash uses history from memory. If HISTTIMEFORMAT is set, timestamps from memory are printed. 4) When you exit bash, it stores histfile. If HISTTIMEFORMAT is set, it includes timestamps into the file, otherwise just commands.Rawdon
So when you enable HISTTIMEFORMAT during bash session and run "history", you will see correct timestamps for commands issued during current session and the same timestamp for commands from previous sessions.Rawdon
R
86

Regarding this link you can make the first solution provided by krzyk permanent by executing:

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
source ~/.bash_profile
Resupinate answered 1/2, 2017 at 8:36 Comment(5)
Fang's answer has the temporary solution.Caprifig
It shows today's date and time for all previously executed commands which is wrong, is there any way I can get the correct date and time when previous commands were executed?Unhitch
I guessed, there is not because the file bash history is only a text with no any more data.Resupinate
if you're using zsh: history -EFunches
You're going to want this in all of your interactive shells, not just login shells. As such, you want to set HISTTIMEFORMAT in ~/.bashrc rather than in ~/.bash_profileZeebrugge
M
54

Try this:

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

> history

You can adjust the format to your liking, of course.

Marva answered 22/7, 2016 at 12:41 Comment(3)
This does not show date and time of command when it was fired. Lets say command was fired yesterday but if I write as you suggested it will show todays date. Which I do not want.Wahhabi
@RJ07: This won't work on commands executed in the past, since no timestamp was saved for them. However, if you add this to your bash.src, the timestamps will be saved for all future bash inputs.Marva
OK, this only works for THE FUTURE. As Rushvi says it doesn't update past histories... still ok though.Heredes
M
45

In case you are using zsh you can use for example the -E or -i switch:

history -E

If you do a man zshoptions or man zshbuiltins you can find out more information about these switches as well as other info related to history:

Also when listing,
 -d     prints timestamps for each event
 -f     prints full time-date stamps in the US `MM/DD/YY hh:mm' format
 -E     prints full time-date stamps in the European `dd.mm.yyyy hh:mm' format
 -i     prints full time-date stamps in ISO8601 `yyyy-mm-dd hh:mm' format
 -t fmt prints time and date stamps in the given format; fmt is formatted with the strftime function with the zsh extensions  described  for  the  %D{string} prompt format in the section EXPANSION OF PROMPT SEQUENCES in zshmisc(1).  The resulting formatted string must be no more than 256 characters or will not be printed
 -D     prints elapsed times; may be combined with one of the options above
Mattias answered 23/8, 2020 at 21:19 Comment(2)
This answer was best suited. I have zsh, so it seemed redundant to explicitly set formats in .zshrc, if we can do the same thing with a predefined option.Intrust
history -E 0 lists all history entries. see: superuser.com/a/232462/280738Theola
R
7

It depends on the shell (and its configuration) in standard bash only the command is stored without the date and time (check .bash_history if there is any timestamp there).

To have bash store the timestamp you need to set HISTTIMEFORMAT before executing the commands, e.g. in .bashrc or .bash_profile. This will cause bash to store the timestamps in .bash_history (see the entries starting with #).

Rutharuthann answered 22/7, 2016 at 12:41 Comment(3)
So if it is stored without time and time then now there is no way to get the time?Wahhabi
Exactly, if it is not stored anywhere you can't get it out.Trull
ohk. Thanks for replyingWahhabi
P
5
HISTTIMEFORMAT="%d/%m/%y %H:%M "

For any commands typed prior to this, it will not help since they will just get a default time of when you turned history on, but it will log the time of any further commands after this.

If you want it to log history for permanent, you should put the following line in your ~/.bashrc

export HISTTIMEFORMAT="%d/%m/%y %H:%M "
Pino answered 11/12, 2017 at 20:2 Comment(0)
C
3

It depends on which shell you are using. For GNU Bash, changing the HISTTIMEFORMAT variable will help. This nixCraft article explains how to set that variable permanently, but uses an ambiguous date format. For ISO 8601, use:

HISTTIMEFORMAT="%G-%m-%dT%T "

Result:

$ history
[...]
   13  2022-11-07T13:32:01 pwd
   14  2022-11-07T13:32:05 cd
   15  2022-11-07T13:32:10 ls -l
Converted answered 7/11, 2022 at 18:33 Comment(1)
Thanks, ISO8601 is the way to go as it's easily machine-sortable.Fann
S
1

On macOS without editing any rc files:

history -t"%F %T"
Sash answered 27/12, 2022 at 18:49 Comment(0)
D
-1

Solved with two simple commands:

export HISTTIMEFORMAT='%F %T'

history

The date and time associated with each history entry can be written to the history file, marked with the history comment character by setting the HISTTIMEFORMAT variable.

Disputant answered 13/5 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.