in Linux

Useful chaining operators in Linux

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Chaining operator is something which helps to run multiple commands at once like we execute a script and task completes automatically. Usually, people use Bash shell command line Chaining Operators (Linux operator) in shell scripting but also we can use these operators on shell prompt.

Ampersand Operator (&)

The function of ‘&‘ is to make the command run in background. Just type the command followed with a white space and ‘&‘.

  • Run one command in the background:

[root@server ~]# yum -y update &

  • Run two command in background:

[root@server ~]# yum -y update & yum -y upgrade &

Semi-colon Operator (;)

The semi-colon operator makes it possible to run, several commands in a single go and the execution of command occurs sequentially.

[root@server ~]# yum -y update; apt-get upgrade; mkdir test; touch index.js

AND Operator (&&)

The AND Operator (&&) would execute the second command only, if the execution of first command succeeds.

For example, I want to visit website manarsystem.com using links command, in terminal but before that I need to check if the host is live or not.

[root@server ~]# ping -c3 www.manarsystem.com && links www.manarsystem.com

OR Operator (||)

The OR Operator (||) allow you to execute second command only if the execution of first command fails, if the first command is executed successfully, with an exit status ‘0‘? Obviously! Second command won’t execute.

[root@server ~]# mkdir test || links manarsystem.com

NOT Operator (!)

The NOT Operator (!) is much like an ‘except‘ statement. This command will execute all except the condition provided. To understand this, create a directory ‘files‘ in your home directory and ‘cd‘ to it.

[root@server ~]# mkdir files
[root@server ~]# cd files

Next, create several types of files in the folder ‘files‘.

[root@server ~]# touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html

See we’ve created all the new files within the folder ‘files‘.

[root@server ~]# ls

a.doc a.html a.pdf a.xml b.doc b.html b.pdf b.xml

Now delete all the files except ‘html‘ file all at once, in a smart way.

[root@server ~]# rm -r !(*.html)

Just to verify, last execution. List all of the available files using ls command.

[root@server ~]# ls

a.html b.html

Cheatsheet:

A; B # Run A and then B, regardless of success of A
A && B # Run B if and only if A succeeded
A || B # Run B if and only if A failed
A & # Run A in background.

Written By:

Blog for everything about hosting, website, server and technical support

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.