in

10 Interesting Usages of “!” Operator in Linux Commands

Usage of ! operator

Hello folks, In this article, we are going to share essential pieces of information about the Exclamatory symbol or “!“. This operator is mainly used as the Logical Navigation operator and it is mainly used to find the history of codes in the command line to run the same command again with modification. Now we can jump into the article and will see the interesting usages of ! operator in Linux command line. The commands used below are completely checked in a bash shell although we may have a chance to miss some essential usages of this operator.

Jump To:

How to run a command from history by the command number:

In the previous post, we already shared the information about viewing the “History” of the commands that used in the command line. You can check the basic tricks and tips of Linux here. You can get the history using the following command.

history

How to use? Copy the above code and paste in terminal

history command

Now run a command from the history by the number of the histories that appearing in the command line. It will provide you with the complete data of the particular history.

History command
History command

The above image show s that the number of the commands that used before in the terminal. Here we choose the line number 41 to know the complete process of that history.

Get History using ! operator
Get History using ! operator

The ! operator just rerun the command by calling its number. By calling the number the ! operator will execute the command in history once again. The number may get vary with your history.

How to run previously executed command as the 3rd last command?

From the above topic, you may know the method of calling history by its number and here run previously by their running sequence being the last run command will be represented as -2, second last as -3, seventh last as -8 and more.

Check this too  How to Install PIP for Python 2, Python 3 on Debian 10?

For this, run the command history in the command line and then check the fifth last command, 7th last command and 9th last command by running the following codes.

!-6

!-8

!-10

How to use? Copy the above code and paste in terminal

Run previously executed command
Run previously executed command

That’s it, friends. With this command, you will able to view the particular history command by calling its the names based on the positions.

How to run the arguments of the last command for the new command?

First of all, am going to install the Chrome browser in Linux here it’s for the example purpose not compulsory. Before that, we need to install the dependencies in the Linux server for running the Chrome in Linux. The command for install all the dependencies: sudo apt-get install libxss1 libappindicator1 libindicator7

Installing dependencies
Installing dependencies

Yes, the above code will install the dependencies of Chrome browser and if you want to re-execute the command without typing and then you need to follow the below command and it will re-execute the above code without typing the full code and with its arguments.

ls -l !$

How to use? Copy the above code and paste in terminal

Run a command without arguments
Run a command without arguments

Here !$ will carry arguments passed in the last command to this new command. The reusability of code has been achieved here.

Handling more than 2 valuables using ! operator:

Handling more than one operator? Yes, it is possible to handle more than one operator. Let us consider that I created a simple text file in the desktop and named it as the file1.txt

touch /home/avi/Desktop/file1.txt

How to use? Copy the above code and paste in terminal

Then I’m copying the file to the downloads folder by using the “cp” command.

cp /home/avi/Desktop/file1.txt /home/avi/downloads

How to use? Copy the above code and paste in terminal

In the above command, we passed more than one arguments and they are “/home/avi/Desktop/file1.txt” & “/home/avi/downloads”. To print both arguments differently, we need to use the echo [arguments] simply.

echo “1st Argument is : !^”

Check this too  Paste Command in Linux: Syntax, Commands & Examples!

echo “2nd Argument is : !cp:2”

How to use? Copy the above code and paste in terminal

The first argument can be printed using the “!^” and a remaining argument can be printed using the “!cp:2” command. Here “cp” is the name of the command and “2” is the number of the argument.

On the basis of Keyword, execute the last command:

We can execute the last command based on the keyword. Just follow the below methods.

ls /home > /dev/null                [Command 1]

ls -lA /usr/bin > /dev/null                       [Command 2]

How to use? Copy the above code and paste in terminal

Here we used the ls command but with different switches. Now execute the command to regenerate the older commands.

! ls -lA                [Command 2]

How to use? Copy the above code and paste in terminal

Reuse of command using keywords
Reuse of command using keywords

Run a command to remove all files except ! Filename

Here the ! operator is referred to as the “Logical Not” operator, which means it will execute the commands one with the conditions are not matched.

By this operator, you can able to remove a file from the directory except for the one the name of which is filename.txt

rm !(filename.txt)

How to use? Copy the above code and paste in terminal

You can use this operator for removing the files from the directory with the reference of the file extension. Here, for example, we use the .jpeg format.

rm !(*.jpeg)

How to use? Copy the above code and paste in terminal

Checking the directory using the ! operator:

! -d” is command we mainly used it for validating the directory and then we need to use the logical operator “&&” for printing the directory exist or not and “||” logical OR operator is used for checking the directory  presence.

[ ! -d /home/avi/Tecrobust ] && printf ‘\nno such /home/avi/Tecrobust directory exist\n’ || printf ‘\n/home/avi/Tecrobust directory exist\n’

How to use? Copy the above code and paste in terminal

In the above code, if the above condition is “0”. It will print && value otherwise it will go for || operator value.

Check this too  URL Encode & Decode on Linux using Command Terminal!

Exit the command using the ! operator:

By following the above command. It will print the value if the condition exists otherwise you can use the below command to exit the command.

[ ! -d /home/avi/Tecrobust ] && exit

How to use? Copy the above code and paste in terminal

Testing the directory & create the new one using the ! operator

From the above command it will check the directory and if the directory did not exist then the following command will create a new directory.

[ ! -d /home/avi/Tecrobust ] && mkdir /home/avi/Tecrobust

How to use? Copy the above code and paste in terminal

The real power of ! operator: Ultimate Usage

The “!!” operator will help you to alter the command which ran before in the terminal. Otherwise, you can use the “Navigation Up Arrow” to run the previously used commands.

Providing permissions to the user:

This !! operator will help you to run the command as the administrator. The below scenario will explain the real usage of the ! operator.

For example, if you run the below code, you will get the error “OOPS.. error !! Don’t worry the last command will not be executed“. This error will usually occur when you run the “sudo apt” codes.

apt-get upgrade && apt-get dist-upgrade

How to use? Copy the above code and paste in terminal

You can execute the sudo apt code without typing it by just follow the below codes.

su -c !!

How to use? Copy the above code and paste in terminal

The above code will print the sudo apt without using it.

That’s it, friends. We are winding the tutorial here. We hope that you liked this tutorial very much and if you have any query, then feel free to comment us.

  • Article Length
  • Image Explanantion
  • Content Clarity
  • Command used
4.8

Summary

This article will provide you essential usages of the ! operator and learn more tutorials on Tecrobust

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Rachel

I'm Rachel, From Arizona. Am playing the "Content Management" Role in Tec Robust. I started working in TR from Jan 2019. Big Marvel Fan.

Contact me:
tecrobust [@] gmail.com
Sub: Message to Rachel

Best Linux Apps

Install Google Chrome on Ubuntu Unity 18.04.1 LTS

Linux Tips & Tricks

5 Basic Linux Tips & Tricks Command Line – Interesting Tricks