Table of Contents
This tip is about the how to Execute Multiple Commands in Linux at Once. So read this free guide, How to Execute Multiple Commands in Linux at Once. If you have query related to same article you may contact us.
How to Execute Multiple Commands in Linux at Once – Guide
If you use Linux on a daily basis, you’ll know that the command line is the dominant tool when working with files, installing and configuring system software, and running them. It becomes even more systematic if you run two or more commands at the same time from the command line and save a lot of time.
Here, we’ll look at the different methods in which we can productively merge and run multiple Linux commands on a single line.
Option one: the semicolon (;) operator
The semicolon (;) operator allows you to run multiple commands in succession, regardless of whether each previous command succeeds. For example, open a Terminal window (Ctrl + Alt + T on Ubuntu and Linux Mint). Then type the following three commands on one line,
separated by semicolons and press Enter. This will give you a list of the current directory (ls), find out which directory you are in (pwd) and display your login name (whoami) at once.
ls; pwd; Who am I
You also don’t need to put spaces between semicolons and commands. You can enter the three commands as ls; pwd; whoami. However, spaces make the combined command more readable, which is especially useful if you are putting a combined command into a shell script.
Option two: the logical AND (&&) operator
If you want the second command to run only if the first command succeeds, separate the commands with the logical AND operator, which is two ampersands (&&). For example, we want to make a directory called MyFolder and then change to that directory – as long as it has been created successfully. So we type the following on the command line and press Enter.
mkdir MyFolder && cd MyFolder
The folder was created successfully, so the cd command was executed and we are now in the new folder.
We recommend using the logical AND operator instead of the semicolon operator most of the time (;). This ensures you don’t do anything disastrous. For example, if you run a command to change to a directory and then forcefully remove everything in that directory recursively (cd / some_directory; rm -Rf *), you can quit up ruining your system if the directory change didn’t happen. Not that we recommend that you run a command to unconditionally remove all files in a directory at once.
Option three: the logical OR operator (||)
Sometimes you might want to run a second command only if the first command is not successful. To do this, we use the logical OR operator or two vertical bars (||). For example, we want to check if the MyFolder directory exists ( [ -d ~/MyFolder ] ) and create it if not (mkdir~/MyFolder). We then type the following command at the prompt and press Enter.
[ -d ~/MyFolder ] || mkdir ~ / MyFolder
Make sure there is a space after the first square bracket and before the second square bracket, or the first command that checks to see if the directory exists will not work.
In our example, the MyFolder directory does not exist, so the second command creates the directory.
Combining multiple operators
You can also combine multiple operators on the command line. For example, we want to first check if there is a file ( [ -f ~/sample.txt ] ) If so, we print a message on the screen saying this (echo “File exists.”). Otherwise, we create the file (tap ~/sample.txt). So we type the following at the command prompt and press Enter.
[ -f ~/sample.txt ] && echo “File exists.” || tap ~/sample.txt
In our example, the file did not exist, so it was created.
Here is a useful summary of each of the operators used to combine commands:
All of these command combination methods can also be used in shell scripts on Linux and Windows 10.
Final note
I hope you like the guide How to Execute Multiple Commands in Linux at Once. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.