出于安全原因,您可能希望清除历史文件和屏幕。某些 Linux 发行版可能会在您注销时清除屏幕,而其他发行版则不会。许多程序一次将输入读取为一行。

GNU 历史库能够跟踪这些行,将任意数据与每一行相关联,并利用来自先前行的信息来组成新行。Bash 和其他 shell 可能会使用这个历史库。默认文件是 ~/.history 或 ~/.bash_history。

Bash 的历史功能

bash 的历史函数依赖于一个名为HISTFILE的变量,通常设置为当前用户的.bash_history文件(位于用户的主目录中)。当回显时,它返回用户历史文件的完整路径和名称,如下所示:

$ echo $HISTFILE
/home/franhesco/.bash_history

1)删除Linux历史命令

历史可以用一些命令重置,但操作后,如果你注销并登录到你的shell,你会看到相同的历史

一种。删除之前的命令

您可以使用history -ccommand 清除当前 shell 中的先前历史命令。如果您刚刚输入了密码并且没有退出该 shell 或明确保存其历史记录,这就足够了(但太过分了)。

下面的例子显示了我们当前的历史

$ history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 history

现在让我们使用命令。

$ history -c
$ exit

当您退出 bash 时,历史将保存到历史文件中。在当前会话期间创建的历史记录被附加到文件中,已经存在的条目不受影响。让我们来看看新的历史

$ history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 exit
 5 history

让我们在历史中添加一些命令。

$ history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 exit
 5 history 
 6 ls -l
 7 mkdir aa
 8 ls -li
 9 history

要使用当前 shell 的历史记录覆盖历史文件,请history -whistory -c命令之后运行

$ history -c
$ history -w
$ exit

再次登录后,让我们检查一下我们的历史记录

$ history 
 1 history -w
 2 exit
 3 history

你可以看到我们的历史开始于history -wentry 命令。

湾 删除单个命令

您可以使用该-d选项删除不需要的历史记录条目。这将删除位置偏移处的历史条目。但是当您关闭终端并再次打开它时,历史记录不会被删除。所以我们最后使用 history -w来保存更改。

# history -d offset

举些例子:

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget username:password@private.ftp.com/secret/file.tar.gz
 6 mkdir

现在,如果要删除mkdir 命令的第六个条目,只需使用:

# history -d 5
# history -w

查看结果

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget username:password@private.ftp.com/secret/file.tar.gz
 6 history
 7 history -d 6

您可以看到我们上面没有 mkdir 命令条目。

2)彻底清除bash

要完全清除服务器上的 bash 历史记录,另一种解决方案是链接~/.bash_history/dev/null

$ ln -sf /dev/null ~/.bash_history

然而,一个令人讨厌的副作用是历史条目链接到内存,当您注销时它会刷新回文件。要解决此问题,您可以使用以下命令:

ln -sf /dev/null ~/.bash_history && history -c && exit

3) 关闭 bash 历史记录

您可以使用以下两种方式之一停止记录历史记录:为所有用户关闭它,或为单个用户关闭记录历史记录。

一种。为所有用户关闭

您可以关闭所有用户unset HISTFILE/etc/profile文件中添加行的 bash 历史记录。此行停用系统上每个用户的历史文件

# echo "unset HISTFILE" >> /etc/profile

您需要有权限才能应用上面的命令

湾 为特定用户关闭

上面的命令,可以关闭特定用户的 bash 历史记录。你只需要指明他的 bash_profile 文件。

# echo "unset HISTFILE" >> /etc/profile

每个用户都将登录,他的历史记录将被重置如下。他所有的历史命令都会保存到用户注销

$ history 
 1 history

C。编辑 .bashrc

您可以通过编辑历史命令参数的两个值来删除历史命令。

  • HISTSIZE 这是在 bash 会话进行时存储在历史列表中的内存中的行数或命令数
  • HISTFILESIZE 保存历史堆栈写入历史文件时使用的行数。

为此,请编辑您的.bashrc并添加

HISTFILESIZE=0
HISTSIZE=0

现在,您可以使用上面列出的任何命令成功删除 bash 历史记录,甚至可以停止记录到 bash 历史记录。

4) 删除一些条目行

您可以使用history -d offsetbuilt-in 从当前 shell 的历史记录中删除特定行。如果您想删除一系列行,这并不实用,因为它只将一个偏移量作为参数,但您可以用循环包装它。

命令提示符中的这一行会有所帮助。

for i in {1..N}; do history -d START_NUM; done

其中START_NUM是历史记录中条目的起始位置N是您可能想要删除的条目数。

让我们检查历史命令的最后 15 个条目

# history 15
 986 stat synchro 
 987 ln synchro sync2
 988 stat synchro 
 989 debugfs /dev/vda
 990 exit
 991 echo $HISTFILE
 992 cat .bash_history 
 993 ls -l .bash_history 
 994 passwd patrick
 995 echo "unset HISTFILE" >> /home/papso/.bash_profile
 996 history 
 997 for i in {1..10}; do history -d 867; done
 998 history 
 999 history 30
 1000 history 15

现在让我们删除从 986 行开始的 13 个条目

for i in {1..13}; do history -d 986; done

现在让我们检查结果

# history 15
 975 ls -il /home/bobbin/sync.sh filesync 
 976 ls -l /home/bobbin/sync.sh filesync 
 977 ln /home/bobbin/sync.sh synchro
 978 ls -il /home/bobbin/sync.sh synchro 
 979 cp script script2
 980 ls -il script script2
 981 stat script
 982 stat script2
 983 man find
 984 find / -inum 517333
 985 stat filesync 
 986 history 30
 987 history 15
 988 for i in {1..13}; do history -d 986; done
 989 history 15

你可以看到我们没有相同的结果。

当用户使用登录或交互式/非登录 shell 登录时,将打开用户的.bash_history文件。现在可以使用一些有用的命令对这个文件进行操作。历史记录对于检索以前使用过的命令很有用,但您可能需要删除这些使用过的命令的一些条目。该~/.bash_history文件不会记录您在响应其他程序的提示时键入的内容,而只会记录您在 bash 提示中键入的内容。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