「四川联想代理」如何从Linux/Unix Bash shell上删除历史记录中的一条命令?

成都联想服务器总代理 141 2023-05-29 https://www.lenovocd.com/ 成都联想服务器总代理

在Ubuntu的bash终端应用中工作,在云平台的RHEL服务器上远程工作。四川联想服务器代理我输入了一个错误的、危险的命令,这个命令我已经不想再记得了。如何删除历史文件中的危险命令?如何从bash历史文件中删除或删除单个命令?

成都联想服务器经销商

你可以使用历史记录命令来清除所有的历史记录或选定的命令行。在本教程中,您将学习如何在Linux、MacOS和类似Unix的系统中清除bash历史记录中的特定命令。

如何查看行号的历史记录?

只需输入history命令:

$ history

默认情况下,历史记录存储在~/.bash_history文件中。但是,名为$ HISTFILE的bash变量定义了保存命令历史记录的文件名。如果未设置,则退出shell时不会保存命令历史记录。使用printf命令/ echo命令显示当前设置:

echo $HISTFILE

printf Bash history file - %s $HISTFILE

如何在Linux中删除历史记录中的单条命令编号为1013的命令

## Delete the bash history entry at offset OFFSET ##

history -d offset

history -d number

history -d 1013

验证一下:

$ history

Linux bash shell从历史文件中删除单个命令

在位置OFFSET处删除bash历史记录条目。负偏移量从历史记录列表的末尾算起。

如何删除所有历史记录?

想删除所有的bash历史记录?试试下面的语法命令:

$ history -c

添加上面的命令到你的〜/ .bash_logout中的文件,以便在注销的时候进行清理:

$ cat /dev/null ~/.bash_history history -c

将-c传递给history命令删除所有条目$ {} HISTFILE要清除历史记录列表。

提示:像专家一样控制bash历史记录

首先,您可以通过在〜/ .bashrc文件中附加以下配置选项来增加bash历史记录的大小:

## Set the maximum number of lines contained in the history file ##

HISTFILESIZE=5000000

## Set the number of commands to remember in the command history ##

HISTSIZE=10000

## Append it ##

shopt -s histappend

######

# Controlling how commands are saved on the history file ##

# ignoreboth means: ##

# a) Command which begin with a space character are not saved in the history list ##

# b) Command matching the previous history entry to not be saved (avoid duplicate commands) ##

######

HISTCONTROL=ignoreboth

保存并关闭文件。

在哪里可以找到有关历史命令的更多信息?

通过键入以下命令来阅读bash手册页:

$ man bash

另一个选项是键入以下命令:

$ help history

示例输出:

结论

在本快速教程中,您学习了从在Linux,macOS,* BSD或类似Unix的系统上运行的bash历史记录中删除单个命令。

成都联想代理www.lenovocd.com


【公司名称】成都鸿盛广达科技有限公司

【代理级别】成都联想服务器总代理

【销售经理】成都鸿盛广达科技有限公司

【联系方式】座机:028-85952921 手机:13981931555

【公司地址】成都市武侯区人民南路四段一号时代数码广场A座17楼