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

The method of using history command in Linux

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

Share

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

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:

Clear

Ls-al

Sudo dnf update-y

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 the last command that matches the pattern you provide and run it.

Search history

You can also enter! Rerun the last 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 the last few commands you execute. For example:

$history | grep dnf sudo dnf update-y history | grep dnf$ history | tail-n 3 history history | grep dnf 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.

Next step

There are many other things you can do with history:

Set the history buffer to the date and time of each row in a certain number of records to prevent certain commands from being recorded in the history

For more information about the history command and other interesting things, please refer to https://www.gnu.org/software/bash/manual/.

Summary

The above is the method of using history command in Linux introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!

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