Amazing exclamation point (!) under the Linux command line

Source: Internet
Author: User

‘!‘Symbols can not only be used as negative symbols in Linux, but can also be used to remove commands from a History command record or to run a command without modification. All of the following commands have been tested exactly in the bash shell. Although I haven't tried it, most of them can't be run in another shell. Here we introduce the ‘!‘ amazing and marvelous usage of symbols in the Linux command line.

1. Use numbers to find a command from the history command list to execute

You may not realize that you can run from a list of historical commands (the set of commands that you have already executed). First, use the "history" command to find the ordinal of the previous command.

    1. $ history

Use the history command to find the last command executed

Now, you can run this command only by using the numbers that appear in front of the command in the output of the history command. For example, run a history command that is numbered 1551 in the output.

    1. $ !1551

Use the command ID to execute the last Run command

This way, the command numbered 1551 (the above example is the top command) runs. This is useful in the way that the previous commands are executed by ID numbers, especially if the commands are long. You only need to use ![ The serial number of the history command output] to invoke it.

2. Second to last, seventh command, etc. before running

You can run the previously executed command in a different way, by using 1 for the last command, 2 for the penultimate command, 7 for the bottom seventh command, and so on.

First use the history command to get a list of executed commands. the execution of the history command is necessary because you can use it to ensure that there are no rm command > file or other commands that can cause danger. Next, the sixth, eighth, tenth commands are executed.

    1. $ history
    2. $ !-6
    3. $ !-8
    4. $ !-10

Run a command before executing with a negative number

3. Pass the parameters of the last executed command to facilitate the running of the new command

I need to display /home/$USER/Binary/firefox the contents of the folder, so I do:

    1. $ ls /home/$USER/Binary/firefox

Next, I realized that I should execute ' ls-l ' to see which file is an executable file. So should I re-enter the entire command? No, I don't need to. I just need to take the last parameter in the new command, like this:

    1. $ ls -l !$

!$this will pass the parameters of the last executed command to this new command.

Pass the parameters of the previous command to the new command

4. How to use! To process two or more parameters

For example, I created a text file file1.txt on my desktop.

    1. $ touch /home/avi/Desktop/1.txt

Then use the absolute path in the CP command to copy it to /home/avi/Downloads .

    1. $ cp /home/avi/Desktop/1.txt /home/avi/downloads

Here, we pass two parameters to the CP command. The first one is /home/avi/Desktop/1.txt , the second one is /home/avi/Downloads . Let's deal with them separately, using echo [参数] to print two different parameters.

    1. $ echo "1st Argument is : !^"
    2. $ echo "2nd Argument is : !cp:2"

Note The first parameter can be used "!^" for printing, and the remaining commands can be "![命令名]:[参数编号]" printed.

In the example above, the first command is cp that the second parameter also needs to be printed. So "!cp:2" , if any of the commands such as XYZ run has 5 parameters, and you need to get the fourth parameter, you can use "!xyz:4" . All parameters can be obtained by means of the "!*" .

Handle two or more parameters

5. Execute the last command on a keyword basis

We can execute the last executed command on the basis of the keyword. This can be understood in the following example:

  1. $ ls /home > /dev/null [命令1]
  2. $ ls -l /home/avi/Desktop > /dev/null [命令2]
  3. $ ls -la /home/avi/Downloads > /dev/null [命令3]
  4. $ ls -lA /usr/bin > /dev/null [命令4]

Above we used the same command (LS), but there are different switches and different operating folders. And, we also pass the output to /dev/null , we do not show the output, so the terminal is still very clean.

Now execute the previous command on the basis of the keyword.

    1. $ ! ls [command 1]
    2. $ ! ls -[command 2]
    3. $ ! ls -la [command 3]
    4. $ ! L s -la [command 4]

Check the output, and you'll be amazed to see that you've ls executed the commands you've already executed using just the keyword.

Execute commands on the basis of a keyword

(LCTT: To clarify, this usage will follow the command name to find the last matching command, does not match the parameters.) So the above four commands are executed ls -lA /usr/bin > /dev/null , and the new parameters are added. )

6.!! The power of the operator

You can use (!!) the Run/modify command that you ran last. It will be accompanied by some modifications/adjustments and calls to the previous command. Let me show you some real-world scenarios.

