Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use the history command in Linux

Shulou Source: shulou.com Published: 2022-06-01 06:03:45 10月05日 Update

This article mainly introduces how to use the history command in Linux, the article is very detailed, has a certain reference value, interested friends must read it!

Use powerful history commands to make your command prompt more efficient.

As I spend more and more time on the terminal, I feel like I'm constantly looking for new commands to make my daily tasks more efficient. GNU's history command is a command that really changes my daily work.

The GNU history command saves a list of all other commands run from the terminal session and then allows you to replay or reuse those commands without having to re-enter them. If you are a veteran player, you know the power of history, but for us half-baked or novice system administrators, history is an immediate productivity gain.

History 101

To view the command history, open the terminal program in Linux and enter:

$history

This is the response I got:

1 clear2 ls-al3 sudo dnf update-y4 history

The history command displays a list of commands entered since the start of the session. What's interesting about history is that you can replay any command using the following command:

$! 3

The! 3 prompt tells shell to rerun the third command in the history list. I can also enter the following command to use:

Linuser@my_linux_box:! sudo dnf

History will search for a command that matches the pattern you provide and run it.

Search history

You can also enter! Rerun a command in the command history. Also, by pairing with grep, you can search for commands that match the text pattern, or by using it with tail, you can find several commands that you execute. For example:

$history | grep dnf3 sudo dnf update-y5 history | grep dnf$ history | tail-n 34 history5 history | grep dnf6 history | tail-n 3

Another way to do this is to type Ctrl-R to invoke the recursive search of your command history. After entering, the prompt changes to:

(reverse-i-search) `':

Now you can start typing a command, and a matching command will be displayed, press enter to execute.

Change executed commands

History also allows you to rerun commands using different syntax. For example, if I want to change my previous command history | grep dnf to history | grep ssh, I can execute the following command at the prompt:

$^ dnf ^ ssh ^

History will rerun the command, but replace dnf with ssh and execute it.

Delete History

Sometimes you want to delete some or all of your history. If you want to delete a specific command, enter history-d. To clear the history, execute history-c.

The history file is stored in a file that you can modify. Bash shell users can find .bash _ history in their home directory.

The above is all the contents of the article "how to use history commands in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Command history input run prompt search prompt terminal content file pattern article different powerful effective interesting instant continuous not used value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Information Linux Microsoft