Chaining Operator in Linux with Examples

Chaining Operator in Linux with Examples

Chaining of Linux commands implies combining several commands and executes them based on the behavior of operator used to combine them. It helps to automate the process efficiently. The following tutorial teaches you the most frequently used command chaining operators.

The Concatenation Operator ()

This operator is used to concatenate large commands over many lines in the shell.

[root@linuxhelp ~]# vi linuxhelp(1).txt
[root@linuxhelp ~]# ls -l linuxhelp(1).txt 
-rw-r--r--. 1 root root 37 Apr  4 02:27 linuxhelp(1).txt

The Ampersand Operator (& )

This operator is used to make the command run in background. It is allowed to run more than one command in the background.

[root@linuxhelp ~]# ping -c4 google.com &  
[1] 5303
[root@linuxhelp ~]# PING google.com (216.58.197.78) 56(84) bytes of data
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=1 ttl=57 time=5.19 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=2 ttl=57 time=51.5 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=3 ttl=57 time=5.40 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=4 ttl=57 time=5.03 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 5.032/16.794/51.553/20.068 ms

[1]+  Done                    ping -c4 google.com

Also execute two in the background.

[root@linuxhelp ~]# ping -c5 google.com &  ls -l & 
[1] 5318
[2] 5319
total 640
-rw-------. 1 root root   1554 Mar 20 21:31 anaconda-ks.cfg
-rw-r--r--. 1 root root  76531 Apr  1 11:27 apache-tomcat8file.txt
-rw-r--r--. 1 root root 214932 Nov 15  2014 dateutils_0.3.1-1.1_amd64.deb
-rw-r--r--. 1 root root 231628 Apr  2 16:38 dateutils-0.3.1-2.1.x86_64.rpm
drwxr-xr-x. 3 root root     73 Apr  3 22:50 Desktop
drwxr-xr-x. 2 root root     35 Apr  1 11:27 Documents
drwxr-xr-x. 3 root root   4096 Apr  3 22:50 Downloads
-rw-r--r--. 1 root root   1605 Mar 20 16:02 initial-setup-ks.cfg
-rw-r--r--. 1 root root     37 Apr  4 02:27 linuxhelp(1).txt
-rwxrwxrwx. 1 root root    349 Mar 20 20:04 mediawiki
drwxr-xr-x. 2 root root     35 Apr  1 11:27 Music
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Pictures
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Public
drwxr-xr-x. 8 root root     83 Mar 24 07:25 rpmbuild
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Templates
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Videos
drwxr-xr-x. 7 root root   4096 Jan 18  2015 vnstat-1.13
-rw-r--r--. 1 root root 101568 Jan 18  2015 vnstat-1.13.tar.gz
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=2 ttl=57 time=29.8 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=3 ttl=57 time=74.5 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=4 ttl=57 time=5.16 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=5 ttl=57 time=6.48 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4011ms
rtt min/avg/max/mdev = 5.169/24.433/74.522/26.701 ms

[1]-  Done                    ping -c5 google.com
[2]+  Done                    ls --color=auto &ndash l

The semi-colon Operator ( )

It runs several commands at once and also enables the execution of command occur one after another.

