Home > Common Problem > body text

How to view and manage Linux command history

WBOY
Release: 2023-08-01 21:17:30
forward
5721 people have browsed it

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 
Copy after login

Some options paired with the history command include:

Options Description
-cClear the command history of the current session
-w Write command history to file
-rReload command history from history file
-n Limit the number of outputs from recent commands

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

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

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

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

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

使用命令历史记录扩展

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

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

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

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

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

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

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

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

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

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

限制存储的命令数

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

export HISTSIZE=
Copy after login

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

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

忽略特定命令

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

export HISTIGNORE=
Copy after login

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

export HISTIGNORE='ls:cd:exit'
Copy after login
如何查看和管理 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!

Related labels:
source:yundongfang.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!