📅  最后修改于: 2023-12-03 15:27:02.557000             🧑  作者: Mango
在使用bash shell时,所有的命令都会被记录在历史记录文件中,方便我们以后查找使用。但是有时候我们希望删除部分或全部的历史记录,本文将介绍如何清除bash历史记录。
在bash shell中,我们可以使用history命令查看所有的历史记录。对于某条不希望留在历史记录中的命令,可以使用history命令输出的序号,通过history -d
命令来删除该条历史记录。
$ history
1 ls -al
2 cd Documents/
3 vim test.txt
4 cat test.txt
5 exit
6 history
$ history -d 3 # 删除序号为3的历史记录
$ history
1 ls -al
2 cd Documents/
3 cat test.txt
4 exit
5 history
如果希望清除所有的历史记录,可以使用history -c
命令。
$ history -c # 清除所有历史记录
$ history
$
有时候我们希望某些命令不被记录进历史记录中,可以在命令前添加空格或使用history -a
命令将命令写入历史记录文件(默认为~/.bash_history
),然后使用history -c
命令清空历史记录。
$ echo "Hello World!" # 这条记录会被记录在历史记录中
Hello World!
$ echo "Hello World!" # 在命令前添加空格,不会被记录
Hello World!
$ history -a mycommand.sh # 将命令写入历史记录文件中
$ history -c # 清空历史记录
$ history
$
如果希望每次退出bash shell后都清空历史记录,可以在~/.bashrc
文件中加入以下行:
unset HISTFILE
或者使用如下命令:
$ echo 'unset HISTFILE' >> ~/.bashrc
这将禁止对所有命令进行历史记录,并在退出bash shell时清空历史记录。
如果希望在bash shell关闭时自动清空历史记录,可以使用如下命令:
$ echo 'history -c' >> ~/.bash_logout
这将在每次退出bash shell时清空历史记录。
以上就是删除和清空bash历史记录的方法,希望对您有所帮助。