Friday, 31 January 2014

10 Operators In Linux To Learn And Remember

Knowing the operators and using them at the right time can reduce your work by a lot. 

1. Ampersand (&): This command is used in order to make a command run in the background. All you need to do is type in your command followed by a white space and then the operator. This leaves the command to operate in the background. You can also use this to run multiple commands in the background.

For one command,

xyz@localhost:~$ ping ­c5 www.xyz.com &

Two commands, 

root@localhost:/home/xyz# apt-get update & apt-get upgrade &

2. semi-colon (;): This command is used in order to run several commands in one go. The commands are executed sequentially.

root@localhost:/home/xyz# apt-get update ; apt-get upgrade ; mkdir test

This command will start with the update part, then execute the upgrade command and then the mkdir command.

3. AND (&&): This is the logical and operator. This executes the second command if and only if the first command has been executed. It is often used to check the exit status of the last command. 

root@localhost:/home/xyz# ping -c3 www.xyz.com && links www.xyz.com

In this case you are trying to visit xyz.com if using the links command. But you want to check if the host is available first.

4. OR (||): The logical OR operator executes the second command if the execution of the first command has failed. 

xyz@localhost:~$ apt-get update || links xyz.com

In this, the ‘links xyz.com’ command will be executed since the user is not allowed to update the system. 

5. NOT (!): This is the logical NOT operator that executes all statements except the one exception that you have provided. 

6. AND – OR (&& – ||): This operator acts like an if-else statement. So, if you want to ping xyz.com and echo ‘Success’ if successful and ‘Fail’ otherwise.

abc@localhost:~/abc$ ping -c3 www.xyz.com && echo "Success" || echo "Fail"

7. PIPE (|): This operator is used when the output of the first command is to be the input for the second command. 

xyz@localhost:~$ ls -l | less

We are pipelining the output of the ls-l command to less.

8. Command Combination {}: When you want to put in a combination of multiple commands. 

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.

xyz@localhost:~$ [ -f /home/xyz/Downloads/abc.txt ] || echo “The file not available”

xyz@localhost:~$ [ -f /home/xyz/Downloads/abc1.txt ] || echo “The file not available” 

Here you are checking if the files abc and abc1 are available in the Downloads directory.

9. Precedence (): As the name suggests, use this if you want to execute a command in the order of precedence.

(comm_x1 &&comm_x2) || (comm_x3 && comm_x4)

In this the second part in parantheses will be executed if the first one fails.

10. Concatenation (\): This operator is used to concatenate large commands over a number of lines in the shell.

xyz@localhost:~/Downloads$ nano test\(1\).txt 


Thanks to efytimes

No comments:

Post a Comment