export HISTTIMEFORMAT="%d/%m/%y %T "
to your .bashrc and then history
will show the date and time before each command that was run.When I last needed it ~2011, there was a small and simple tool called libsnoopy for that.
Emphasis on "log file" though, the "history file" is a different use case, fundamentally unfit for forensics.
1) It made the history command run much, much slower (I have HISTSIZE=100000)
2) Dates tend to reset to a uniform but meaningless value. I think it's related to OS reboots.
My preference is just for a date in the history, rather than date and time. I find that when I'm searching history, date is usually granular enough for me, so I can omit the time and save a little bit of screen real estate.
With an updating prompt, you can see when a command ran without having to type 'history'.
And adding a date/time stamp to history is very useful when you need to go back further - hours, days or months.
I know not everyone is on the zsh train, but it might be able to be ported to bash (it's just pre-hooks and some sqlite)
## History configuration, see HISTORY in bash(1)
## builtin fc, aka fix command, for history manipulation
## fc [-e ename] [-lnr] [first] [last]
## -e or invoke editor ename; defaults to $FCEDIT
## -l or list commands
## -n or suppress entry numbers
## -r or reverse command order
## first, last selects a range of entries by numeric index
##
## fc -s [pat=rep] [cmd]
## -s or substitute all occurences of pat with rep
#
# - h() is short for history
# - r() is short for replay
# - always append to the history, don't overwrite
# - save multi-line commands in a single entry
# - insist on vim everywhere
# - don't record commands with a leading space or duplicates
# - constrain the size of the history file
# - ignore common things
# - constrain the number of history entries
# - set up nicer timestamps
alias h="history"
alias r="fc -s"
shopt -s histappend
shopt -s cmdhist
export FCEDIT=$EDITOR
export HISTCONTROL=ignoreboth
export HISTFILE=$HOME/.bash_history
export HISTFILESIZE=1048576
export HISTIGNORE='ls:ls -l:ls -latr:ps -ef:fc:h:history:clear:exit'
export HISTSIZE=8192
export HISTTIMEFORMAT='[%F %T] '
I don't mean that it would show the time when the prompt was written to the terminal. That's easy.
I mean that it would show the time when the command entered at it was executed. Those two times can be very different.
This little hack updates your Bash prompt with the current time when you execute a command.
As someone who spends many hours every day in the terminal, it's improved my life. Maybe it can help you too.