One of the basic philosophies behind the provision of commands in Linux is to make available many simple functions which perform specific tasks, efficiently. These simple functions can easily be bolted together to build up larger and more complex commands.
Wherever it makes sense, UNIX (and hence Linux) commands are configured so that, by default, they take any input they require from the keyboard and send any ordinary output to the screen. Commands also arrange to send their error output to the screen by default. Sometimes it would be useful if you could take input from or send output to a file instead. In these cases it would be possible to write a command so that you could specify file names as extra or optional command line parameters. Some commands do have this option. However, to provide this functionality for every command on the off chance it might come in handy, would make the executable files rather larger than they would otherwise need to be.
In order to avoid the necessity for this, Linux and the shell together provide facilities that allow a program's standard input and output to be redirected as required. Consider the command:
$ ls -1 /usr/tmp >dir
What this does is to create a long listing of the contents of the directory /usr/tmp. Normally, that listing would be displayed on the screen. But when the command includes a greater than (>) symbol followed by a file name, then the output will be sent to the specified file instead of to the screen. Using this form of output redirection, the specified file will be created if it does not already exist, or its present contents will be overwritten if it does already exist.
That this has actually happened is easy to verify, as follows:
$ cat dir total 4 drwxr-xr-x 2 pc book 1024 May 22 23:31 bin drwxr-xr-x 2 pc book 1024 May 22 23:31 doc drwxr-xr-x 2 pc book 1024 May 22 23:31 lib drwxr-xr-x 2 pc book 1024 May 22 23:31 src
Remember that the ls command does not know about the special function of the > character. In fact, the ls command does not even see the >, as the character and the following file name are removed by the shell before any command line parameters are passed on to the command. When ls runs, it just continues to send its output to the standard output device as usual, except that the shell has redirected the standard output away from the screen and into the specified file before ls was started. The same is true for the other redirection operators, as you will soon see. Sometimes, when using output redirection, you may have an existing file to which you wish to append the output generated by another command. This can be done by using the append redirection operator >>, which is just made up of two greater than symbols, side by side:
$ ls /usr/tmp >>dir $ cat dir total 4 drwxr-xr-x 2 pc book 1024 May 22 23:31 bin drwxr-xr-x 2 pc book 1024 May 22 23:31 doc drwxr-xr-x 2 pc book 1024 May 22 23:31 lib drwxr-xr-x 2 pc book 1024 May 22 23:31 src bin doc lib src
Incidentally, the cat command will take its input from the standard input device (keyboard, by default) if no file names are given as command line parameters. Input from the keyboard is terminated by entering the end-of-file character (Ctrl-d) at the start of a line on its own. Given this information, and the use of output redirection, the following simple command will allow you to write text directly from the keyboard into a file:
$ cat >text.file any text entered here goes into text.file up to Ctrl-d $
As well as redirecting a program's standard output, it is also possible to redirect its error output. The standard output and error output from a program are normally treated as two separate things so that you can redirect them individually. So, for example, if you wanted to see the normal output from a program on the screen but wanted to send any output error messages to a file so you could review them later, you could achieve this with:
$ ls /usr/tmp 2>err.file bin doc lib src
using the notation 2> (or 2>> to append) to indicate redirection of the error output device.
In order to send both standard output and error output to the same file at the same time, another output redirection operator is used (&>):
$ ls /usr/tmp &>output.file
Just as a program's output can be written to a file, so its standard input can be read from a file instead of the keyboard. This is done with the less than (<) redirection operator:
$ wc </etc/passwd 21 42 775
Notice that the name of the file /etc/passwd does not appear in the output from this wc command. This is because wc does not see the file name - it is dealt with by the shell. The wc command operates as though its input was coming from the keyboard, with no file name involved.
Another type of input redirection is known as a here document. This type of redirection tells the shell to set up the current command so that its standard input comes from the command line. The redirection operator in this case is << and the text to be redirected into the command is enclosed between a matched pair of delimiter words, as follows:
$ wc <<delim > this text forms the content > of the here document, which > continues until the end of > text delimiter > delim 4 17 98
When you enter the text for a here document, you do not need to type in the'>' characters at the start of each line they are supplied by the shell as a prompt. This prompt is used by the shell to tell you that the current command is unfinished and that it still expects more input before it can execute your command.
Any word can be used after the << operator as a start of text delimiter (delim in this example). The text of the here document continues until the same delimiter word is encountered again at the start of a line on its own. At that time the text of the here document, excluding the start and end delimiters, is redirected into the command (wc in this case) as its standard input text.
It is possible, using redirection, to combine commands to obtain new functionality from the system which no single command provides alone. For instance, to find the number of files in a directory you could use the command sequence:
$ ls /usr/bin >/tmp/dir $ wc -w </tmp/dir 459 $ rm /tmp/dir
This sequence runs the ls command on the directory /usr/bin and redirects the output to /tmp/dir. The second command counts the number of words in the output file from ls and displays the result - 459, a lot of files in this particular directory. As in this example, you should always make sure you remove any temporary files you create in /tmp as soon as you have finished with them so that they don't just get forgotten and waste disk space.