In Linux, to perform any task, we need to execute the appropriate commands on the terminal. In case you need more efficiency, you can run multiple commands simultaneously in Linux. This post will quickly teach you how we can run multiple commands in Linux depending on different situations or conditions.
How do I run multiple commands in Linux at the same time?
There are several operators available to run multiple commands in Linux at the same time, such as the semicolon or;, the logical AND operator, and the logical OR operator.
Use the semicolon operator “;”
Semicolon or “;” operator allows you to run multiple commands at the same time in Linux, when the command is separated by “;”.
Basic syntax for the semicolon operator:
[[email protected] ~]# ls;date;df anaconda-ks.cfg epel-release-6-8.noarch.rpm htop-1.0.2 install.log.syslog mytable.sh Pictures mytest mytest4.sh Desktop figlet-2.2.2-1.el6.rf.x86_64.rpm htop-1.0.2.tar.gz man name.txt Public mytest1.sh mytest5.sh Documents for_loop_test.sh htop-2.0.2-2.fc26.x86_64.rpm Music p rpmbuild mytest2.sh mytest.sh Downloads ftp-0.17-51.1.el6.x86_64.rpm install.log mygrep –p Templates mytest3.sh Videos Sun Jun 06 21:10:51 BDT 2017 Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_destroyer-lv_root 16034576 8763864 6434580 58% / tmpfs 1956784 36 1956748 1% /dev/shm /dev/sda1 495844 37181 433063 8% /boot /dev/mapper/vg_andreyex-lv_andreyex 198337 5647 112870 4% /andreyex /dev/sde1 1245608 3356 1165674 1% /home [[email protected] ~]#
It doesn’t matter if the previous command is successful or not. Even if one command fails, the next command will be executed. This is very useful in shell scripting.
Using the logical operator “&&”
Sometimes we only need to execute the second command if the first command completed successfully, as opposed to the semicolon operator.
Basic syntax of a logical operator:
Command1 && Command2 && .... && CommandN
An example of a logical operator.
[[email protected] tmp]# mkdir /tmp/data && cd /tmp/data
In the above example, we created a folder or directory named “data” under the directory section “/ tmp”. Since we have successfully created the / tmp / data directory, we may be able to change to the “/ tmp / data” directory.
In order to double-check this condition, run the same command again, as shown below:
[[email protected] ~]# mkdir /tmp/data && cd /tmp/data mkdir: cannot create directory `/tmp/data': File exists [[email protected] ~]#
In the above command output, “mkdir / tmp / data” failed because it already exists. The system does not allow the second command to run.
The logical AND operator is recommended over the semicolon operator, as it will check the success or failure status of the previous command. To explain this, let’s look at an example.
- Go to the directory / tmp / data1
- Delete all files in it.
[[email protected] data]# pwd /tmp/data [[email protected] data]# ls -ltr total 0 -rw-r--r-- 1 root root 0 Jun 06 21:16 3.txt -rw-r--r-- 1 root root 0 Jun 06 21:16 2.txt -rw-r--r-- 1 root root 0 Jun 06 21:16 1.txt [[email protected] data]# cd /tmp/data1;rm -rf * -bash: cd: /tmp/data1: No such file or directory [[email protected] data]# ll total 0 [[email protected] data]#
Here, in the above example, executing the command “cd / tmp / data1; rm -rf *” in parallel led to a catastrophe, since the system directory / tmp / data1 does not exist, and therefore instead of deleting files in / tmp / data1 / will delete all of our files in the current working directory that are located in / tmp / data. To avoid this situation use the commands below with the logical operator “cd / tmp / data1 && rm -rf *”.
[[email protected] data]# pwd /tmp/data [[email protected] data]# ls -ltr total 0 -rw-r--r-- 1 root root 0 Jun 06 21:11 3.txt -rw-r--r-- 1 root root 0 Jun 06 21:11 2.txt -rw-r--r-- 1 root root 0 Jun 06 21:11 1.txt [[email protected] data]# cd /tmp/data1 && rm -rf * -bash: cd: /tmp/data1: No such file or directory [[email protected] data]# ls -ltr total 0 -rw-r--r-- 1 root root 0 Jun 06 21:11 3.txt -rw-r--r-- 1 root root 0 Jun 06 21:11 2.txt -rw-r--r-- 1 root root 0 Jun 06 21:11 1.txt [[email protected] data]#
Here the second team failed, as the first team also failed.
Using the logical OR operator “||”
In some other cases, we have to execute the second command only if the first command failed. To handle this we use the logical OR operator.
Basic syntax for logical OR operator:
Command1 || Command2 || .... || CommandN
An example of a logical OR operator.
[ -d /tmp/data1 ] || mkdir /tmp/data1
Here we have only created the directory “/ tmp / data1” in the second command, only if it does not exist, which is checked in the first command “[-d /tmp/ data1]”.
Combining multiple statements to execute multiple commands in Linux
We can also combine multiple statements to run multiple commands in Linux.
cat /etc/shadow > /dev/null && echo "File opened successfully" || echo "Failed to open File"
We have used multiple operators in the above command. In case the file exists, we will receive a message like this “successfully opened the file” otherwise we will receive the error “Could not open the file”. This way we can combine multiple statements at one time to run multiple commands in Linux.