[root@linuxhelp ~]# ls -l   cd /opt/   mkdir /home/user1/Desktop/test
total 640
-rw-------. 1 root root   1554 Mar 20 21:31 anaconda-ks.cfg
-rw-r--r--. 1 root root  76531 Apr  1 11:27 apache-tomcat8file.txt
-rw-r--r--. 1 root root 214932 Nov 15  2014 dateutils_0.3.1-1.1_amd64.deb
-rw-r--r--. 1 root root 231628 Apr  2 16:38 dateutils-0.3.1-2.1.x86_64.rpm
drwxr-xr-x. 3 root root     73 Apr  3 22:50 Desktop
drwxr-xr-x. 2 root root     35 Apr  1 11:27 Documents
drwxr-xr-x. 3 root root   4096 Apr  3 22:50 Downloads
-rw-r--r--. 1 root root   1605 Mar 20 16:02 initial-setup-ks.cfg
-rw-r--r--. 1 root root     37 Apr  4 02:27 linuxhelp(1).txt
-rwxrwxrwx. 1 root root    349 Mar 20 20:04 mediawiki
drwxr-xr-x. 2 root root     35 Apr  1 11:27 Music
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Pictures
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Public
drwxr-xr-x. 8 root root     83 Mar 24 07:25 rpmbuild
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Templates
drwxr-xr-x. 2 root root      6 Mar 20 16:03 Videos
drwxr-xr-x. 7 root root   4096 Jan 18  2015 vnstat-1.13
-rw-r--r--. 1 root root 101568 Jan 18  2015 vnstat-1.13.tar.gz
[root@linuxhelp opt]# 
[root@linuxhelp opt]# ls -l /home/user1/Desktop/
total 440
-rw-r--r--. 1 root root      0 Mar 25 01:40 ]
-rw-r--r--. 1 root root 214932 Nov 15  2014 dateutils_0.3.1-1.1_amd64.deb
-rw-r--r--. 1 root root 231632 Mar 25 01:42 dateutils-0.3.1-2.1.x86_64.rpm
drwxr-xr-x. 2 root root      6 Apr  4 02:55 test
drwxr-xr-x. 2 root root      6 Apr  2 21:20 tset

The AND Operator (& & )

It executes the second command only if the execution of first command succeeds and is quiet useful in checking the execution status of the final command.

[root@linuxhelp opt]# ls -l & &  ping -c3 google.com
total 189348
-rw-r--r--. 1 root root 152239254 Apr 13  2015 jdk-8u45-linux-x64.rpm
-rw-r--r--. 1 root root  41426623 Apr 13  2015 jre-8u45-linux-x64.rpm
drwxr-xr-x. 3 root root        64 Apr  1 09:30 tomcat
-rw-r--r--. 1 root root    115467 Apr  2 15:00 tutorial1
-rw-r--r--. 1 root root    102921 Apr  2 14:50 tutorialpoint
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=1 ttl=57 time=5.83 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=2 ttl=57 time=6.23 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=3 ttl=57 time=52.7 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 5.835/21.615/52.775/22.034 ms

The OR Operator (||)

It functions like an else statement in programming language, it executes the second command only if the execution of the first command fails.

[user1@linuxhelp ~]$ yum update || ls -l
Loaded plugins: fastestmirror, langpacks
You need to be root to perform this command.
total 4
drwxrwxr-x. 2 user1 user1    6 Apr  2 21:59 bin
drwxr-xr-x. 4 user1 user1 4096 Apr  4 02:55 Desktop
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Documents
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Downloads
drwxrwxr-x. 2 user1 user1   32 Apr  2 21:39 linuxhelp
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Music
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Pictures
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Public
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Templates
-rw-rw-r--. 1 user1 user1    0 Apr  2 22:07 test.txt.
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Videos

If the first command is executed then the second command will not be executed.

[user1@linuxhelp ~]$ ls -l || ping google.com
total 4
drwxrwxr-x. 2 user1 user1    6 Apr  2 21:59 bin
drwxr-xr-x. 4 user1 user1 4096 Apr  4 02:55 Desktop
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Documents
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Downloads
drwxrwxr-x. 2 user1 user1   32 Apr  2 21:39 linuxhelp
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Music
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Pictures
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Public
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Templates
-rw-rw-r--. 1 user1 user1    0 Apr  2 22:07 test.txt.
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Videos

The NOT Operator (!)

The operator behaves like an except statement. It works by executing all except the condition provided.

Create a directory name of linuxhelp in your systems home directory and cd to it.

[user1@linuxhelp ~]$ mkdir linuxhelp
[user1@linuxhelp ~]$ cd linuxhelp/

Now create different types of files in the folder like .txt .html .doc

[user1@linuxhelp linuxhelp]$ touch a.html b.html a.doc b.doc a.txt b.txt

Now we see that we have created new files in the folder, linuxhelp.

[user1@linuxhelp linuxhelp]$ ls -l
total 0
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 a.doc
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 a.html
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 a.txt
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 b.doc
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 b.html
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 b.txt

