Traditionally, computers were only capable of doing one thing at a time and did not have the capacity to multitask. So, it’s really time-consuming if you’re working on a large project where you need to type numerous commands and then wait for the first one to execute before writing the second, and so on. So there is a way where you can write multiple commands at once, but the computer will always perform one single task from your list of activities at a time and then rapidly switch to the next one when the previous one is completed. This is also known as context shifting, and it happens so quickly that no one would notice, and that will give an impression as the commands are running in parallel. This article will cover some of the key methods on how you can execute multiple commands simultaneously so you won’t need to write each command separately.
Running multiple commands using the logical AND (&&) operator
In this method, you need to write the logical AND (&&) operator between multiple commands that continuously execute those commands. For example, we will create a text file, and then want to add some random text in it, then we will also verify these commands to check if they are working properly or not.
$ command A && command B
For example, we will create a text file, and then want to add some random text in it, then we will also verify these commands to check if they are working properly or not.
$ touch myFile.txt && echo ”Hello Linux”>myFile.txt $ ls && cat myFile.txt
You can see from the above image that we used the touch command to create a file with the name of “myFile.txt” after that we have used the logical AND (&&) operator to write some text inside it by using the echo command. Later you can see that we have also verified by running the list (ls) command that shows the text file is now created and then we have also verified its content by running the cat command in parallel.
Running multiple commands in parallel using the Pipe (|) operator
The next method of executing multiple commands in parallel is by using a pipe (|) operator. Pipes allow you to combine two or more commands at once and perform them sequentially.
$ command A | command B
For example, we have taken an example where we will read the content of a text file first and then also try to find some specific keywords in it by typing.
$cat myFile.txt | grep linux
As you can see that we are reading the content of “myFile.txt” first then we have used the grep command to locate the keyword “linux” in it which is displayed by the red color.
Running multiple commands in parallel using a semicolon (;) operator
Like the previous two methods, you can also execute multiple commands in parallel using a semicolon (;) operator. All you need to do is to write different commands and separate them with a semicolon operator.
$ command A ; command B
For example, we will update and upgrade the system by separating these two commands with a semicolon (;) operator.
Running multiple commands in parallel using bash scripting
You can also run multiple commands but first, you need to create the bash script file by typing
$ nano myScript.sh
This will create the bash script file with the name of myScript.sh and then we will write some commands in it as an example as shown below:
#! /bin/bash touch myFile.txt echo “This is Linux”>myFile.txt ls && cat myFile.txt
After that, we will save and exit this file by pressing “CTRL+O” and “CTRL+X” then we will test our bash script file by executing:
$ bash myScript.sh
It can be seen in the above output that all the commands mentioned in the script are executed.
Running and executing one command at a time on Linux Mint takes a long time and is not advised especially if you have a short time. So what you can do is to write multiple commands on the same line so that you don’t need to wait for their execution. But you can only do that by separating those commands with some specific operators that we have discussed in this article; otherwise, you won’t be able to execute those commands in parallel.
The Best Linux Tutorials Website