- 10 Linux Command-Line Operators and What They Do
- 1. The Ampersand Operator (&)
- 2. The Semicolon Operator (;)
- 3. The OR Operator (||)
- 4. The Pipe Operator (|)
- 5. The AND Operator (&&)
- 6. The NOT Operator (!)
- 7. The Precedence Operator ((..))
- What does ./ mean in Linux?
- Meaning of ./ in Linux
- Where ./ is used in Linux?
10 Linux Command-Line Operators and What They Do
Similar to programming languages, Linux shells also employ certain operators. Here are some of the most-used command-chaining operators on Linux.
Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.
Command-chaining operators are special characters used to write miniature shell scripts in the command line. They are generally used to execute commands in a certain sequence, defined by the placement of operators between the commands. This is incredibly useful in automating tasks.
Let’s learn about a few common-chaining operators on Linux that can help you complete tasks faster and with lesser user intervals.
1. The Ampersand Operator (&)
Often when you open a program or execute a command from the shell, you have to either wait till the command terminates or manually exit out of the program before you can continue using the shell. Here’s where the ampersand operator (&) comes into play.
By appending the ampersand operator to any command, you dictate the shell to execute that Linux command in the background so that you can continue using the shell untethered.
Usually, if you run gedit from the terminal, you will be unable to use the terminal unless you close the text editor. But, by appending the ampersand operator, you can make it run in the background and continue to use the shell immediately.
2. The Semicolon Operator (;)
The semicolon operator is an incredibly useful Linux chaining operator that you can use to execute commands in a defined, sequential order. Order your commands and separate them by semicolons.
pwd ; mkdir test ; cd test ; touch file
The above syntax dictates the shell to execute each command one after the other. Note that the shell does not check if each command terminates successfully. As soon as the shell receives a return code, it moves on to executing the next command.
3. The OR Operator (||)
The OR operator will execute the command that follows only if the preceding command fails, i.e., returns an exit code of 0. It functions like a logical OR gate, which returns a value of 1 when the input is 0.
In this example syntax, bad_command is a false command that will fail to execute and since it fails, the command succeeding the OR operator, which is the ls command, will execute successfully.
4. The Pipe Operator (|)
The pipe operator directs the output of the preceding command as input to the succeeding command. It is most commonly used to filter data with the grep command.
cat test | grep -i "makeuseof"
This command sends the output of the cat command as input to the grep command, which then filters the output against a specified string.
5. The AND Operator (&&)
This operator functions in similar ways to the semicolon operator except, unlike the semicolon operator, AND operator will execute commands only if the preceding command was successfully executed.
pwd && mkdir test && cd test && bad_command && ls
In this example syntax, the shell will successfully execute all the commands up until bad_command. However, since bad_command fails to run, the shell will return an error and skip the ls command.
6. The NOT Operator (!)
The NOT operator works in similar ways to an except statement in programming. For instance, if you want to perform an operation on a large number of files in a directory but want to exclude a few based on some parameter, then you can use the NOT operator by passing the parameter after the NOT character (!).
This sample command will recursively remove all files in a directory except for files that have a «.txt» extension.
7. The Precedence Operator ((..))
The commands following the AND and OR operators depend on the exit code of the preceding command. These operators are binary and only evaluate the two commands that come before and after them.
So when working with multiple operators, it’s important to set groups and precedence to ensure that the execution sequence meets your expectations.
(ls *.txt > txt-files.list && cp *.tx ~) && (ls *.deb > deb-packages.list && cp *.deb ~) || echo "Precedence Test!"
In this sample syntax, both groups of commands must return an exit code 0 to ensure the successful execution of the last command. This example requires that both commands in the first set () exit with 0 for the second set () to run.
What does ./ mean in Linux?
The terminal of the Linux system is a command-based user interface. You provide your commands into the terminal, and the shell then executes your command and performs the task your command specified.
Many a time, while running some command, you may have observed the ./ present. However, you couldn’t figure out what does the ./ mean. Here we’ve explained what does the ./ mean in the Linux systems and where it’s used.
Meaning of ./ in Linux
To understand the meaning of ‘./’, let’s break it into ‘.’ and ‘/’.
The ‘.’ means the present working directory where you are right now.
The ‘/’ means that you are looking for a directory and not working for a file.
Therefore ./ means inside your current directory.
The ‘ls’ command gives the list of the files and directories present in your current working directory unless some specific directory name is given to it.
As we can see in the image above, the present working directory is ‘Candid.Technology’. When we do a ‘ls -l’ we can see the contents of the directory ‘Candid.technology’. Now, when you give the command ‘ls -l ./’, the output displays the contents of the Candid.Technology directory.
Thus, the ./ refers to nothing else but the current working directory.
Where ./ is used in Linux?
The ./ is generally used to specify the file path or the directory path in a relative way. Let’s understand the example given below to gain more insights.
As you can see in the image above, the present working directory is Candid.Technology. Now, you want to copy a file ‘Hello.txt’ from the home directory to a directory ‘example’ present inside your current directory without changing your current working directory. Here, you can use the ‘./’ to specify the file path.
The path ‘./example/Copied_Hello.txt’ specifies that there is a directory called example in the current working directory (./), where you need to copy the Hello.txt file as Copied_Hello.txt. Thus, by using ‘./’, you specified the relative path for the destination of the copied file, and your current working directory also remained the same.
Therefore, you can use ‘./’ in Linux to specify your current working directory and use it in relative paths to use some files without changing the current working directory.