: Introduction to UNIX
When a UNIX command-line
program (like ls, cat, head, tail
, etc.) is executed, by default
it reads input from the standard input, stdin, which is usually the terminal keyboard.
It writes its output by default to the standard output, stdout,
which is usually the
For example, if we type the command cat by itself, the program (cat ) will try to read its input from
the terminal keyboard, until it reaches the end-of-file (EOF) character,
which is Ctrl-D. As usual, it will copy its input (stdin ) to its output (stdout ). When you specify file(s)
to cat , it reads
from them instead of stdin .
So what do you do when you would like cat to write to a file instead of stdout ?
You can redirect
the command's output from stdout to a file by using > or >>. Instead of the command's output being
sent to the terminal screen, it will be sent to the file named after the
or >>. To use the command cat to type words directly to a file,
you can redirect the output from stdout to the file like this:
Redirecting stderr (Standard Error)
cat > test.txt
Since no filename is
specified to the cat command, it reads input from the terminal
) until it reaches and EOF character (Ctrl-D). The > redirects output from the terminal
) to the file specified, test.txt .
This will create the
file test.txt or overwrite it if it exists. To
append to the end of a file instead of overwriting it, use
2>&1 > /dev/null Will concatenate both stderr and stdout and send both to /dev/null See p. 309 in the book for this.
2>/dev/null Will redirect stderr to /dev/null
input is similar in form to redirecting output. An example:
cat < test.txt
This will redirect
the command's input to read from the file named test.txt instead of reading from stdin .
smauney@sol:~ $ ls
stdin is redirected
from y.txt, you
don't even get the chance to say no. The rm command looks to stdin , sees a "y" and a newline from having
pressed the enter key, and dutifully removes the file. Since stdin is normally the terminal keyboard,
that the response is being interactively typed at the keyboard, although
public projects stuff example_file
smauney@sol:~ $ rm example_file
/bin/rm: remove `example_file'? n
smauney@sol:~ $ cat y.txt
smauney@sol:~ $ rm example_file < y.txt
/bin/rm: remove `example_file'?
smauney@sol:~ $ ls
public projects stuff
(|) connects the
standard output of one command directly to the standard input of another.
smauney@sol:~/public $ ls
ls command lists the files
in the directory to stdout . The pipe (|) redirects the stdout of ls to the
stdin of sort . Since the output of sort isn't redirected, it is sent to
, which is the terminal screen. Another example:
harold harry lab1 poem richard stuff
smauney@sol:~/public $ ls | sort
>ls | sort | grep ha
After the second escape character \ there is also a space. The \ tell the command to use the next character as a regular character
grep command only sends
lines that contain the letter sequence "ha" to stdout . The output of ls is redirected to the input of sort.
sort 's output
is redirected to the input of grep . grep sends its output to stdout , the terminal screen.
is a command that processes an input stream of data and produces an output
stream of data. In the above example, sort and grep are examples of commands that are
Filters are used often in UNIX. You
can string together many filters with pipes to produce the
is a special filter that takes the data it gets from stdin and both redirects it to a file
and its stdout . An example:
>ls | sort | tee list | grep
The file list was created by tee before the
had a chance to modify the data stream.
smauney@sol:~/public $ cat list