如何删除特定时间段内的命令行历史记录?
只用一个命令就可以做到这一点吗?
答案1
虽然可以存储命令运行的时间,但历史操作命令不使用时间作为参考。HISTTIMEFORMAT
在man bash
:
HISTTIMEFORMAT
If this variable is set and not null, its value is used as a
format string for strftime(3) to print the time stamp associated
with each history entry displayed by the history builtin. If
this variable is set, time stamps are written to the history
file so they may be preserved across shell sessions. This uses
the history comment character to distinguish timestamps from
other history lines.
如果你HISTTIMEFORMAT
在运行要删除的命令之前进行了设置,.bash_history
则会出现如下行:
$ tail -4 ~/.bash_history
#1449955320
history
#1449955329
history -w
然后,您可以利用 Unix 时间戳来删除它们,awk
例如:
awk -F# -v end=$(date -d yesterday +%s) \
-v start=$(date -d 'now - 3 days' +%s) \
'$2 < start || $2 > end {print; getline; print}'
我不确定此命令如何与多行命令一起工作,但您可以计算时间戳来获取分配给命令的编号,然后用它history
来删除它。
如果您事先没有设置HISTTIMEFORMAT
,那么您必须手动执行此操作。