I've seen a lot of documentation on how to timestamp the output of the history command. But how would you go about time-stamping a saved history file? For example I have a server setup to save each users bash history to /root/history/.bash_history-$USER. This is done through a line in /root/.bash_profile
export HISTFILE=/root/history/.bash_hist-$(who -m | awk '{print $1}')
I have added the line:
export HISTTIMEFORMAT="%d/%m/%y %T "
to /root/.bash_profile and sourced it. However timestamps are not put in the files, you only see them when you run the history command. How can I set this up to do both?
EDIT:
I have added the lines in my .bashrc file as suggested:
export HISTSIZE=3000
export HISTFILESIZE=5000
export HISTFILE=/root/history/.bash_hist-$(who -m | awk '{print $1}')
format_history () {
perl -i -e '$/=undef;my $string=<>;$string=~s/#([0-9] {10}.*\n.*$)/"# ".localtime($1)."#\n#$1"/ge;print $string;' /root/history/.bash_hist-$(who -m | awk '{print $1}')
}
export HISTTIMEFORMAT="%d/%m/%y %T "
shopt -s histappend
PROMPT_COMMAND="history -a;format_history;$PROMPT_COMMAND"
Now when I try to open my history file, using vim, cat or less, I see:
command I ran
#Thu Mar Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec Wed Dec 31 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 18:00:31 1969 14:26:02 2016
EDIT 3:
After making latest changes:
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
# Fri Apr 1 15:26:35 2016#
#1459542395
command