Now you delete all files except the file called html.

[user1@linuxhelp linuxhelp]$ rm -rf !(*.html)

Now verify by listing all the available files using ls command.

[user1@linuxhelp linuxhelp]$ ls -l
total 0
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 a.html
-rw-rw-r--. 1 user1 user1 0 Apr  4 03:18 b.html

The AND &ndash OR operator (& & &ndash ||)

It is like if else statement and is a combination of ‘ AND‘ and ‘ OR‘ Operator.

[user1@linuxhelp linuxhelp]$ ping -c4 google.com & &  echo " Verified"  || 
echo " HOST DOWN" 
PING google.com (216.58.196.110) 56(84) bytes of data.
64 bytes from maa03s19-in-f110.1e100.net (216.58.196.110): icmp_seq=1 ttl=57 time=5.52 ms
64 bytes from maa03s19-in-f110.1e100.net (216.58.196.110): icmp_seq=2 ttl=57 time=5.94 ms
64 bytes from maa03s19-in-f14.1e100.net (216.58.196.110): icmp_seq=3 ttl=57 time=5.99 ms
64 bytes from maa03s19-in-f110.1e100.net (216.58.196.110): icmp_seq=4 ttl=57 time=13.6 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 5.525/7.780/13.655/3.396 ms
Verified

The PIPE Operator (|)

It is essential when the output of the first command acts as an input to the second one.

[user1@linuxhelp ~]$ ls -l | less
total 4
drwxrwxr-x. 2 user1 user1    6 Apr  2 21:59 bin
drwxr-xr-x. 4 user1 user1 4096 Apr  4 02:55 Desktop
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Documents
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Downloads
drwxrwxr-x. 2 user1 user1   32 Apr  4 03:21 linuxhelp
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Music
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Pictures
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Public
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Templates
-rw-rw-r--. 1 user1 user1    0 Apr  2 22:07 test.txt.
drwxr-xr-x. 2 user1 user1    6 Mar 23 02:01 Videos
(END)

The Command Combination Operator {}

Use this operator to combine two or more commands and the second command depends upon the execution of the first.
For example, the user has check the bin directory if the directory is not exist create a directory and echo the directory the message existing.

[user1@linuxhelp ~]$ [ -d bin ] || { echo Directory does not exist, creating directory now.  mkdir bin  } & &  echo Directory exists.
Directory exists.

The Precedence Operator ()

It is used to execute command in precedence order.

[user1@linuxhelp ~]$ yum update & & ls -l || cd /opt/ & &  ping -c2 google.com 
Loaded plugins: fastestmirror, langpacks
You need to be root to perform this command.
PING google.com (74.125.130.138) 56(84) bytes of data.
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=1 ttl=48 time=50.9 ms
64 bytes from sb-in-f138.1e100.net (74.125.130.138): icmp_seq=2 ttl=48 time=77.3 ms

--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 50.998/64.185/77.372/13.187 ms

In the above line, if the first command fails then neither of the Command_x3, Command_x4 will be executed and for this reason we use Precedence Operator.

FAQ
Q
Whether it possible to use the multiple chaining operators with any Linux command?
A
Yes, its possible to use the multiple chaining operators with any Linux command. You can see the following example for this syntax: "ping -c3 www.linuxhelp.com && echo "Verified" || echo "Host Down"
Q
How to use the Precedence Operator () with any Linux command?
A
Here I have to give an example to use the Precedence Operator () with any Linux command. For Ex: "Command_x1 &&Command_x2 || Command_x3 && Command_x4".
Q
Give me the syntax to use the concatenation operator with any Linux command?
A
You can see the following example to get the idea to use the concatenation operator with any Linux command. For Syntax: "nano test\(1\).txt".
Q
Give me the example to use the ampersand symbol with any Linux command?
A
Here I have give one example to use the ampersand symbol with "ping" command in Linux. For Ex: " ping ­c5 www.linuxhelp.com &"
Q
How to use the semicolon operator with Linux command?
A
You can see the following syntax to get an idea to use the semi-colon with any Linux commands. For Ex: "apt-get update ; apt-get upgrade ; mkdir test"