X
    Categories: Linux

How to run multiple commands in Linux Simultaneously


In Linux for carrying out any task, we need to execute respective commands on the terminal. In case you need more efficiency you can run multiple commands in Linux at a time. This post will quickly teach you how we can run multiple commands in Linux depending on different situation or conditions.


 

How to run multiple commands in Linux at a time?

There are multiple operators available to run multiple commands in Linux at a time such as “semicolon or ;”, “Logical AND operator” and “logical OR” operator.

  • Using semicolon “;” Operator

The semicolon or “;” operator allows us to run multiple commands in Linux at a time when the command is separated by the “;” Operator.

Basic Syntax for Semicolon Operator:

Command1;Command2;Command3;....;CommandN
[root@rhel1 ~]# ls;date;df
anaconda-ks.cfg  epel-release-6-8.noarch.rpm       htop-1.0.2                    install.log.syslog  mytable.sh  Pictures   test      test4.sh
Desktop          figlet-2.2.2-1.el6.rf.x86_64.rpm  htop-1.0.2.tar.gz             man                 name.txt    Public     test1.sh  test5.sh
Documents        for_loop_test.sh                  htop-2.0.2-2.fc26.x86_64.rpm  Music               p           rpmbuild   test2.sh  test.sh
Downloads        ftp-0.17-51.1.el6.x86_64.rpm      install.log                   mygrep              –p          Templates  test3.sh  Videos
Sun Jan 15 14:21:51 BDT 2017
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_rhel1-lv_root
                      16070076   8763864   6489880  58% /
tmpfs                  1960684        36   1960648   1% /dev/shm
/dev/sda1               495844     37181    433063   8% /boot
/dev/mapper/vg_uxtechno-lv_uxtechno
                        198337      5647    182450   4% /uxtechno
/dev/sde1              1233308      3356   1167304   1% /home
[root@rhel1 ~]#

It does not matter if the previous command succeeded or not. Even in case of failure of one command, next command gets executed successfully. This comes very handy in shell scripting. Get your free copy of book for shell scripting Here.

Using Logical AND Operator “&&”

Sometimes we need to execute second command only if the first command gets completed successfully unlike semicolon operator.

Basic syntax for Logical AND Operator:

Command1 && Command2 && .... && CommandN

Example for Logical AND Operator.

[root@rhel1 tmp]# mkdir /tmp/data && cd /tmp/data

In the above example we have created folder or directory named “data” under “/tmp” directory. Since we have successfully created directory /tmp/data we can able to do change directory to “/tmp/data”.

To recheck this condition we fire same command one more time and the output is as below:

[root@rhel1 ~]# mkdir /tmp/data && cd /tmp/data
mkdir: cannot create directory `/tmp/data': File exists
[root@rhel1 ~]#

In the above output command “mkdir /tmp/data” failed since it already exist. System does not allow us to run second command.

The Logical AND operator is recommended over semicolon operator since it check the condition of success or failure of previous command.  To explain this let’s take an example.

Requirement:

  1. Change directory to /tmp/data1
  2. Remove all files in it.
[root@rhel1 data]# pwd
/tmp/data
[root@rhel1 data]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Jan 15 14:37 3.txt
-rw-r--r-- 1 root root 0 Jan 15 14:37 2.txt
-rw-r--r-- 1 root root 0 Jan 15 14:37 1.txt
[root@rhel1 data]# cd /tmp/data1;rm -rf *
-bash: cd: /tmp/data1: No such file or directory
[root@rhel1 data]# ll
total 0
[root@rhel1 data]#

Here in the above example of executing “cd /tmp/data1;rm -rf *” commands  parallel resulted in disaster since on the system /tmp/data1 directory does not exist and so rather than deleting files under /tmp/data1 system deleted all our files under current working directory which is /tmp/data. To avoid such situation use below command with logical AND operator “cd /tmp/data1 && rm -rf *” .

[root@rhel1 data]# pwd
/tmp/data
[root@rhel1 data]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Jan 15 14:58 3.txt
-rw-r--r-- 1 root root 0 Jan 15 14:58 2.txt
-rw-r--r-- 1 root root 0 Jan 15 14:58 1.txt
[root@rhel1 data]# cd /tmp/data1 && rm -rf *
-bash: cd: /tmp/data1: No such file or directory
[root@rhel1 data]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 Jan 15 14:58 3.txt
-rw-r--r-- 1 root root 0 Jan 15 14:58 2.txt
-rw-r--r-- 1 root root 0 Jan 15 14:58 1.txt
[root@rhel1 data]#

Here second command does not succeeded since first command got failed.

Using Logical OR Operator “||”

In some other case we need to execute second command only if the first command got failed. To handle this case we use Logical OR Operator.

Basic syntax for Logical OR Operator:

Command1 || Command2 || .... || CommandN

Example for Logical OR Operator.

[ -d /tmp/data1 ] || mkdir /tmp/data1

Here we are only creating 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 operator to run multiple commands in Linux

We can also combine multiple operators in order to run multiple commands in Linux.

cat /etc/shadow > /dev/null && echo "File opened successfully" || echo "Failed to open File"

In the above command we have used multiple operators. In case file exists we will get message as “File opened successfully” else we will get message “Failed to open File” error message. This how we can combine multiple operators in one go in order to run multiple commands in Linux.

 

View Comments (0)

Related Post