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 practical techniques for Linux to use history to reduce repetitive commands

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

Share

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

Today, I will talk to you about the practical skills of Linux using history to reduce repeated commands, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

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

In the usual Linux operation process, many commands are repeated, you must not want to enter a large number of duplicate commands. If you are a system administrator, you may need to audit user actions, and it is important to manage the history of Linux commands.

1 basic principles

The history of the Linux command is persisted, and the default location is the .bash _ history file of the current user's home directory.

When the Linux system starts a Shell, Shell reads the history from the .bash _ history file and stores it in the corresponding memory buffer.

The Linux commands we normally operate are recorded in the buffer. Historical command management, including those executed by the history command, manipulates the buffer rather than the .bash _ history file directly.

When we exit Shell, such as pressing Ctrl+D, the Shell process writes the contents of the history buffer back to the .bash _ history file.

2 detailed explanation of use

Now that we understand the fundamentals of history, let's learn how to use it in detail.

(1) basic usage

Enter the history command directly, and you can see that all the most recent commands are displayed.

$history 1 bash 2 ls 3 vim .bash _ history 4 cat .bash _ history 5 history 6 bash

Sometimes I don't need to show all the history commands, only the last 10 history records. I can add the number N after the command.

$history 10

Normally, the buffer contents are saved to a file only when Shell exits normally. If you want to actively save the history of the buffer, execute the-w option

$history-w

Of course, if you perform some sensitive command operations, you can execute-c to delete the buffer contents directly.

$history-c

(2) repeated execution of orders

If you want to repeat some commands, you can use! To quickly execute repeated commands.

For example, if you repeat the 1024 history command, you can execute the following command

$! 1024

The number of 1024 can be checked through history.

Repeat the previous command

$!

Repeat the penultimate 6 history command, which can be represented by a negative number, and-6 indicates the penultimate 6 record

$!-6

(3) search history command

Sometimes, if you need to repeat the last command at the beginning of a string, you can also pass! To operate, then press Enter to execute

For example, if you have just executed a long command that only records that the command begins with curl, you can quickly execute the command through! curl

$! curl

This usage is efficient, but it is unsafe, because it is possible to execute commands that are not what you want to execute, which is a bad thing. It can be safely executed by: P.

$! curl:p curl www.sina.com.cn

After adding: P, only the search command is printed out. If you want to execute it, press the Up key and enter.

If you only know that a command contains x information, and it doesn't start with x, it can also be passed? To execute a command that contains a string

(4) Interactive search history command

In the Linux search history command, you can also search in an interactive way, simply efficient and direct. After entering Ctrl+R on the command line, enter the interactive interface and type the keywords you need to search. If you match multiple commands, you can type Ctrl+R multiple times to switch the last matching command.

(reverse-i-search) `sina': echo sina

As you can see, after I type sina, I automatically match to the last command that matches sina, and press enter to execute the command.

(5) repeated execution of the above order

Here is a summary of the various ways to repeat the previous command. You can choose one you like.

!!-1 Ctrl+p Up Ctrl+R

(6) display time stamp

Sometimes you need to audit the Linux system, which adds a timestamp to the history and is very useful for display.

$export HISTTIMEFORMAT='%F% T'$history 3 46 2021-04-18 15:21:33 curl baidu.com 47 2021-04-18 15:21:35 pwd 48 2021-04-18 15:21:39 history 3

As you can see, the history has shown a timestamp. In fact, these are not enough for audit requirements, you can add more detailed information:

$export HISTTIMEFORMAT= "% F% T `who-u ami 2 > / dev/null | awk'{print $NF}'| sed\-e's / [()] / / g'``whoami`" 6 2021-04-18 16:07:48 113.200.44.237 root ls 7 2021-04-18 16:07:59 113.200.44.237 root pwd 8 2021-04-18 16:08:14 113.200.44.237 root history

(7) controlling the total number of historical records

By default, the Linux system stores up to 1000 history records, which can be viewed through the HISTSIZE environment variable

$echo $HISTSIZE 1000

For scenarios that need to be audited, 1000 history records may be too few, and we can modify them to appropriate values.

$export HISTSIZE=10000

Note that the HISTSIZE variable can only control the number of history records in the buffer. If you need to control the maximum number of records stored in the .bash _ history file, you can control it through HISTFILESIZE.

The above command line changes only take effect in the current Shell environment. If you need to take effect permanently, you need to write to the configuration file.

$echo "export HISTSIZE=10000" > > ~ / .bash_profile $echo "export HISTFILESIZE=200000" > > ~ / .bash_profile $source ~ / .bash_profile

(8) change the history file name

Sometimes, to facilitate management and backup, you need to change the path and name of the history file. Simple, you can also change its file name through the environment variable HISTFILE

$echo "export HISTFILE=/data/backup/chopin.bash_history" > > ~ / .bash_profile $souce ~ / .bash_profile

(IX) banning historical records

In some special environment, we need to disable history.

$echo "export HISTSIZE=0" > > ~ / .bash_profile $echo "export HISTFILESIZE=0" > > ~ / .bash_profile $source ~ / .bash_profile

Haha, the function of disabling history is achieved by directly setting the value of the above two variables to 0.

(10) A little skill that hackers must know

Finally, share an unknown, hacker must know a little trick.

Isn't it cool to add an extra space before the command, such a command will not be recorded in history?

If this technique doesn't work on your system, please check whether the environment variable HISTCONTROL contains ignorespace. It seems that the centos system does not set this value by default.

3 summing up time

In the Linux system, the history command can be very convenient to help us manage historical commands. Usually, our commands are recorded in the cache first and then in the file when Shell exits.

History command provides a very convenient management function, reasonable configuration and management of history, can make your Linux system more robust and secure.

All right, old rule, sweet brother Xiao, let's sum up the common methods of history command.

History n: show only the most recent n history records

History-c: clear the history in the cache

History-w: save the cache history to a file

History-d N: delete article N history

There are several ways to execute commands repeatedly:!,!-1,! n,! string and other interactive history commands. Use Ctrl+R shortcuts to properly use several relevant environment variables to make your Linux system more secure:

HISTSIZE: controls the maximum number of buffer history records

HISTFILESIZE: controls the maximum number of history files

HISTIGNORE: sets which commands are not recorded in history

HISTTIMEFORMAT: format the time displayed by the history command

HISTCONTROL: extended control option

In a production environment, these environment variables need to be persisted to the configuration file ~ / .bash_profile

Export HISTCONTROL=ignoreboth # ignorespace: ignore commands starting with spaces # ignoredups: ignore successive repeating commands # ignoreboth: both of the above parameters are set # set append rather than override shopt-s histappend export HISTSIZE=1000 export HISTFILESIZE=200000 export HISTTIMEFORMAT= "% F% T" export HISTIGNORE= "ls:history" after reading the above, do you have any further understanding of Linux's practical techniques of using history to reduce repetitive commands? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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