history命令参数
history [n] 显示最近n条历史命令
history -d offset
history -anrw [filename]
history -p arg [arg ...]
history -s arg [arg ...]
不带选项的话,显示带行号的命令历史列表。列出的行中含有 * 的已经被修改过。参数 n 使得只显示最后 n 行。如果给出了 filename,它被用做历史文件名;没有的话,将使用 HISTFILE 的值作为历史文件名。选项如果给出,则具有下列意义:
-c 清空历史列表,删除所有条目。
-d offset 删除 offset 位置的历史条目。
-a 将 ``新'' 的历史条目 (自当前 bash 会话开始输入的历史命令) 追加到历史文件中。
-n 将尚未从历史文件中读取的历史条目读入当前历史列表。这些行是当前 bash 会话开始之后,才追加到历
史文件中的行。
-r 读取历史文件的内容,使用它们作为当前历史。
-w 将当前历史列表写入历史文件,覆盖历史文件的原有内容。
-p 对后续的 args 进行历史替换,在标准输出上显示结果。 不会将结果存入历史列表。每个 args 都必须
被引用,来禁止 普通的命令扩展。
-s 将 args 保存到历史列表中,作为单独的条目。历史列表中的最后一个命令在添加 args 之前被删除。
返回 0,除非遇到了非法的选项,读/写历史文件发生错误,在 -d 的 参数中给出了无效的 offset,或者对 -p 的后续参数进行历史扩展失败。
history命令上显示日期时间
vim ~/.bashrc 添加 export HISTTIMEFORMAT=" %F %T "
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export HISTTIMEFORMAT=" %F %T `whoami` " #whoami 可以用来显示用户
重新加载用户的环境变量 source .bashrc
关闭/打开history记录功能
set +o history #关闭history命令
set -o history #开启history命令
查看历史命令记录文件
[root@localhost ~]# cat ./.bash_history | tail -n 10
#1704466256
history
#1704466265
cat ./.bash_history | tail -n 10
#1704466292
cat ./.bash_history
#1704466301
history
#1704466330
history -a
执行历史命令
225 2024-01-05 22:55:48 history
[root@localhost ~]# !225
history
1 2024-01-05 22:05:16 history
2 2024-01-05 22:05:44 history -r
3 2024-01-05 22:05:44 vim