Unix Redirection and Pipe Operation
Unix has a reference to represent these 3 streams.
Input redirection can be used to provide input to commands from a file. Input redirection can be achieved through the < or 0< operators.
command [opts] [args] 0< input-file-name
In the above example, the
grep command is being provided with an input from a file. Using
grep we search for lines with “line” keyword present. The command output’s the lines with the given keyword present inside the input file.
This command can also be used with or without 0 (zero), either way the results will be the same.
The file name can be specified along with the file location in the command.
Redirecting output from commands can be done using the > or 1> operators. Both of them work the same way, it will redirect the output from the command and write it into a file specified.
command [opts] [args] 1> output-file-name
The above example demonstrates the use of the output redirection by writing the output of the
ls command into a file.
When writing the output to files, if the file already exist then the contents will be replaced.
When a file already exits, you can use the >> operator to append the output.
To redirect the output to a file, use the output redirection reference 2>.
command [opts] [args] 2> output-file-name
The above example illustrates the usage of error redirection by using the ls command against a directory and file that does not exist.
Pipe is one of the most useful operations available. With it we can provide the output of one command as an input to another command.
command [opts] [args] | command [opts] [args]
Here we can see how the pipe is used to redirect the output from the
ls command and provide it as an input to the
grep command to filter out the list provided.
Using these commands will make lots of your work easier. Try to make the best use of it.