history:命令历史
SHELL进程会在bash退出时保存此前用户执行过的命令到 KaTeX parse error: Expected 'EOF', got '#' at position 26: …_history 文件中。 #̲HOME:当前用户家目录
HISTFILE:可以保存1000条命令(可以更改), echo $HISTFILESIZE查看
环境变量:HISTCONTROL
ignoredups :忽略重复的命令
ignorespace :忽略以空白开头的命令
ignoreboth :以上两者同时生效
[root@host ~]# help history
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]
显示或操纵历史列表。
带行号显示历史列表,将每个被修改的条目加上前缀 `*'。
参数 N 会仅列出最后的 N 个条目。
选项:
-c 删除所有条目从而清空历史列表。
-d 偏移量 从指定位置删除历史列表。
-a 将当前绘画的历史行追加到历史文件中
-n 从历史文件中读取所有未被读取的行
-r 读取历史文件并将内容追加到历史列表中
-w 将当前历史写入到历史文件中
并追加到历史列表中
-p 对每一个 ARG 参数展开历史并显示结果
而不存储到历史列表中
-s 以单条记录追加 ARG 到历史列表中
# !n:执行列表中第n条命令
# !!:再次执行上一条命令
# !string :执行最近一次以string开头的命令
[root@host ~]# echo $HISTFILE
/root/.bash_history
[root@host ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .pki .viminfo
.. .**bash_history** .bash_profile .cshrc .tcshrc
[root@host ~]# cat .bash_history
grep --help
hich
which
which grep
...
[root@host ~]# history #history查看的是当前用户执行的命令,还未保存之文件中
1 2020-05-02 15:44:47man history
2 2020-05-02 15:44:52ls
3 2020-05-02 15:44:55cd
4 2020-05-02 15:44:56ls
5 2020-05-02 15:45:00ls -a
6 2020-05-02 15:45:09ls .bash_history
7 2020-05-02 15:45:19cat .bash_history
8 2020-05-02 16:09:23vim /etc/profile
9 2020-05-02 16:24:30history
10 2020-05-02 16:24:43source /etc/profile
[root@host ~]# history -c #清空
[root@host ~]# history
1 2020-05-02 23:43:29history
[root@host ~]# history
1 2020-05-02 23:43:29history
2 2020-05-02 23:44:25cat /tmp/file
3 2020-05-02 23:44:36echo "hello world"
4 2020-05-02 23:44:40history
[root@host ~]# history -d 2 #删除第二条
[root@host ~]# history
1 2020-05-02 23:43:29history
2 2020-05-02 23:44:36echo "hello world"
3 2020-05-02 23:44:40history
4 2020-05-02 23:44:47history -d 2
5 2020-05-02 23:44:49history
[root@host ~]# history -r
[root@host ~]# history
...
922 2020-05-02 23:44:36echo "hello world"
923 2020-05-02 23:44:40history
924 2020-05-02 23:44:47history -d 2
925 2020-05-02 23:44:49history
926 2020-05-02 23:47:32history -a
927 2020-05-02 23:51:36history
---
[root@host ~]# history -c
[root@host ~]# ls
anaconda-ks.cfg
[root@host ~]# echo "hello world !"
-bash: !": event not found
[root@host ~]# echo "hello world! "
hello world!
[root@host ~]# history -w #将当前命令历史写入文件中
[root@host ~]# tail .bash_history
#1588435072
ls
#1588435106
echo "hello world! "
#1588435111
history -w
配置history : /etc/profile
[root@host ~]# vim /etc/profile
...
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000 #命令历史文件保存1000条,超出的部分会自动删除旧命令,可以再次修改
if [ "$HISTCONTROL" = "ignorespace" ] ; then #忽略空白开头的命令
export HISTCONTROL=ignoreboth #忽略重复的命令以及以空白开头的命令
else
export HISTCONTROL=ignoredups #忽略重复的命令
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " #history结果会显示年月日时分秒
...
-------------------------------------------------------------------------------------------------------返回目录