Redirecting operators in Linux

In order to understand about Redirecting operators in Linux we should know how we communicate with a computer. When we are communicating with a computer there should be a way to do it and this is achieved by STDIN (0), STDOUT (1), STDERR (2) file descriptors. These file descriptors are  assigned with a number as shown below.

STDIN –> 0 STDOUT –> 1 STDERR –> 2   We will see what actually these redirection operators in detail.

STDIN

STDIN: stands for STandarD INput. By using this we can give an input to the computer to do some task. Whatever device we used to give input to a computer will come under STDIN. A STDIN can be A keyboard A mouse A scanner A floppy A CD/DVD ROM A touch screen A barcode reader Or a card reader

 STDOUT

STDOUT: The abbreviation is STandarD OUTput. By using this we can see the output from a computer for the it just processed or executed. Whatever device we used to get the output from a computer will come under this STDOUT. A STDOUT can be A monitor A speaker Or a printer

STDERR 

STDERR: This is abbreviated as STandarD ERRor. By using this, A computer can communicate with user to give him warning/error etc that something went wrong when executing a task. A STDERR can be A monitor A printer A log file A LED indicator Or a speaker   Why we require redirecting operators? We require redirecting operators in some situations where our standard communication with computer will not meet our requirement. For example sometimes we want to store an error which is popping on a screen to a file for future reference. At that time we can use one of these redirecting operators to change the default way of communication between a user and a server. In Linux there are many redirecting operators (File descriptors) are shown below

  • Basic Redirecting Operators
  • Advanced Redirecting Operators
  • File descriptors
  • Hardware files useful for redirection operators.
  • Combining redirection operators

Basic redirecting operators

> –Output redirecting operator < –Input redirecting operator >> –Output appending operator << –Input appending operator (no significant practical use) or HERE documents <<< HERE string | –Pipe operator, redirects output of a command to next command  

Advanced redirecting operators and commands

1> –Standard Output redirect operator (same as >) 1>> –Standard output append redirect operator (same as >>) 2> –Standard Error redirect 2>> –Standard error redirect with append &> –Standard Output and Error tee command –To redirect output to the screen as well as a file xargs command –To feed output of command as files to other command(bit tricky) – — (hyphen or Dash) redirection from standard input/output We will see above operators with some examples on how to use them. We are not giving much details on 1>, 1>> as these operators are almost equal to >, >>.

File descriptors(bit more advanced)

>& <&   Example1: Redirect output of a command to a file for future reference. Example: Redirect output of fdisk -l to a file file1.txt fdisk -l > file1.txt The above command will save fdisk -l command output to file1.txt and you will not see any output when you execute above command. Example: Redirect the content of /etc/password file to xyz.txt file. cat /etc/passwd > xyz.txt   Example2: Send a mail to surendra@linuxnix.com with body of the mail as file content of abc.txt mail surendra@linuxnix.com < abc.txt Example3: Use output redirect append operators to redirect fdisk command output once again so that I can see both the the previous output and present one in single file.

 fdisk -l >> file1.txt

Note: Not discussing input append redirect operator as it’s very rarely used in practice. Example4: How can I count all the files with in present working directory

ls -l | wc -l

Example5: Redirect error of a command to a file and output to the screen.

ls -hrtfd 2> file3.txt

Exampl6: Redirect and append error to a file and output to the screen.  

ls -wertdf 2>> file4.txt

Example7: Redirect both output and error to a file  

ls -ewrdter &> file5.txt

Note: &> and 2>&1 is one and the same.

TEE command

tee command: tee(T) command is a special command which will redirect the output to a file as well as screen at same time, please consider it as capital T when it works. Example8: Redirect fdisk command to a file as well as on the screen at a time. For example if you want to execute fdisk –l command want to see output of the command then execute one more command to redirect output to a file for future reference. We can do that with following commands

 fdisk –l

fdisk –l > fdskout.txt

But with tee command you no need to execute two command. Below is a single command for the above fdisk example.

 fdisk -l | tee file6.txt

XARGS command

xargs command: xargs command is one more special command which will redirect output of a command and feed it as input to other commands. Example9: List all the files which contain .sh file extension and within them list all the files which contains bash in them.  

ls *.sh | grep bash

If I execute above command, this will not give all the files containing bash but will give file name containing bash. For this type of requirements we have to use xargs. Which will consider the previous command output as file names and feed them to next command to args as shown below?  

ls *.sh | xargs grep bash

– (hyphen or dash) Operator

– (hyphen or dash) Operator: This operator will act as both input as well as output redirect operator. I did not find much usage on using this operator as input redirecting operator. There are numerous examples for using this as output redirect operation such as with tar, cut commands etc. I have taken following examples from tldp.org. Example10: Search for a string in a file1.txt file and feed that to diff command to see the difference between abc.txt file and this new temporary file (file2.txt) files. Normally we do that as follows..  

grep xyz /temp/loc/file1.txt > /temp/loc/file2.txt

Now compare file2.txt with abc file.

diff /temp/loc/abc.txt /temp/loc/file2.txt

This can be done in single command using – operator  

grep xyz /temp/loc/file1.txt | diff /temp/loc/abc.txt –

Please show some love by sharing this post on facebook, linkedin etc, if it helped you. Please comment your thoughts on this if you know any other different redirect operators.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.