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_profileexport 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=3000export HISTFILESIZE=5000export 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 histappendPROMPT_COMMAND=history -a;format_history;$PROMPT_COMMANDNow 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 2016EDIT 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##1459542395command
timestamp saved history file
bash;command history
You could do something likeperl -Mposix -pe 's/^#([0-9]{10})/POSIX::strftime( %d\/%m\/%y %T, gmtime($1))/eg' .bash_history > readable_historyto replace the epoch timestamps in the file with formatted ones.  To do it in place, you could modify it to insert the formatted times as comments above each line, but make sure to put a space before the first number so that bash doesn't try to interpret it as it's own timestamp when reading the history file.Edit:  Here's the line to do it in place without messing up the history file:perl -Mposix -i -pe 's/^#([0-9]{10})(.*)/# .POSIX::strftime( %d\/%m\/%y %T, gmtime($1)).\n#$1$2/ge' .bash_historyThis will make a file that looks like this:#1356431101 command arg arg#1356531101 command2 arg2Look like this:# 25/12/12 10:25:01#1356431101 command arg arg# 26/12/12 14:11:41#1356531101 command2 arg2Edit2:  Putting this in my bashrc did the trick for me:format_history () {    perl -i -e '$/=undef;my $string=<>;$string=~s/#([0-9]{10}.*\n.*$)/# .localtime($1).#\n#$1/ge;print $string;' ~/.bash_history}export HISTTIMEFORMAT=%d/%m/%y %T shopt -s histappendPROMPT_COMMAND=history -a;format_history;$PROMPT_COMMANDFinal edit -- Here's a better version of the regex that shouldn't duplicate anything:perl -i -e '$/=undef;my $string=<>;$string=~s/(#\s[^\n]*\n)?#(\d{10}\n)/# .localtime($+).\n#$+/ge;print $string;' your_history_file_name_here