Know about I/O Redirections and Pipe

Linux Tutorial


There are three standard devices in CLI, they are Standard Input, Standard Output and Standard Error.

Default Standard Input device is Keyboard. The screen is the default Standard Output and Standard Error device. Meaning the input is generally given from Keyboard and output and errors are generally displayed on Screen.

Sometimes it is required to pass the input from a file. Similarly, sometimes it is required to store the output and error in the file. In such scenario, IO redirectors are used. The IO redirectors are as follows.


  • < – Input Redirector
  • > – Output Redirector
  • 2> – Error Redirector
  • &> – Error and Output both Redirector
$ cat /etc/issue /etc/resolve.conf > output
$ cat /etc/issue /etc/resolve.conf 2> error
$ cat /etc/issue /etc/resolve.conf > out 2> err
$ cat /etc/issue /etc/resolve.conf &> both
$ mail root@localhost
Subject: Test
This is the body of the mail.
$ mail -s "Test Mail" root@localhost < /etc/passwd


Pipe (|) takes the output of the command on the LHS and pass it as input to the command on the RHS.

$ head /etc/services | tail -n 3
$ cat -n /etc/services | head -n 70 | tail -n 10
$ dmidecode | less
$ cat -n /etc/services | head -n 70 | tail -n 10 | tee output.txt

There is one more command called tee command, which saves the output of previous command in a file and also prints it on screen.