Never Ending Security

It starts all here

How To Chain Commands In Linux Cheatsheet

How To Chain Commands in Linux

Chaining of Linux commands allows you to execute multiple commands at the same time. The behaviour of that execution can be controlled with the operators you use in between them. Chaining of commands in Linux is like a short shell scripts which you can execute directly from the terminal.

Ampersand Operator (&)

The function of ‘&‘ is to make the command run in background. Just type the command followed with a white space and ‘&‘. You can execute more than one command in the background, in a single go.

Run one command in the background:$ ping ­c5 &

Run two command in background, simultaneously: apt-get update & apt-get 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. apt-get update ; apt-get upgrade ; mkdir test

The above command combination will first execute update instruction, then upgrade instruction and finally will create a ‘test‘ directory under the current working directory.

AND Operator (&&)

The AND Operator (&&) would execute the second command only, if the execution of first command fails, i.e., the exit status of the first command is 1. This command is very useful in checking the execution status of last command.

Example: Visit website using links command but first check if the host is live or not. ping -c3 && links

OR Operator (||)

The OR Operator (||) is much like an ‘else‘ statement in programming. The above operator allow you to execute second command only if the execution of first command fails, i.e., the exit status of first command is ‘1‘.

Example: Execute ‘apt-get update‘ from non-root account and if the first command fails, then ‘links ‘ command will execute.$ apt-get update || links

In the example above, the user was not allowed to update system, Exit status of first command will be ’1′ and the last command ‘links’ is executed.

What if the first command is executed successfully, with an exit status ‘0‘?  Second command won’t execute.$ mkdir test || links

Here, the user creates a folder ‘test‘ in his home directory, for which user is permitted. The command executed successfully giving an exit status ‘0‘ and the last part of the command is not executed.

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 ‘nowhere‘ in your home directory and ‘cd‘ to it.$ mkdir nowhere$ cd nowhere

Next, create several types of files in the folder ‘nowhere‘.$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html

Confirm…$ 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.$ rm -r !(*.html)

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

a.html  b.html

AND – OR operator (&& – ||)

The above operator is actually a combination of ‘AND‘ and ‘OR‘ Operator. It is much like an ‘if-else‘ statement.

For example, let’s do ping to, if success echo ‘Verified‘ else echo ‘Host Down‘.$ ping -c3 && echo "Verified" || echo "Host Down"
PING ( 56(84) bytes of data. 
64 bytes from ( icmp_req=1 ttl=55 time=216 ms 
64 bytes from ( icmp_req=2 ttl=55 time=224 ms 
64 bytes from ( icmp_req=3 ttl=55 time=226 ms 

--- ping statistics --- 
3 packets transmitted, 3 received, 0% packet loss, time 2001ms 
rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms 

PIPE Operator (|)

This PIPE operator is very useful where the output of first command acts as an input to the second command. For example, pipeline the output of ‘ls -l‘ to ‘less‘ and see the output of the command.$ ls -l | less

Command Combination Operator {}

Combine two or more commands, the second command depends upon the execution of the first command.

For example, check if a file ‘xyz.txt‘ and ‘xyz1.txt‘ is available under my Downloads directory or not, and output corresponding output.$ [ -f /home/cyberpunk/Downloads/xyz.txt ] || echo “The file does not exist”$ [ -f /home/cyberpunk/Downloads/xyz1.txt ] || echo “The file does not exist” 

“The file does not exist”

Precedence Operator ()

The Operator makes it possible to execute command in precedence order.

Command_x1 && Command_x2 || Command_x3 && Command_x4.

In the above pseudo command, what if the Command_x1 fails? Neither of the Command_x2,Command_x3Command_x4 would executed, for this we use Precedence Operator, as:

(Command_x1 && Command_x2) || (Command_x3 && Command_x4)

In the above pseudo command, if Command_x1 fails, Command_x2 also fails but StillCommand_x3 and Command_x4 executes depends upon exit status of Command_x3.

Concatenation Operator (\)

The Concatenation Operator (\) as the name specifies, is used to concatenate large commands over several lines in the shell. For example, The below command will open text file test(1).txt.$ nano test\(1\).txt

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s