Streams, Redirection, and Pipes
From Initq
Types of Stream
First we need to understand three types of streams, (input, output and error).
- Standard input
- This data comes from the keyboard, terminal or login client. this is called stdin and is depicted by 0.
- Standard output
- This is data that is normally displayed on the screen via stdout which is depicted by 1. GUI programs don't use stdout, unless their output is going to an xterm.
- Standard error
- A second type of output stream is the stderr depicted by 2. Stderr is send to the same device as stdout so can't tell them apart. But you can redirect one to a file and one to the screen.
| Handle | Name | Description |
|---|---|---|
| 0 | stdin | Standard input |
| 1 | stdout | Standard output |
| 2 | stderr | Standard error |
Redirecting Input and Output
| Redirection Operation | Effect |
|---|---|
| > | Create a new file containing standard output. If the specified file exists, it's overwritten. |
| 1> | Create a new file containing standard output. If the specified file exists, it's overwritten. |
| >> | Append standard output to the existing file. If the specified file does not exist, it's created. |
| 1>> | Append standard output to the existing file. If the specified file does not exist, it's created. |
| 2> | Create a new file containing standard error. If the specified file exists, it's overwritten. |
| 2>> | Append standard error to the existing file. If the specified file does not exist, it's created. |
| &> | Create a new file containing both standard error and standard output. If the specified file exists, it's overwritten. |
| < | Send the contents of a specified file to be used as standard input. |
| << | Accept text on the following lines as standard input. |
| <> | Causes the specified file to be used as both input and output. |
| 2>&1 | Send error to standard output. |
Piping
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:
command1 | command2
executes command1, using its output as the input for command2 (commonly called piping, since the "|" character is known as a "pipe").
This is equivalent to using two redirects and a temporary file:
command1 > tempfile command2 < tempfile rm tempfile
A good example for command piping is combining echo with another command to achieve something interactive in a non-interactive shell, e.g.
echo -e "user\npass" | ftp localhost
This runs the ftp client with input user, press return, then pass.
If you had a file with 100 lines and you only need to show lines 25 to 75, you could use pipes to do this.
cat file | head -75 | tail -50
Find all files ending in tilde and delete them
find -name "*~" | xargs rm
find -name "*~" -exec rm {} \;
rm `find ./ -name "*~"`
