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 techniques related to using history on the Linux command line

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you to the Linux command line using history-related skills, the content is very detailed, interested friends can refer to, I hope to help you.

When you use the Linux command line frequently, using history effectively can greatly improve productivity.

When history is entered on the command line, it displays the serial number and command. For audit purposes, it may be helpful to display the timestamp along with the command. 1. Display timestamps using HISTTIMEFORMAT

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

2. Ctrl + r Search History

When you have executed a long command, you can simply search the history using keywords and re-execute the same command without having to type it all in. 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 containing the word wget.

#When you press Ctrl + R, the following states appear: (reverse-i-search)``: #Then type wget, followed by a command to filter out matching historical commands. If you don't want the command 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 found, press Enter to execute the command.

Filter out matching historical commands. To change parameters, press the tab key or the arrow keys. You can exit search mode to modify this historical command and execute it.

3. Repeat the last command immediately.

It can be used! Two exclamation marks. Execute the last command. It can also be used!- 1. Execute the previous command.

4. Execute specific commands from history

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

[root@localhost ~]# ! 4ss -tulNetid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process udp UNCONN 0 0 127.0.0.1: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 history

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

[root@localhost ~]# echo $HISTSIZE1000

Add the following two lines to.bash_profile and execute source ~/.bash_profile to take effect. In the following command, make history store fewer history records, only 200.

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

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 History File Name

By default, the file name of the history record is.bash_history. You can change its file name using the HISTFILE variable. You can write the HISTFILE variable to the ~/.bash_profile file. 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 can already store history.

7. clear history

Use the-c option to clear 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_profile

About the Linux command line using history-related skills is what to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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

Development

Wechat

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

12
Report