简介
它默认能记住我们敲过的前1000个命令。
这么这个1000条是如何定义的呢?它在/etc/profile中定义。我们可以更改它的值,之后执行source/etc/profilelinux运维博客,或则重新进shell来更新到shell环境中。
提早解释一些相关的命令:
文章多次说到source和export命令,她们的功能很像。
source命令将更改后的脚本文件重新执行一遍linux 命令里支持变量吗,并将相关变量放在全局变量中。
export命令将变量放在全局变量中,并且仅在当前终端下有效。
查找
命令:history。
[root@localhost ~]# history|tail -n 5
1019 20221206 142154 chattr +a .bash_history
1020 20221206 142202 ls -l ~/.bash_history
1021 20221206 142216 lsattr .bash_history
1022 20221206 142225 rm -rf .bash_history
1023 20221206 142606 history|tail -n 5
删掉
非永久删掉,只是从显存中删掉。
命令:history-c。
1023 20221206 142606 history|tail -n 5
[root@localhost ~]# history -c
[root@localhost ~]# !hi
-bash: !hi: event not found
[root@localhost ~]# history
1 20221206 142642 history
保存
保存之前储存在显存中。退出终端会保存到~/.bash_history这个文件中。
最好给~/.bash_history文件加上隐藏a权限linux 命令里支持变量吗,让它只能追加,避免被他人篡改。
[root@localhost ~]# chattr +a .bash_history
[root@localhost ~]# ls -l ~/.bash_history
-rw-------. 1 root root 15739 12月 6 14:00 /root/.bash_history
[root@localhost ~]# lsattr .bash_history
-----a-------------- .bash_history
[root@localhost ~]# rm -rf .bash_history
rm: 无法删除'.bash_history': Operation not permitted
修改history显示格式
第一种方式linux cp,直接在终端执行。
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
这些方式指使在本次shell中生效。
永久生效的方式:
更改~/.bashrc文件,添加:
HISTIMEFORMAT="%Y-%m-%d %H:%M:%S "
几个常用的history选项
喜欢阿文的文章,就给个免费的关注吧,您的支持是我持续创作的动力哦~