8 Mysterious Uses of (!) Operator in Linux Commands

Introduction

The (!) operator is a powerful tool in Linux commands that can be used to perform a variety of tasks. It is often used to execute commands, control the flow of a script, and even to create aliases. In this article, we will explore eight mysterious uses of the (!) operator in Linux commands. We will look at how it can be used to create aliases, execute commands, and even control the flow of a script. We will also discuss how it can be used to create shortcuts and to quickly access the last command used. Finally, we will discuss how it can be used to quickly search for files and directories. By the end of this article, you will have a better understanding of the mysterious uses of the (!) operator in Linux commands.

8 Mysterious Uses of (!) Operator in Linux Commands

1. To separate multiple commands on the same line: The exclamation point (!) can be used to separate multiple commands on the same line. For example, the following command will execute both the ls and pwd commands:

$ ls ! pwd

2. To execute the last command: The exclamation point (!) can be used to execute the last command that was entered. For example, if the last command entered was ls, the following command will execute it:

$ !ls

3. To execute a command from the history list: The exclamation point (!) can be used to execute a command from the history list. For example, if the command ls was entered previously, the following command will execute it:

$ !ls

4. To execute a command with a specific argument: The exclamation point (!) can be used to execute a command with a specific argument. For example, if the command ls was entered previously, the following command will execute it with the argument -l:

$ !ls -l

5. To execute a command with a specific pattern: The exclamation point (!) can be used to execute a command with a specific pattern. For example, if the command ls was entered previously, the following command will execute it with the pattern *.txt:

$ !ls *.txt

6. To execute a command from a specific line number: The exclamation point (!) can be used to execute a command from a specific line number. For example, if the command ls was entered previously, the following command will execute it from line number 10:

$ !10:ls

7. To execute a command from a specific user: The exclamation point (!) can be used to execute a command from a specific user. For example, if the command ls was entered previously by the user john, the following command will execute it:

$ !john:ls

8. To execute a command from a specific time: The exclamation point (!) can be used to execute a command from a specific time. For example, if the command ls was entered previously at 12:00 PM, the following command will execute it:

$ !12:00:ls
[ad_1]

The '!' symbol or operator in Linux can be used as a Logical Negation operator as well as to fetch commands from history with tweaks or to run previously executed commands with modification.

Most of the following Linux commands usage with '!' symbol can vary between different shells. While the examples I provided are commonly used in the bash shell, some other Linux shells may have different implementations or may not support certain uses of the ‘!’ symbol at all.

Let’s dive into the amazing and mysterious uses of '!' symbol or operator in Linux commands.

1. Run a Command from History Using Command Numbers

You might not be aware of the fact that you can run a command from your history command (already/earlier executed commands). To get started first, find the command number by running the ‘history‘ command.

$ history
Find Last Executed Commands in Linux
Find the Last Executed Commands in Linux

To run a command from history by its command number, you can use the '!' symbol followed by the command number as shown.

$ !1551
Run a Command by Its Command Number
Run a Command by Its Command Number

When you execute the above command, it will run the top command at line number 1551 from your history.

Please note that the actual command number may vary depending on your command history. You can use the history command to view the list of commands along with their line numbers.

2. Run Previously Executed Commands in Linux

You may run those commands which you have run previously by their running sequence the last run command will be represented as -1, the second last as -2, the seventh last as -7, and so on.

For example, if you want to rerun the command that was executed six, eight or tenth commands ago, you can use the !-n, where n is the number of commands back you want to reference.

$ history
$ !-6
$ !-8
$ !-10
Re-run a Command in Linux
Re-run a Command in Linux

3. Pass Previous Command’s Arguments to New Command

I need to list the content of the directory ‘/home/$USER/Binary/firefox‘ so I fired.

$ ls /home/$USER/Binary/firefox

Then I realized that I should have fired ‘ls -l‘ to see which file is executable there. So should I type the whole command again? No, I don’t need it. I just need to carry the last argument to this new command as:

$ ls -l !$

Here !$ will carry arguments passed in the last command to this new command.

Pass Arguments of Last Executed Command to New Command
Pass Arguments of the Last Executed Command to the New Command

4. How to Handle Two or More Arguments in Commands

Let’s say I created a text file 1.txt on the Desktop.

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

and then copy it to ‘/home/avi/Downloads‘ using the complete path on either side with the cp command.

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

Now we have passed two arguments with the cp command. First is ‘/home/avi/Desktop/1.txt‘ and the second is ‘/home/avi/Downloads‘, let’s handle them differently, just execute echo [arguments] to print both arguments differently.

$ echo “1st Argument is : !^”
$ echo “2nd Argument is : !cp:2”

Note 1st argument can be printed as “!^” and the rest of the arguments can be printed by executing “![Name_of_Command]:[Number_of_argument]”.

In the above example, the first command was ‘cp‘ and 2nd argument was needed to print. Hence “!cp:2”, if any command says xyz is run with 5 arguments and you need to get the 4th argument, you may use “!xyz:4”, and use it as you like. All the arguments can be accessed by “!*”.

Handle Two or More Arguments

5. Run Last Commands Based on Specific Keywords

We can execute the last executed commands on the basis of keywords. We can understand it as follows:

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

Here we have used the ls command but with different switches and for different folders. Moreover, we have sent to the output of each command to ‘/dev/null‘ to keep the console clean.

