Linux Command Line Operators

In Linux following command line operators are used. All these operators are briefed below.

1.    Redirection Operator : >

Normally output of any command is sent to console or say monitor. Using redirection operator, the output of command can be sent to some file or some device as shown below.

# ls -l > test.txt

The above command send output to newly created file test.txt. But if the file is existing then it gets overwritten.

# ls -l > /dev/lp0

This command sends output to printer connected on Parallel Port (/dev/lp0).

2.    Indirection Operator : <

# mysum < data.txt

In above command mysum is a executable program. The program expects input from keyboard. Here we have kept input in the file data.txt. So here while execution the program does not prompts for keyboard input, but it simply reads the data.txt file for input.

3.    Home Operator : ~

$ cd ~

The ~ operator is just a shortcut to home folder path. For example if your home folder is /home/abc then the above command simply takes you to the home folder. It is immaterial where you are right now.

4.    Operator : .

# cp /etc/passwd .

In above command the (.) operator simply indicates present folder. So the file /etc/passwd will be copied to present location.

5.    Operator : ..

# cp /etc/passwd ..

The (..) Operator indicates a previous location i.e. one folder below from present. So in above example the file /etc/passwd  will be copied to one folder below your present location.

6.    Pipe Operator : |

# cat abc.txt | wc

The (|) pipe operator can be used to send output of one command to other command as input. In above example the output of cat command is sent to wc command. Just check the below lines.

[root@station1 ~]# cat abc.txt | wc
2 12 58
[root@station1 ~]#

7.    Wildcard Operator : *

# ls -l /etc/p*

The (*) wildcard operator can be used with a meaning as all. In above example it simply lists all files from /etc folder which begins with p as starting letter.

8.    Filter Operator : ?

# ls -l a?b*

The (?) operator just filters the character. In above example the ls commands lists all files which begins with character a as first character and the second character can be anything. But the third character should be b and next all characters can be anything as we have used * there.

9.    Append Operator : >>

# ls -l >> a.txt

The (>>) append operator is very safe redirection operator as it checks the existence of file a.txt first.  If the file is not there then it creates new file else if file is there then it simply appends the file a.txt.

10.    The  Operator : <<

# cat > abc.txt << SS
> This is Line 1.
> This is Line 2.
> SS

# cat abc.txt
This is Line 1.
This is Line 2.

Just check the first command ( cat > abc.txt << SS ). Here we are creating a new file abc.txt without any text editor. The output of cat command is sent to abc.txt till we enter text SS in new line.