How to Run Multiple Commands in Linux at Once
The Linux terminal is a powerful tool that allows you to perform various system operations using commands. File manipulation, program management, and service automation are some of the operations you can carry out efficiently using shell commands.
However, when it comes to executing multiple operations, running commands one by one isn’t efficient. A faster way to do it is to chain multiple commands in one line. Not only does this speed up the process, but it also saves you time.
Let’s explore all the ways to run multiple commands at once in Linux.
Running Multiple Linux Commands at Once
Linux employs three operators to help you execute multiple commands in one line:
- The Semicolon (;) operator
- The Logical OR (||) operator
- The Logical AND (&&) operator
All of these operators can run two or more shell commands at once. However, knowing which operator to use and when can help you craft commands more effectively. The following sections discuss the purpose and the syntax for using these operators properly:
1. Using the Semicolon (;) Operator
Segmenting a chain of commands with the semicolon is the most common practice for running multiple commands in a Linux terminal. Part of the reason for this is how the operator performs: it runs all the commands in the sequence irrespective of whether or not the previous command ran successfully.
For instance, if there are two commands: command A and command B, using the semicolon operator in between them ensures both commands get executed sequentially regardless of the output of the first command.
command A ; command B
So if you’re in a situation where there’s a need to run two or more unrelated terminal commands at once such that the output status of the first command doesn’t affect the execution of the latter, the semicolon operator is the way to go.
Example use case: If you want to view the name of the current user and the system hostname, you can do so using:
whoami ; hostname
Bear in mind that the shell executes these commands in the exact order you mention them. Here’s what the output would look like:
2. Using the OR (||) Operator
The very definition of the word “or” is a giveaway here: when you run two commands using the OR operator, you tell the shell to execute only one command out of the two.
Consider a scenario where you’ve used the OR operator with two commands: command A and command B. This is how the conjoined command would look like with the OR operator:
command A || command B
Here, command B will execute only if command A fails, i.e. when command A returns an error. Likewise, if command A runs successfully, command B won’t execute.
Talking about its use case, you can use the OR operator when you need to run two related commands together such that the shell executes the next command only when the previous one fails.
Example use case: Let’s assume you want to create a new file, say Document.txt, but before you do that, you want to make sure the file doesn’t already exist in the current directory. In such a situation, you can run your commands in the following sequence:
find . -name Document.txt || touch Document.txt
Here, the find command will look up the present working directory for the Documents.txt file. If it finds the file, the command progression will stop—and the second command won’t run.
On the other hand, if it doesn’t find the file, the command to the right will execute, and a new file with the name Document.txt will be created in your present working directory.
3. Using the AND (&&) Operator
As you’d have probably guessed, the AND operator lets you run multiple commands in sequence, i.e., it executes the next command in a sequence only when its previous command runs successfully.
To understand this better, consider a scenario where you wish to run two related commands such that you want the second command to run only if the first one returns a valid output. In this case, you can bind the commands together using the AND operator, referred to as &&, to get your desired result.
Example use case: One of the most common use cases of the AND operator in Linux is to create a new directory and get into it immediately. That way, you won’t have to run the two commands individually to carry out the operation.
For this guide, let’s assume you want to create a new directory called Documents and immediately change your present working directory to it. This is how you can do it:
mkdir Documents && cd Documents
Here, the mkdir command will create a new directory named Documents in your present working directory. If it succeeds, it’ll allow the cd command to execute.
Combining Multiple Operators to Meet Your Execution Criteria
Besides using operators individually in your commands, you can also group multiple operators to fulfill your execution criteria. This comes in handy when you want to execute commands that satisfy multiple conditions.
For instance, consider a scenario where you want to execute two commands (command B and command C) only when command A fails. To do this, you’ll need to use operators as shown in the notation below:
command A || command B && command C
Example use case: Let’s say you want to determine whether a folder (named Document) exists in your current working directory and create it if it isn’t there.
In this case, instead of running separate commands to find the directory and create a new one, you can use the OR and AND operators together to perform the entire operation efficiently.
Here’s what it would look like:
find . -name Document || echo "Directory not found" && mkdir Document
In this command, find asks the shell to search for a folder named Document in the current working directory. If the directory isn’t present, the terminal transfers the flow to the echo and mkdir commands, which print the specified string and create a new folder, respectively.
Running Multiple Commands at Once Using Shell Scripts
A shell script is a program that lets you execute a series of commands at once automatically. It eliminates the need to type multiple commands into the Linux terminal and saves time and effort.
Simply create a script with all the commands you need to run frequently and turn it into an executable file. Then, run it whenever you need to execute those commands, and it’ll do everything for you.
Start by creating a new file and type in the commands you want to execute at once. Save the file with an appropriate name and add a “.sh” extension at the end.
Now, open the terminal and navigate to the folder where you’ve saved the script. Run the following command to make the file executable:
chmod +x file_name
Once it’s done, the next time you need to execute those commands again, go to the folder that contains the script file in the terminal and execute it like this:
One application where you can put this to use is system updates. Rather than manually typing and executing the sudo apt update and sudo apt upgrade commands into the terminal every time you want to update your system, you can create a script that automatically runs the commands for you.
Simply create a script file with the following command and follow the rest of the steps demonstrated above:
sudo apt update && sudo apt upgrade -y
Efficiently Running Terminal Commands in Linux
As you just saw, using operators in your commands can simplify most command-line operations.
If you prefer performing different system operations on your computer through the terminal, knowing how to use these operators can be very helpful and assist you in running Linux commands efficiently.
Similarly, if you’re just getting started—or are less familiar—with Linux, learning different terminal commands is another step towards mastering the command-line interface.