Now execute the last run commands on the basis of keywords.

$ ! ls			[Command 1]
$ ! ls -l		[Command 2]	
$ ! ls -la		[Command 3]
$ ! ls -lA		[Command 4]

Check the output and you will be astonished that you are running already executed commands just by ls keywords.

Run Commands Based on Keywords

6. Repeat Last Executed Command in Linux

You can run/alter your last executed command using (!!) operator, which is a shorthand notation that allows you to refer to the previous command executed in the command line.

For example, last day I run a one-liner script to find out the IP address of the Linux machine.

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

Then suddenly I figured out that I need to redirect the output of the above script to a file ip.txt, so what should I do? Should I retype the whole command again and redirect the output to a file? Well, an easy solution is to use UP navigation key and add '> ip.txt' to redirect the output to a file.

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

Thanks to the Life Savior UP navigation key here. Now consider the below condition, the next time I run the below one-liner script.

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

As soon as I run the script, the bash prompt returned an error with the message “bash: ifconfig: command not found”, it was not difficult for me to guess I run this command as a user where it should be run as root.

So what’s the solution? it is difficult to log in to root and then type the whole command again! Also (UP Navigation Key) in the last example didn’t come to the rescue here. So? we need to call “!!” without quotes, which will call the last command for that user.

$ su -c “!!” root

Here su is the switch user which is the root, -c is to run the specified command as the user and the most important part !! will be replaced by command and the last run command will be substituted here. Yeah! You need to provide a root password.

Repeat Last Executed Command in Linux
Repeat Last Executed Command in Linux

7. Remove Files Except One File Using ‘!’ Operator

In Linux, the ! operator (known as the “bang” operator) is used for history expansion that allows you to refer to previous commands in your command history and perform various operations with them.

To remove all files from a directory except for a specific file (important_file.txt), you can use the rm command with ! operator as shown.

$ rm !(important_file.txt)

To remove all the file types from the folder except the one the extension of which is ‘.pdf‘.

$ $ rm !(*.pdf)

8. Check If a Directory Exists in Linux

Here we will use '! -d' to validate if the directory exists or is not followed by Logical AND Operator (&&) to print that directory does not exist and Logical OR Operator (||) to print the directory is present.

Logic is, when the output of [ ! -d /home/avi/Tecmint ] is 0, it will execute what lies beyond Logical AND else it will go to Logical OR (||) and execute what lies beyond Logical OR.

$ [ ! -d /home/avi/Tecmint ] && printf '\nno such /home/avi/Tecmint directory exist\n' || printf '\n/home/avi/Tecmint directory exist\n'

Similar to the above condition, but here if the desired directory doesn’t exist it will exit the command.

$ [ ! -d /home/avi/Tecmint ] && exit

A general implementation in scripting language where if the desired directory does not exist, it will create one.

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

That’s all for now. If you know or come across any other use of '!' which is worth knowing, you may like to provide us with your suggestion in the feedback. Keep connected!

[ad_2]

8 Mysterious Uses of (!) Operator in Linux Commands

The exclamation mark (!) operator is a powerful tool in Linux commands. It is used to execute a previously used command, to substitute a string in a command, to negate a condition, and to perform various other operations. Here are 8 mysterious uses of the (!) operator in Linux commands.

1. Executing a Previously Used Command

The (!) operator can be used to execute a previously used command. For example, if you want to execute the command “ls -l” again, you can simply type “!ls”. This will execute the last command that started with “ls”.

2. Substituting a String in a Command

The (!) operator can also be used to substitute a string in a command. For example, if you want to substitute the string “foo” with “bar” in the command “echo foo”, you can type “echo !:s/foo/bar”. This will execute the command “echo bar”.

3. Negating a Condition

The (!) operator can be used to negate a condition. For example, if you want to execute a command only if a certain condition is not met, you can use the (!) operator. For example, if you want to execute a command only if the file “foo.txt” does not exist, you can type “if [ ! -f foo.txt ]; then command; fi”.

4. Executing a Command in the Background

The (!) operator can be used to execute a command in the background. For example, if you want to execute the command “sleep 10” in the background, you can type “!sleep 10 &”. This will execute the command in the background.

5. Executing a Command in a Subshell

The (!) operator can be used to execute a command in a subshell. For example, if you want to execute the command “echo foo” in a subshell, you can type “!(echo foo)”. This will execute the command in a subshell.

6. Executing a Command in a Different Directory

The (!) operator can be used to execute a command in a different directory. For example, if you want to execute the command “ls” in the directory “/tmp”, you can type “!cd /tmp; ls”. This will execute the command in the specified directory.

7. Executing a Command with a Different User

The (!) operator can be used to execute a command with a different user. For example, if you want to execute the command “ls” as the user “foo”, you can type “!sudo -u foo ls”. This will execute the command as the specified user.

8. Executing a Command with a Different Environment

The (!) operator can be used to execute a command with a different environment. For example, if you want to execute the command “echo $PATH” with a different environment, you can type “!env PATH=/usr/local/bin echo $PATH”. This will execute the command with the specified environment.

The (!) operator is a powerful tool in Linux commands. It can be used to execute a previously used command, to substitute a string in a command, to negate a condition, and to perform various other operations. By understanding the mysterious uses of the (!) operator, you can become a more efficient Linux user.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00