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

"!" in Linux What's the use?

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

Share

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

This article mainly introduces "!" in Linux. What is the use? what is introduced in this article is very detailed and has a certain reference value. Interested friends must finish reading it!

Execute the previous order

For example, after executing the previous command, you can execute the previous command again using the following ways:

$whereis bash # execute the command bash: / bin/bash / etc/bash.bashrc / usr/share/man/man1/bash.1.gz$!! # execute the previous command whereis bashbash: / bin/bash / etc/bash.bashrc / usr/share/man/man1/bash.1.gz again

!! Represents the previous order executed. As you can see, when you enter two exclamation marks, it displays the previous command and executes the previous command. Of course, we usually think of using the "UP" key to do this. But if it is based on the previous command expansion,! It would be more convenient to come.

For example, you want to view a file, but forget to enter more:

$/ opt/user/test.txt # forgot to enter more$ more! # isn't that much faster?

Use! Isn't it more convenient?

Execute the command using the arguments of the first or last of the previous command

Use the last argument of the previous command

For example, if you use ls to list the contents of the directory without any parameters, but want to execute it again, take the-al parameter, and do not want to enter long parameters, you can use the following methods:

$ls / proc/1/task/1/net/tcp/proc/1/task/1/net/tc$ ls-al! $ls-al / proc/1/task/1/net/tcp-r--r--r-- 1 root root 0 December 22 17:30 / proc/1/task/1/net/tcp

The! $here represents the last parameter of the previous command.

Use the first argument of the previous command

You only need to use the first parameter of the previous command using! ^, for example:

$ls-al! ^

Remove the last parameter and execute the previous command

If you want to execute the previous command, but do not want to take the last parameter:

$ls-al dir # assumes that dir is a long string $!:-ls-al

In what scenarios might it be used? For example, if the last parameter of your last command is a long string, and you just don't want to use it, and the backspace key is slow to delete, you can use the above method.

Use all the parameters of the previous command

I talked about using the last parameter of the previous command, so how do I use it if it's not the last parameter? It's very simple. Use! * you can. For example, when we typed the wrong find command and wanted to correct it:

$fin-name "test.zip" # here find is typed incorrectly. $find! * find. /-name "test.zip". / workspaces/shell/find/test.zip./workspaces/shell/test.zip

Use the parameters specified by the previous command

Some readers may ask, what if I only want to use one of these parameters? According to! [command name]: [parameter number] rule is fine. For example:

$cp-rf dira dirb/ # copy the dira to dirb$ ls-l! cp:2 # to view the contents of the dira ls-l diratotal 0 RWMI RW RWMI-1 hyb hyb 0 December 22 17:45 testfile

When the parameters of the previous command are very long, and you need to take a parameter in the middle, the effect is more obvious.

Execute commands in history

We all know that you can view previously executed commands through the history command, but how do you execute the commands in history again? We can view it through the "UP" key, but when the history command is very long, it is not very convenient. "!" It came in handy:

$history (more omitted here) 2043 touch. / dira/testfile 2044 cp-rf dira dirb/ 2045 ls-al dira 2046 ls-l dira 2047 ls-al dira 2048 ls-l dira 2049 ls-al dira 2050 ls-l dira 2051 history

We can see that when the history command comes out, you can see the command that has been executed before, and you can see that it is preceded by a number. If we want to execute the previous cp-rf dira dirb/ command, we can actually do it in the following way:

$! 2044 # 2044 is the execution of the n command cp-rf dira dirb/

That is, yes! [historical command value] executes the historical command.

Of course, if we want to execute the penultimate command, there is a way:

The $!-2 # exclamation point is followed by a negative number, which represents the penultimate item.

Execute historical commands according to keywords

! Commands can be executed based on keywords.

Execute the previous command that begins with a keyword

For example, execute the previous find command:

$! find # execute the previous command that begins with find

Execute the previous command containing the keyword

For example, execute the previous command that contains name:

$find. /-name "test". / test./find/test$!? name?find. /-name "test". / test./find/test

Replace the parameters of the previous command

For example:

$find. /-name "old*"-a-name "* .zip"

If we need to change the old in this command to new:

$!: gs/old/new

The function of logical non

This is its most familiar function, such as deleting all files except the end of cfg:

Rm! (* .cfg) # Delete with caution

I will not elaborate on it here.

The above is all the contents of this article "in Linux"! "what's the use"? 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