Thursday, February 9, 2012

 Redirecting operators in Linux/ File Descriptor 


 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. There are numbers assigned to these file descriptors as shown below.
TYPE        Symbol
STDIN       0< 
STDOUT      1>
STDERR      2>


 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: The abbreviation is StandarD OUTput. By using this we can see the output from a computer. 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: 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. 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 will not meet our requirement. For example sometimes we want to move an error which is popping on a screen to a file for future reference. So at that time we can use these redirecting operators to change the default way of communication between users and computers.
 In Linux there are many redirecting operators (File descriptors) as shown below
 Basic redirecting operators
 > --Output redirecting operator 
< --Input redirecting operator 
>> --Output appending operator. 
<< --Input appending operator (no significant practical use) 
| --Pipe operator, redirects output of a command to next command.
 Advanced redirecting operators

1> --Standard Output redirect operator (same as >) 
1>> --Standard output append redirect operator (same as >>) 
2> --Standard Error redirector 
2>> --Standard error redirector 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 input files to other command 
- -- (hyphen or Dash) redirection from standard input/output
 We will see above operators with some examples on how to use them. We are giving much details on << operator as it is rarely used and 1>, 1>> as these operators are equal to >, >>.
 Example1: Redirect output of fdisk command to a file for future reference.
 fdisk -l > file1.txt
 Example2: Search if Mitch username is present in /etc/passwd file or not
 grep mitch < /etc/passwd
 Example3: Use output redirect append operators to redirect fdisk command output once again so that I can see both the previous output and present one in single file.
 fdisk -l >> file1.txt
Note: Not discussing input append redirect operator as its 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(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 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 -l *.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 previous command output as file names and feed them to next command to args as shown below?

ls -l *.sh | xargs grep bash
 - (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 character 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..

Cat /temp/loc/file1.txt | grep xyz > /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 –

No comments:

Post a Comment