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.
- How to run a command from history by the command number?
- How to run previously executed command as the 3rd last command?
- How to run the arguments of the last command for the new command?
- Handling more than 2 valuables using ! operator
- On the basis of Keyword, execute the last command
- Run a command to remove all files except ! Filename
- Checking the directory using the ! operator
- Exit the command using the ! operator
- Testing the directory using the ! operator
- The real power of ! operator
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.
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.
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.
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.
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.
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
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.
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
Then I’m copying the file to the downloads folder by using the “cp” command.
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.
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.
Here we used the ls command but with different switches. Now execute the command to regenerate the older commands.
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
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.
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.
In the above code, if the above condition is “0”. It will print && value otherwise it will go for || operator value.
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.
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.
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.
You can execute the sudo apt code without typing it by just follow the below codes.
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.
This article will provide you essential usages of the ! operator and learn more tutorials on Tecrobust