4.0.1 Input/Output Redirection
Let's say that you would like to have a list of all the files in a certain directory stored in a text
file. How would you do that without opening a text editor and manually typing the name of each file
into the editor? There is a much quicker way to accomplish this task.
Before we go on, I'd like to talk a little about input and output. All the output that you have seen
so far has been the results of commands that get printed to the screen. There are other ways of using
output, such as redirecting the output to a file or program. The type of input that we have used in our
commands/programs has been from the keyboard, we can also get input from other places. Sure just
using standard input and output is great and all, but it gets a little annoying if the size of the input
is to big to type in to the keyboard all at once. This is where someone got the great idea of allowing
us to redirect output and input to and from files. Using this method allows us to get information from
a file and have the program use it and also allows us to save output for later use.
Linux commands use three different types of I/O streams to do everything. These three types of I/O
are called stdin, stdout, and stderr.
stdin: This is the basic input stream, information received by the
command. Usually it is from the keyboard.
stdout: This is the basic output stream, information sent by the
command. stdout usually is to the monitor.
stderr: This is the error stream, error messages are usually sent to the monitor.
So let's continue with our problem from above. To store the directory listing in a file all we have to
do is redirect the output of the ls command to a file. The file name can be what ever we
want it to be and will be created after the ls is finished and ready to output its results.
See this example:
[bob@host ~] ls -al
total 14
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
[bob@host ~] ls -al > list.txt
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 485 May 2 11:45 list.txt
[bob@host ~]
You won't see any output from the command, that is because we redirected the output to a file instead
of the screen. The > in the command means to redirect the output. If you want to see the
listing that we created you can type 'cat list.txt', this will print the contents of
list.txt to the screen.
[bob@host ~] cat list.txt
total 14
drwxr-xr-x 4 bob other 512 May 2 11:45 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 0 May 2 11:45 list.txt
[bob@host ~]
Let's do a little example on input redirection. We have a list of names stored in a file that we would
like to have sorted in alphabetical order. We can sort the list using the sort command and
input redirection. The sort command already has an option to accept a file to sort, but
if the programmer had not included this we could still get the same effect using the input redirect.
Let's see an example...
[bob@host ~] cat names.txt
Dustin
Brian
Carly
Shannon
Anna
Carly
Courtney
John
[bob@host ~]
Here we have the unsorted file, now lets use sort on it.
[bob@host ~] sort < names.txt
Anna
Brian
Carly
Carly
Courtney
Dustin
John
Shannon
[bob@host ~]
And there we go, it prints the list of names from the file in order. Let's try combining the input and
output redirection by using the unsorted names file and output the sorted list of names to a file.
[bob@host ~] ls -al
total 16
drwxr-xr-x 4 bob other 512 May 2 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52 May 2 12:44 names.txt
[bob@host ~] sort < names.txt > sorted.txt
[bob@host ~] ls -al
total 18
drwxr-xr-x 4 bob other 512 May 2 13:02 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 97 May 1 14:29 .tcshrc
drwxr-xr-x 3 bob other 512 Apr 29 15:32 dir1
-rw-r--r-- 1 bob other 5 Apr 29 15:37 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:59 file2
-rw-r--r-- 1 bob other 52 May 2 12:44 names.txt
-rw-r--r-- 1 bob other 52 May 2 13:02 sorted.txt
[bob@host ~] cat sorted.txt
Anna
Brian
Carly
Carly
Courtney
Dustin
John
Shannon
[bob@host ~]
There are more redirectors that you can use here is a list of them.
> file | Redirects standard output stream to file. |
2> file | Redirects standard error stream to file. |
>> file | Redirects standard output stream to file, appending output
to the file if the file already exists. |
2>> file | Redirects standard error stream to file, appending output
to the file if the file already exists. |
&> file | Redirects standard output and error streams to the file.
|
< file | Redirects standard input stream to the file.
|
<< text | Reads standard input until a line matching text is found, at which
point end of file is posted. |