Yesterday I ran a line of scripts to get my private IP, so I executed:

    1. $ ip addr show | grep inet | grep -v ‘inet6‘| grep -v ‘127.0.0.1‘ | awk ‘{print $2}‘ | cut -f1 -d/

Then I suddenly realized that I needed to redirect the output of the above script to a ip.txt file, so what should I do? Should I re-enter the entire command and redirect to a file? A simple solution is to use the UP cursor key and add ‘> ip.txt‘ to redirect the output to a file.

    1. $ ip addr show | grep inet | grep -v ‘inet6‘| grep -v ‘127.0.0.1‘ | awk ‘{print $2}‘ | cut -f1 -d/ > ip.txt

Here to thank the Savior "up cursor key". Now, consider the following scenario, this time I ran the following line of script.

    1. $ ifconfig | grep "inet addr:" | awk ‘{print $2}‘ | grep -v ‘127.0.0.1‘ | cut -f2 -d:

Once I run this script, the bash prompt returns an error message "bash: ifconfig: command not found" . The reason is not difficult to guess, I ran a command that should be run with root permission.

So, how to solve it? It's too much trouble to log in as root and type the entire command again! and the up navigation key doesn't work. LCTT: When you are logged in as a new user, you cannot use the UP cursor key to find the previous command history of another user. Therefore, we need to call "!!" (remove the quotation marks), which will invoke the last command for that user.

    1. $ su -c !! root

Here su is used to switch to the root user, to -c run a specific command with a user, and the most important part is !! that it will be replaced with the last command that was run. Of course! You need to provide the root password.

!! The power of the operator

I usually use it in the following scenario !! .

When I run apt-get with a normal user, I usually get a hint that I don't have permission to execute.

    1. $ apt-get upgrade && apt-get dist-upgrade

All right, there's a mistake. But don't worry, use the following command to execute successfully ...

    1. $ su -c !!

The same applies to:

    1. $ service apache2 start

Or

    1. $ /etc/init.d/apache2 start

Or

    1. $ systemctl start apache2

Ordinary users are not authorized to perform those tasks, which is equivalent to my running:

    1. $ su -c ‘service apache2 start‘

Or

    1. $ su -c ‘/etc/init.d/apache2 start‘

Or

    1. $ su -c ‘systemctl start apache2‘

(LCTT: Before using !! , be sure to confirm what command you are executing!) In addition, under root, do not develop the habit of using it, because you will always be in an inappropriate directory to execute inappropriate commands! )

7. Run an impact all except! [file_name] File command

!(logical non) can be used to ‘!‘ execute commands on all files/extensions except after the file.

A. Remove all files from the folder, except 2.txt.

    1. $ rm !(2.txt)

B. Remove all file types from the folder, except for PDF types.

    1. $ rm !(*.pdf)
8. Check if a folder (such as/home/avi/tecmint) exists? and print

Here, we use ‘! -d‘ to verify that the folder exists, and when the folder does not exist, it will be printed with the and operator followed by, (&&) and when the folder is present, it will be printed with the OR operator (||) .

Logically, when [ ! -d /home/avi/Tecmint ] the output is 0 o'clock, it executes the contents after the and logical character, otherwise it executes (||) the contents after the or logical character.

    1. $ [ ! -d /home/avi/Tecmint ] && printf ‘\nno such /home/avi/Tecmint directory exist\n‘ || printf ‘\n/home/avi/Tecmint directory exist\n‘
9. Check if a folder exists? Exit the command if it does not exist

This is similar to the situation above, but when the desired folder does not exist, the command exits.

    1. $ [ ! -d /home/avi/Tecmint ] && exit
10. If a folder does not exist in your home folder (say test), create it

This is a common implementation in the scripting language and creates one when the desired folder does not exist.

    1. [ ! -d /home/avi/Tecmint ] && mkdir /home/avi/Tecmint

That's all. If you know or occasionally encounter other ‘!‘ ways to use it, please give us a suggestion in the feedback area. Keep in touch!

via:http://www.tecmint.com/mysterious-uses-of-symbol-or-operator-in-linux-commands/

Amazing exclamation point (!) under the Linux command line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.