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 |
Limit the number of outputs from recent commands |
只需运行 history 命令即可在 Linux 终端中查看所有以前执行的命令的列表:

除了查看命令历史记录之外,您还可以管理命令历史记录并执行修改先前执行的命令、反向搜索命令历史记录甚至完全删除历史记录等操作。
如何修改以前执行的 Linux 命令
如前所述,您执行的每个命令都存储在命令历史记录中。要修改或重用之前执行的命令,可以使用以下任一步骤:-
使用命令历史记录扩展
感叹号“!”与“事件指示符”配对时扩展了 bash 历史记录。您可以使用的一些事件指示符是 –
- !!– 将执行的最后一个命令插入 shell 提示符。
- !
– 将执行的第 n 个命令插入 shell 提示符。 - !
– 指最近执行的。 - !?
– 指包含 的最新命令。
如何查看和管理 Linux 命令历史记录
使用反向命令搜索
有时,滚动浏览数千个命令的整个列表以搜索特定命令是不可行的。为此,您可以使用带有“CTRL + r”的反向命令搜索。它将打开一个新提示。在这里键入要搜索的命令,将显示之前执行的完整命令。按键盘上的回车键执行命令。

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

如何在 Linux 中修改命令历史记录约束
在 Linux 上,bash 命令历史记录会根据一些限制条件进行适当保存。系统环境变量包含了所有这些约束,并且可像用户定义的环境变量一样进行修改。
限制存储的命令数
要限制存储在历史记录中的命令数量,请使用以下语法设置 HISTSIZE 环境变量 –
export HISTSIZE=<max_commands_to_store>
例如,要仅存储最近的 1000 个命令 –
export HISTSIZE=1000

忽略特定命令
要从命令历史记录中保存的特定命令中排除,请将 HISTIGNORE 环境变量与此语法一起使用 –
export HISTIGNORE=<command1:command2:command3>
在这里,命令“command1”,“command2”和“command3”被设置为从bash命令历史记录中排除。例如,要排除命令 ls、cd 和 exit –
export HISTIGNORE='ls:cd:exit'

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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!