Pipes and Redirection


Pipes and Redirection are one the powerful features of Linux Operating System. These are used for sending data stream from input to output or you can say from source to destination.

File Descriptor and Redirection

File descriptor is of three types.

Standard Input (fd 0), Standard Output (fd 1) and Standard Error (fd2)


All three file descriptors to be available for any command to execute. The first, fd 0 (standard input, stdin), is for reading. The other two (fd 1, stdout and fd 2, stderr) are for writing.

2>&1 means temporarily connecting the stderr of the command to the same "resource" as the shell's stdout.

A command reads its input from (stdin), prints normal output to (stdout), and error ouput to (stderr). If one of those three file descriptor is not open then you may encounter problems.




Redirection


Standard Input

Use < operator for standard input redirection, to make file as input file. The file descriptor for Standard Input is 0.

For Example

$ more < longfile.txt

Here we are giving the file longfile.txt as input to command more.


Standard Output

Use > operator for standard output redirection, to make file as output file. The file descriptor for Standard Output is 1.

$ ls −l > lsout.txt

Here we are writing the output that we get from command ls –l to a file lsout.txt. If the file is already present then it overwrite the file with new data. If file is not present then it creates the file and writes the data.

To append to the file, we would use the >> operator

$ ls -al >> lsout.txt

This does not overwrite but append the data in file.


Standard Error

The file descriptor for Standard Error is 2.To redirect to standard error output, preface > operator with the number of the file descriptor we wish to redirect. for standard error file descriptor is 2, so we use the 2> for standard error.

$ kill 1111 2> error.txt

It will put error in error.txt file

$ kill 1111 > out.txt 2> error.txt

It will put the output and error information into separate files.
We can use the >& operator to combine the two output and error outputs in same file.

$ kill −1 1234 > outerror.txt 2>&1


Pipes

We can connect processes together using the pipe ( | ) operator. Here we give the output received from first process to the input of second process.

For displaying the content of a directory in sorted order

$ ls –l | sort

Similarly foe seeing the processes in sorted order page by page.



$ ps | sort | more




No comments: