Home  >  Article  >  How to view and manage Linux command history

How to view and manage Linux command history

WBOY
WBOYforward
2023-08-01 21:17:305819browse

How to view command history in Linux

We can use the history command in Linux to get a list of all previously executed commands. It has a very simple syntax:

history <options>

Some options paired with the history command include:

Options Description
-c Clear the command history of the current session
-w Write command history to file
-r Reload command history from history file
-n 9e1bed15a336cd4e2b3e965d4a4b11b4 Limit the number of outputs from recent commands

只需运行 history 命令即可在 Linux 终端中查看所有以前执行的命令的列表:

如何查看和管理 Linux 命令历史记录

除了查看命令历史记录之外,您还可以管理命令历史记录并执行修改先前执行的命令、反向搜索命令历史记录甚至完全删除历史记录等操作。

如何修改以前执行的 Linux 命令

如前所述,您执行的每个命令都存储在命令历史记录中。要修改或重用之前执行的命令,可以使用以下任一步骤:-

使用命令历史记录扩展

感叹号“!”与“事件指示符”配对时扩展了 bash 历史记录。您可以使用的一些事件指示符是 –

  • !!– 将执行的最后一个命令插入 shell 提示符。
  • !751fecf49c9d13ca89ee2cbb9b75d4f6 – 将执行的第 n 个命令插入 shell 提示符。
  • !e2b9f03cad787b9644e1f51fd23b7dac – 指最近执行的99a2c3ea6fd7e6f38f79ffc1b9e66846。
  • !?98c455a79ddfebb79781bff588e7b37e – 指包含98c455a79ddfebb79781bff588e7b37e的最新命令。
  • 如何查看和管理 Linux 命令历史记录

如何查看和管理 Linux 命令历史记录

有时,滚动浏览数千个命令的整个列表以搜索特定命令是不可行的。为此,您可以使用带有“CTRL + r”的反向命令搜索。它将打开一个新提示。在这里键入要搜索的命令,将显示之前执行的完整命令。按键盘上的回车键执行命令。

如何查看和管理 Linux 命令历史记录

如何在 Linux 中清除命令历史记录

要清除整个命令历史记录,请将 -c 标志与历史记录命令一起使用。

history -c
如何查看和管理 Linux 命令历史记录

如何在 Linux 中修改命令历史记录约束

在 Linux 上,bash 命令历史记录会根据一些限制条件进行适当保存。系统环境变量包含了所有这些约束,并且可像用户定义的环境变量一样进行修改。

限制存储的命令数

要限制存储在历史记录中的命令数量,请使用以下语法设置 HISTSIZE 环境变量 –

export HISTSIZE=<max_commands_to_store>

例如,要仅存储最近的 1000 个命令 –

export HISTSIZE=1000
如何查看和管理 Linux 命令历史记录

忽略特定命令

要从命令历史记录中保存的特定命令中排除,请将 HISTIGNORE 环境变量与此语法一起使用 –

export HISTIGNORE=<command1:command2:command3>

在这里,命令“command1”,“command2”和“command3”被设置为从bash命令历史记录中排除。例如,要排除命令 ls、cd 和 exit –

export HISTIGNORE='ls:cd:exit'
如何查看和管理 Linux 命令历史记录

The above is the detailed content of How to view and manage Linux command history. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yundongfang.com. If there is any infringement, please contact admin@php.cn delete