* command < file Send file as a Input to the command.
* command > file Redirect STDOUT of command to file.
* command >> file Append STDOUT of command to file.
* command 2> file Redirect STDERR of command to file.
* command 2>> file Append STDERR of command to file.
Run the following command as a non-root user
Note what happens when the same command is run but STDOUT is redirected to a file
NOTE!: The STDOUT and STDERR are distinct, Redirecting one does not affect the other.
The following command would redirect STDERR to a file
The above command only show the STDOUT of the command.
If you really do not care about the errors then why you waste a file (find.err)?
There is a special file on your system that is very useful in this sort of situation, /dev/null.
/dev/null is a black hole for data. Anything sent to is simply ignored.
Display only STDOUT
Store STDOUT but ignore STDERR
Guess what happen when run the following command
Redirecting Output to Program
Linux and UNIX provides many small utilities that perform one task very well.
A core design feature of Linux and UNIX is that the output of one command can be fed directly as a input for another command.
Pipes can connect the commands.
NOTE!: STDERR is not forwarded across the pipes.
Examples:
In the above example, All the lower case letters are converted to upper case letters
less - View input one page at a time
mail - Send input via email
lpr - Send input to a printer
Redirecting All Output
Some operators affect both STDOUT and STDERR
&> Redirect All Output (STDOUT and STDERR)
Redirecting I/O Channels to Each Other
Run the following command as a non-root user:
You will find that while STDOUT is display through less, STDERR is not.
This is because a pipe only redirect STDOUT.
If you wanted to send all output to less you would needed to redirect STDERR to STDOUT first.
You can redirect one I/O channel to another using > and the channel’s file descriptor numbers.
For Example:
The above command simply redirect all STDERR to a file called 1 rather than to file descriptor 1 (STDOUT).
To tell your shell that you are referring to a file descriptor, prepend the &
For Example:
Combining Outputs
Suppose you wanted to run two command back to back and send their output through the pipe.
For Example:
The output of these commands, you would find that only the calendar for 2015 was printed, while the calendar for 2014 went to the screen
This can be overcome by running the cal command in a subshell.
Redirecting to Multiple Target
Syntax:
Stores STDOUT of command1 in filename, then pipes to command2
Uses:
Troubleshooting complex pipelines.
Simultaneous viewing and logging of output
Examples:
Redirecting STDIN from a file
Redirect STDIN with <
Examples:
Sending Multiple Lines to STDIN
Redirect Multiple line from keyboard to STDIN with <<WORD
All text untill WORD is sent to STDIN.
Sometimes called heretext
Example:
For Loop - Shell Scripting
Performs actions on each member of a set of values.