history
命令可以显示所有结果,但我们可以使用 history | grep searchingCommand
来筛选获取特定的命令。这真的很有帮助。
但问题是它还会显示那些输入错误或无效的命令。然后要找到正确的命令就很麻烦。我查看了这个链接:终端中选择性的命令历史记录? 但那不是我的解决方案。
所以,有没有办法从历史记录中删除那些在输入时或之后是错误的命令呢?
history
命令可以显示所有结果,但我们可以使用 history | grep searchingCommand
来筛选获取特定的命令。这真的很有帮助。
但问题是它还会显示那些输入错误或无效的命令。然后要找到正确的命令就很麻烦。我查看了这个链接:终端中选择性的命令历史记录? 但那不是我的解决方案。
所以,有没有办法从历史记录中删除那些在输入时或之后是错误的命令呢?
使用:
history -d OFFSET
即使在将历史记录条目添加到您的bash历史文件之前,也可以删除偏移量为OFFSET
的历史记录条目。要找到正确的OFFSET
,您只需要运行history
命令。这是包含您想要删除的历史记录条目的行的起始处的数字。
要保存对历史记录的修改,请使用:
history -w
history -w
命令来保存更改。另外,OFFSET表示history
显示的行号。如果你想删除的行号是873,那么请使用history -d 873
命令。 - cubecubedhistory -d
命令后,使用history -w
来保存更改。 - cubecubedhistory -w
? - Midhun KMOFFSET
是在运行history
命令时看到的命令编号。所以,如果你运行history | tail
并且看到了10个命令,倒数第二个命令是569 ./shouldnt_be_in_history -p password
,那么你只需运行history -d 569
来删除它。 - s g ~/.bash_history
并删除有错别字的命令。gedit ~/.bash_history
sudo -i
inser your password
gedit ~/.bash_history
history -c
命令来完成。
SYNTAX
history history [n] history -c history -d offset history [-anrw] [filename] history -ps arg
KEY
-c Clear the history list. This may be combined with the other options to replace the history list completely.
-d offset Delete the history entry at position offset. offset should be specified as it appears when the history is displayed.
-a Append the new history lines (history lines entered since the beginning of the current Bash session) to the history file.
-n Append the history lines not already read from the history file to the current history list. These are lines appended to the history file since the beginning of the current Bash session.
-r Read the current history file and append its contents to the history list.
-w Write out the current history to the history file.
-p Perform history substitution on the args and display the result on the standard output, without storing the results in the history list.
-s The args are added to the end of the history list as a single entry.
来源:
.bash_history
的命令来删除条目? - Saurav Kumarhistory -c
不起作用。当我关闭终端并重新打开时,历史记录并没有被删除。对于你的回答给出-1评分。 - αғsнιηhistory -c
后使用 history -w
命令来保存更改。 - cubecubed我在原始回答中描述的方法并不真正安全。请向本文底部滚动以了解更多信息。
(原帖)
我想要增加另一种修改(或删除)历史记录条目的方法,这是我在使用bash时偶然发现的:
为了证明这一点,请在bash中执行以下三个命令:
$ echo 1
1
$ echo 2
2
$ echo 3
3
echo 1
,然后将其更改为echo 1 - changed
,但不要按回车键。如果你继续移动历史记录,这一行将保持修改后的状态,并且你可以离开它然后返回。现在移动到echo 2
这一行,将其更改为echo 2 - changed
,同样不要按回车键。为了保存对这些行的更改,选择除了这两行之外的任何命令,并按下Ctrl+c。(由Saurav Kumar于02/06/2018添加)
最简单的方法:
注意:
这只会更改当前会话的命令。如果我们想要更改旧的命令并保存更改,需要在关闭终端之前运行以下命令:
history -w
(于2023年03月05日添加)
假设你刚刚以错误的方式输入并执行了一个命令,而你想要更改或删除历史记录中的错误行。那么你可能会去到那一行并进行更改(就像修正拼写错误一样),使其包含正确的命令,然后按下↓和Ctrl+c,这样正确的命令就保存在那一行上了。然而,如果你现在去执行那一行,旧的行将重新出现在原来的位置。
解决这个问题的一个方法是:前往错误的行并进行更正,然后按下Alt+#。这将把正确的命令作为注释附加到当前的历史记录列表中。现在你可以按照上述描述的方式删除错误的行。之后,你可以通过按下↑、Home、Del、↵(或Ctrl+p、Ctrl+a、Ctrl+d、↵)来执行修正后的命令。
警告:在执行删除命令时,务必避免按下↵或关闭终端,否则该命令将被保存到历史文件中。消除此风险的一种方法是使用
history -d -2
ctrl+r
搜索你想要删除/修改的命令。
2. 使用 end
移动命令并删除/修改该命令。
3. 使用 ctrl+n
跳转到下一行,然后
4. 使用 ctrl+c
- Saurav KumarPROMPT_COMMAND='history -a'
然后重新启动您的终端。
您可以将其添加到.bashrc文件的任何位置。我已将其添加如下,与其他历史相关内容一起。
通常在一个 bash 会话中,执行的命令直到会话结束才写入 .bash_history 文件,因此PROMPT_COMMAND='history -a'
命令将立即将命令写入 .bash_history 文件。sed -i '$d' ~/.bash_history
然后,它就会被删除了。
为了简化操作,你可以给它取一个更简单的别名,并使用它,比如
alias rh ='sed -i '\''$d'\'' ~/.bash_history'
rh
将从历史记录中删除最后一个执行的命令。alias rh = 'sed -i '\''$d'\'' ~/.bash_history'
to .bashrc
注意
等号两边不应有任何空格
如果您不想使用别名,也可以按照以下步骤操作
创建一个名为rh
的命令,并将其放置在/bin
目录中:
打开一个文件,命名为rh
,并粘贴以下代码,保存并关闭:
sed -i '$d' ~/.bash_history
使rh
可执行,并将其放置在/bin
目录中:
chmod +x rh sudo cp rh /bin
现在可以使用rh
命令从历史记录中删除最近的命令。
.bashrc
文件中添加PROMPT_COMMAND='history -a'
的位置在哪里?它具体会做什么?我尝试了sed -i '$d' ~/.bash_history
,它可以正常工作而不需要编辑.bashrc
!但是别名(alias)的功能却无法使用...如果你能详细解释我的问题并使别名功能正常工作,我将接受你的答案.. :) - Saurav Kumarsed -i "$d" ~/.bash_history
,现在它正常工作了。我接受你的答案。。:) - Saurav Kumarhistory
命令进行检查): for i in `seq 1974 1990` ; do history -d 1974 ; done
dd
后,我必须再键入j
或k
才能使命令从历史记录中真正清除。如果我输入另一个命令,或者只是按下Enter
键,bash似乎会认为它只是一个新命令,并且应该保留旧命令在历史记录中。有趣的是,当它做对了时,它会清除命令,但不会删除它,结果就是空白的历史记录条目。但这已经足够好了,可以删除我在命令行中被迫输入的密码,比其他答案要简单得多,所以非常感谢您发布这个! - Michael Scheper