Piping and Re-Direction

Piping Commands-pipe command is used to send the output of one command/program/process to another command/program/process for further processing. The Unix/Linux systems allow the standard output of a command to be connected as standard input of another command. We can make it do so by using the pipe character ‘|’.
e.g.
If we run this command 
ls /usr/bin
Then it will give us a long listing of files of /usr/bin directory. 
Now to view /usr/bin directory content properly we need to pipe the output of the above command to a program called “less”, which displays the output for us one screen at a time. So we can modify above written command as given below.
ls /usr/bin | less 



Now to scroll the display one line at a time we can press Enter key. If we want to move forward one page, we can use the Space bar key and move backward one page by pressing key. At the end of the file, we can press the q key to exit from the file.

Redirection (Input-Output Redirection)

By default, mostly all commands give output on the screen or take input from the keyboard. Linux however provides a way to send output to a file rather than sending on a computer screen similarly takes input from a file in place of the keyboard. this feature of sending the output to a file or getting input from a file is called redirection.
If we use a command
ls ./notes 
Then this will display output of a list of files inside notes directory on the screen, but if we want to redirect its output to a file file.txt then we can use special symbol > sign as given below:
ls ./notes > file.txt
After redirecting output we can display file.txt content on screen using cat command
E.g. cat file.txt

Types of redirection
  • Output redirection ( > )
  • Input redirection ( < )
Output Redirection

Using this redirection, we can send the output of Linux command to a file. If the file already exists then it will be overwritten and if does not exist then a new file is created with the given file name. If we do not want a file to be overwritten, then we should use >> operator instead of > operator.
msg.txt

E.g. cat msg.txt > msg2.txt
Above Linux command will send the output of cat msg.txt command to the file msg2.txt 
Now we can check the redirected output using cat msg2.txt command.

Input Redirection

If we want to input from a file instead of the keyboard then we can use this type of redirection.

E.g. cat < msg.txt
here, the contents of the file msg.txt will be used as input for command cat



UNIT-301
Network Operating System