How to run multiple commands in Linux at once


The Linux terminal is a powerful tool that allows you to perform a wide variety of operations using commands. These commands allow you to perform various computer tasks, including manipulating files, managing programs, and automating services.


Ideally, when you need to run a bunch of terminal commands, you do it one at a time. However, it turns out that there is a better way to do it, and that involves running multiple commands at once.

Follow us as we demonstrate the different ways to run multiple terminal commands in Linux.


Run multiple Linux commands at once

In Linux, there are three ways to run multiple commands in a terminal:

  1. The Semicolon (;) operator

  2. The Logical OR (||) operator

  3. The Logical AND (&&) operator

All of these methods require an operator. Although any of these operators can run two or more shell commands at a time, knowing which operator to use and when can help you create effective commands. The following sections cover both the purpose and the syntax for using these operators correctly.

Related: What Is The Linux Command Line And How Do You Use It?


1. Using the semicolon operator (;)

Segmenting a string of commands with the semicolon is the most common practice when you want to run multiple commands in a terminal. This is partly due to the behavior of the operator: he executes all the commands in the sequence, whether the previous command was executed successfully or not.

VIDEO OF THE DAY MAKEUSE

For example, if there are two commands: command A and command B, using the semicolon operator between them ensures that the first and second commands are executed sequentially, regardless of the output of the first command.

command A ; command B

So if you find yourself in a situation where it is necessary to run two or more unbound terminal commands so that the exit status of the first command does not affect the execution of the last one, the operator semicolon is the solution.


Example use case: To display the current user name and the system host name:

whoami ; hostname

Keep in mind, however, that the shell executes these commands in the order in which you mention them. Here’s what the output would look like:



run multiple terminal commands using a semicolon

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’re telling the shell to run a single command in between.

Consider a scenario where you used the OR operator with two commands: command A and command B. This is what the joint command with the OR operator would look like:

command A || command B

Here, command B will only run if command A fails, i.e. when command A returns an error. Likewise, if command A works successfully, command B will not run.


Speaking of its use case, you can use the OR operator when you need to run two commands linked together so that the shell executes the next command only when the previous one fails.

Example use case: Suppose you want to create a new file, say Document.txt, but before that you want to make sure that a file with the same name doesn’t already exist in the current directory. In such situations, you can run your commands in the following order:

find . -name Document.txt || touch Document.txt

Here the find command will search the current working directory for the Documents.txt to file. If it finds the file, the command progress will stop and the second command will not execute.

On the other hand, if it does not find a matching file, the command on the right will run and a new file with the name Document.txt will be created in your current working directory.


3. Using the AND operator (&&)

As you have probably guessed, the AND operator executes the next command in a sequence only when its previous command completes successfully.

To better understand this, consider a scenario where you want to run two related commands so that you want the second command to run only if the first returns valid output. In this case, we can tie the commands together using the AND operator, called &&, to get the 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 access it immediately. This way, you won’t have to run the two commands individually to complete the operation.

For the purposes of this guide, let’s say you want to create a new directory called Documents and immediately change your current working directory to this one.


mkdir Documents && cd Documents

Here the mkdir The command will create a new directory named Documents in your current working directory. If successful, it will allow execution of the cd command.

Combination of several operators to meet your execution criteria

In addition to using individual operators in your orders, you can also group multiple operators together to meet your fulfillment criteria. This is useful when you want to run commands based on multiple conditions.

Consider a scenario where you want to run two commands (command B and command C) only when command A failed. To do this, you will need to use operators as shown in the notation below:

command A || command B && command C

Example use case: Suppose you want to determine whether a folder (named Document) exists in your current working directory and create it if it is not. 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 whole operation efficiently.


Here’s what it would look like:

find . -name Document || echo "Directory not found" && mkdir Document

In this command, find ask the shell to search for a named folder Document in the current working directory. If the directory is not present, the terminal transfers the stream to the echo and mkdir commands, which print the specified string and respectively create a new folder.

Efficient execution of terminal commands in Linux

As you have just seen, using operators in your commands can simplify many command line operations.

If you are someone who likes to handle different system operations on their computer through terminal, knowing how to use these operators can be very helpful and will definitely help you to run Linux commands more efficiently.

Likewise, if you are new to or are less familiar with Linux, learning different terminal commands is another step towards mastering the CLI.



most-used-linux-terminal-cmds

Linux command reference cheat sheet

This simple cheat sheet will help you get started with Linux command line terminal in no time.

Read more


About the Author


Comments are closed.