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

What are the tips for using history on the Linux command line

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will share with you about the tips for using history on the Linux command line. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Use HISTTIMEFORMAT to display timestamps

When you enter history on the command line, it displays the sequence number and the command. For audit purposes, it may be helpful to display the timestamp with the command.

[root@localhost ~] # export HISTTIMEFORMAT='%F% T'[root@localhost ~] # history | less

two。 Use Ctrl + r to search for history

When you have executed a long command, you can simply use the keyword to search the history and re-execute the same command without having to type it completely. Press Ctrl + r and type the keyword. In the following example, I searched for wget, which shows the previous command "wget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo"" in the history that contains the word wget.

# when the ctrl + r key is pressed, the following state appears. (reverse-i-search) `': # then type wget, followed by a command to filter out matching history commands. If unwanted commands are displayed, you can continue to supplement the command line. (reverse-i-search) `wget': wget-O / etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

When the desired command line is searched, you can press the Enter enter key to execute the command.

Filter out the matching history command. If you want to change the parameters, press the tab key, or the direction key, exit the search mode to modify the history command, and execute it.

3. Repeat the last command quickly

It can be used! Two exclamation marks, execute the previous command. You can also use!-1 to execute the previous command.

4. Execute specific commands from history

Can be used! Add a number and execute the command that specifies the line number in history.

[root@localhost ~] #! 4 ss-tul Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process udp UNCONN 0 0 127.0.1 tul Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process udp UNCONN 323 0.0.0.0 udp UNCONN 0 0 [: 1]: 323 [:]: * tcp LISTEN 0 128 0.0.0.0:ssh 0.0.0.0 tcp LISTEN 0 128 [: Ssh [::]: *

5. Use HISTSIZE to control the total number of rows in the history

By default in Centos8, history stores 1000 history records. You can view it using the following command:

[root@localhost ~] # echo $HISTSIZE 1000

Add the following two lines to .bash _ profile, and then execute source ~ / .bash_profile to take effect. In the following command, make the history storage history less, storing only 200 entries.

[root@localhost ~] # echo "HISTSIZE=200" > .bash _ profile [root@localhost ~] # echo "HISFILETSIZE=200" > > .bash _ profile [root@localhost ~] # source ~ / .bash

The HISTSIZE variable controls the number of history commands displayed.

The HISTFILESIZE variable controls the number of history records in the .bash _ history file.

6. Change the history file name

By default, the history file is called .bash _ history, and its file name can be changed using the HISTFILE variable, and the HISTFILE variable can be written to the ~ / .bash_profile file, as shown in the following example:

[root@localhost ~] # echo "HISTFILE=/root/.cmd_hist" > > ~ / .bash_profile [root@localhost ~] # reboot

After the addition is complete, restart the operating system, and you can see that the new history file is ready to store the history.

7. Clear History

Use the-c option to clear the history:

[root@localhost] # history-c

8. Disable History

If you want to disable history altogether, set HISTSIZE and HISTFILESIZE to 0, as shown below.

[root@localhost ~] # echo "HISTSIZE=0" > .bash _ profile [root@localhost ~] # echo "HISFILETSIZE=0" > > .bash _ profile [root@localhost ~] # source ~ / .bash

Thank you for reading! This is the end of this article on "what are the skills for using history on the Linux command line?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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