Skip to content

Instantly share code, notes, and snippets.

@colematt
Last active March 21, 2023 22:42
Show Gist options
  • Save colematt/c052c4bc9809c5e0a25f3ab30ae2c9da to your computer and use it in GitHub Desktop.
Save colematt/c052c4bc9809c5e0a25f3ab30ae2c9da to your computer and use it in GitHub Desktop.
[Get Date and Time of a History Event] #bash #linux

Get Date and Time of a History Event

By default, history will only give an event designator and command line entry. Unless the event reference is absolute, the events are relative to the current position in the history list.

history
 1995  ls S*.pdf
 1996  ls -lh S*.pdf
 1997  xdg-open System_01Jun2018_30Jun2018_0133*.pdf
 1998  ssh matthew@localhost
 1999  gpass
 2000  history 

To get the date and time, use the HISTTIMEFORMAT environment variable:

# Display date and time once
HISTTIMEFORMAT="%d/%m/%y %T " history

# Display date and time in this shell
export HISTTIMEFORMAT="%d/%m/%y %T "
history

# Permanently display date and time
echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
source ~/.bashrc
history

Now history will also display the time and date of the event.

history
 1995  11/03/10 04:31:36 ls S*.pdf
 1996  11/03/10 04:31:38 ls -lh S*.pdf
 1997  11/03/10 04:31:40 xdg-open System_01Jun2018_30Jun2018_0133*.pdf
 1998  11/03/10 04:31:55 ssh matthew@localhost
 1999  11/03/10 04:33:15 gpass
 2000  11/03/10 04:33:32 history 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment