How to view and manage Linux command history
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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Are you looking for the five lighthouses in the History of the Sinking Tide quest? This guide will give you a detailed explanation of where these lighthouses are waiting to be discovered. We hope this will help you find the lighthouse you need quickly and complete your mission successfully! The history of the sinking of the Mingtide Five lighthouse locations are introduced and the specific locations are listed: 1. The first lighthouse: Please go to the barren stone highland, located directly above Beiluoye. 2. The second lighthouse: Next, please go to the Zhongqu Platform, which can be found around the teleportation point on the northeast side. 3. The third lighthouse: Please go to the southeast of Hukou Mountains and find it along Wuming Bay. 4. The fourth lighthouse: Please go to the teleportation point at the southeastern end of the Angry Birds Zee, near the cliff. 5. The fifth lighthouse: Please go to the first silent zone of the Lightless Forest, and you will find it on the edge of the cliff.

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features