Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the history command in Linux

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report