How to run multiple commands in Linux

29/12/2020
Different types of commands are needed to run from the terminal in Linux. Sometimes we need to run multiple commands at a time where the commands can depend on each other or not. Running multiple commands at once is called command chaining. Many types of operators can be used for command chaining for different purposes. How you can run two or more commands by using most commonly used operators is shown in this tutorial.

Pipe (|) operator

pipe(|) operator is used to run two or more related commands at a time. The input of the next command will be the output of the previous command. So, the success of each command depends on the success of earlier command without first command. In the following command, the first command, ls will find out the list of files and folders of the current location and send the output as input for the second command, wc. It will print the total number of lines, words, and characters based on the input data.

$ ls -l | wc -lwc

Semicolon (;) Operator

Semicolon(;) operator is used to running two or more unrelated commands at a time. This means that the output of each command is not dependent on other commands. In the following example, three types of commands are combined together and the failure of each command will not create an effect on the output of other commands. The first command will print the content of a file, the second command will make a directory and the third command will change the current directory.

$ cat myfile.txt ; mkdir newdir ; cd Desktop

Logical AND (&&) operator

The commands which run by Logical AND (&&) are related with each other like pipe (|) command. So, if the previous command will not execute successfully then the next commands will not work. In the following example, two commands, mkdir, and rmdir combined by && operators. So, it mkdir command is failed to execute successfully then rmdir command will not execute. According to the output of ls command, myDir directory already exists in the current location. So the first command will not execute and for this second command will not execute also.

$ ls
$ mkdir myDir && rmdir temp
$ ls

Logical OR (||) operator

Logical OR (||) operator is the opposite of the Logical AND (&&) operator. The next command will execute if the previous command fails to execute. Three cat commands are combined with OR (||) operator in the following example. When you will run the command, first of all, it will try to display the content of cat.txt file. If no such file exists in the current location then it will try to execute the next command. According to the output, bird.txt file exists in the current location and the content of this file is displayed.

$ cat cat.txt || cat dog.txt || cat bird.txt

Multiple commands with multiple operators

You can use multiple operators to run multiple commands at a time.  In the following example, three commands are combined with OR (||) and AND (&&) operators. After running the command, first of all, it will change the current directory to newdir if the directory exists. If this command fails then it will create the directory by executing the second command and print the message, “directory is created.” According to the output, newdir directory not exist in the current location. So, the error message is displayed and the directory is created later.

$ cd newdir || mkdir newdir && echo "directory is created"

Combination operator {}

Two or more commands can be combined using this operator and if the execution of the first command fails then the second command will not execute. In the following example, OR, AND and combination operators are used together. First commands will check the temp directory is exist in the current location or not. If the first command fails then it will create a temp directory and print a message. The last command will show the current directory list.

$ [ -d temp ] || { mkdir temp; echo temp directory is created now.;  } && ls

Precedence operator ()

You can use this operator for grouping the commands at the time of execution. Here, each group will work as a single task. In the following example, two command groups are defined and if the first group fails to execute then the second group will execute.

$ (cd temp && ls -a) || (mkdir temp && ls)

Conclusion

This tutorial explained the mostly used operators for running multiple commands in Linux. But there are many others operators exist in bash which are used to run two or more commands together. These are  ampersand (&), redirection (<,>,>>), Logical NOT (!), Combination ({}) etc.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

BASH command output to the variable

Different types of bash commands need to be run from the terminal based on the user’s requirements. When the user runs...
29/12/2020

How to check the variable is set or empty in bash

A variable can be defined or undefined. When any variable is not declared or declared but no value is assigned then the...
29/12/2020

Bash Range: How to iterate over sequences generated on the shell

You can iterate the sequence of numbers in bash by two ways. One is by using seq command and another is by specifying range...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021