时间戳保存的历史文件

时间戳保存的历史文件

我看过很多关于如何为历史命令的输出添加时间戳的文档。但是您将如何为保存的历史文件添加时间戳呢?例如,我有一个服务器设置将每个用户的 bash 历史记录保存到 /root/history/.bash_history-$USER 。这是通过 /root/.bash_profile 中的一行完成的

export HISTFILE=/root/history/.bash_hist-$(who -m | awk '{print $1}')

我添加了以下行:

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

到 /root/.bash_profile 并获取它。但是,时间戳不会放入文件中,只有在运行历史命令时才能看到它们。我该如何设置才能同时执行这两项操作?

编辑:

我已按照建议在 .bashrc 文件中添加了以下行:

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"

现在,当我尝试使用 vim、cat 或 less 打开历史文件时,我看到:

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

编辑3:

进行最新更改后:

# 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

答案1

你可以做类似的事情

perl -Mposix -pe 's/^#([0-9]{10})/POSIX::strftime( "%d\/%m\/%y %T", gmtime($1))/eg' .bash_history > readable_history

用格式化的时间戳替换文件中的纪元时间戳。要就地执行此操作,您可以修改它以在每行上方插入格式化时间作为注释,但请确保在第一个数字之前放置一个空格,以便 bash 在读取历史记录时不会尝试将其解释为自己的时间戳文件。

编辑:这是在不弄乱历史文件的情况下就地执行此操作的行:

perl -Mposix -i -pe 's/^#([0-9]{10})(.*)/"# ".POSIX::strftime( "%d\/%m\/%y %T", gmtime($1))."\n#$1$2"/ge' .bash_history

这将创建一个如下所示的文件:

#1356431101 command arg arg
#1356531101 command2 arg2

看起来像这样:

# 25/12/12 10:25:01
#1356431101 command arg arg
# 26/12/12 14:11:41
#1356531101 command2 arg2

Edit2:将其放入我的 bashrc 中对我来说很有效:

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 histappend
PROMPT_COMMAND="history -a;format_history;$PROMPT_COMMAND"

最终编辑 - 这是正则表达式的更好版本,不应重复任何内容:

perl -i -e '$/=undef;my $string=<>;$string=~s/(#\s[^\n]*\n)?#(\d{10}\n)/"# ".localtime($+)."\n#$+"/ge;print $string;' your_history_file_name_here

相关内容