0% found this document useful (0 votes)
20 views8 pages

Input, Output and Error Redirection

The document explains the concepts of input, output, and error redirection in Unix/Linux, detailing how to redirect command outputs to files or devices using file descriptors. It covers standard input, output, and error streams, along with practical examples of using redirection operators like '>', '>>', and '<'. Additionally, it provides a summary table of redirection syntax and exercises for practice.

Uploaded by

Geiken Wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Input, Output and Error Redirection

The document explains the concepts of input, output, and error redirection in Unix/Linux, detailing how to redirect command outputs to files or devices using file descriptors. It covers standard input, output, and error streams, along with practical examples of using redirection operators like '>', '>>', and '<'. Additionally, it provides a summary table of redirection syntax and exercises for practice.

Uploaded by

Geiken Wa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

School of ITCI Page 1 of 8

___________________________________________________________________________

Input, Output and Error Redirection


Introduction

Usually, whenever you run a command you get some output at the shell prompt. In case you
don't want that output to appear in the shell window, you can redirect it elsewhere. you can
make the output go into a file...or maybe go directly to the printer.. or you could make it
disappear.

This is known as Redirection. Not only can the output of commands be redirected, you can
also redirect the input for commands.

File Descriptors

One important thing you have to know to understand Redirection is file descriptors. In
Unix/Linux every file has a number associated with it called the file descriptor. And in
Unix/Linux everything is a file. Right from your devices connected to your machine to the
normal text files storing some information - all of these are looked at, as files by the
Operating System.

Similarly even your screen on which your commands display their output are files for
Unix/Linux. These have file descriptors associated with it. So when a program actually
executes it sends its output to this file descriptor and since this particular file descriptor
happens to be pointing to the screen, the output gets displayed on the screen. Had it been
the file descriptor of the printer, the output would have been printed by the printer.

Whenever any command is executed the command has 3 important files to work with. They
are standard input, standard output, and standard error. These are 3 files that are always
open when a command runs. You could consider them to be inherently present for all
commands. As explained before a file descriptor, is associated with each of these files -

File
Descriptor Points to -
Descriptor
0 Standard Input (Generally Keyboard)
1 Standard output (Generally Display/Screen/CLI)
2 Standard Error Ouput (Generally Display/Screen/CLI)

In the following, I cat the file text.root which is around 9MB in size, and then almost
immediately suspend the program with a CTRL+Z.

[uday@uday-VirtualBox:~]$ cat text.root

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 2 of 8
___________________________________________________________________________

Using the ps command, I next examine the process id (PID column) for the cat command.

After examining the PID for cat command, I next examine the process’s /proc/pid/fd
directory where pid is substituted by the cat command process id (PID) as shown below:

Not surprisingly, the cat process has the file /home/uday/text.root open (it must be able to
read the file to display its contents). The cat command has three open files before it, or,
more exactly, the same file open three times: /dev/pts/1. Also note the file descriptor
corresponding to /home/uday/text.root.

You could redirect any of these files to other files. In short if you redirect 1 (standard
output) to the printer, your command’s output would start getting printed instead of being
displayed on the screen.

What is the standard input? That would be your keyboard. Most of the times since you
enter commands with your keyboard, you could consider 0 to be your keyboard. Since you
get the output of your command on the screen, 1 would be the screen (display) and the
errors as well are shown on the screen to you, so 2 would also be the screen. Thus by
redirecting the standard error (2) to some file one can avoid seeing the error messages on
the screen!!

Output Redirection

The most common use of Redirection is to redirect the output (that normally goes to the
terminal) from a command to a file instead. This is known as Output Redirection. This is
generally used when you get a lot of output when you execute your program. Often you see
that screens scroll past very rapidly. You could get all the output in a file and then even
transfer that file elsewhere or mail it to someone.

The way to redirect the output is by using the ' > ' operator in shell command you enter. This
is shown below. The ' > ' symbol is known as the output redirection operator. Any command
that outputs its results to the screen can have its output sent to a file.
Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 3 of 8
___________________________________________________________________________

[uday@station ~]$ ls > listing

The ' ls ' command would normally give you a directory listing. Since you have the ' > '
operator after the ' ls ' command, redirection would take place. What follows the ' > ' tells
Unix/Linux where to redirect the output. In our case it would create a file named ' listing '
and write the directory listing in that file. You could view this file using any text editor or by
using the cat command.

Note: If the file mentioned already exists, it is overwritten. So care should be taken to enter
a proper name. In case you want to append to an existing file, then instead of the ' > '
operator you should use the ' >> ' operator. This would append to the file if it already exists,
else it would create a new file by that name and then add the output to that newly created
file.

Note: Also, when your shell sees a redirection operator like ‘>’ it will first create a file and
then run the command

In this example, I redirect the following command to /tmp/listing and then almost
immediately suspend the program with a CTRL+Z.

uday@uday-VirtualBox:~]$ ls –lR / > /tmp/listing

Using the same technique as before, I examine the files opened by the ls command and the
file descriptors associated with them.

Notice that the file descriptor 1 ( in other words Standard Output) is not connected to
terminal, but instead to the file /tmp/listing.

Input Redirection

Input Redirection is not as popular as Output Redirection. Since most of the times you
would expect the input to be typed at the keyboard. The general use of Input Redirection is

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 4 of 8
___________________________________________________________________________

when you have some kind of file, which you have ready and now you would like to use some
command on that file.

You can use Input Redirection by typing the ' < ' operator. An excellent example of Input
Redirection is shown below:

[uday@station ~]$ mail uday@localhost < my_typed_letter

The above command would start the mail program with contents of the file named '
my_typed_letter ' as the input since the Input Redirection operator was used.

Note: You can't have Input Redirection with any program/command. Only those commands
that accept input from keyboard could be redirected to use some kind of text files as their
input. Similarly Output Redirection is also useful only when the command/program sends its
output to the terminal.

Error Redirection

This is a very popular feature that is used by Unix/Linux users. Some commands in
Unix/Linux that you type outputs a lot of error messages which you would like to ignore. For
example whenever you perform a search for a file, you always get a lot of permission denied
error messages. There may be ways to fix those things. But the simplest way is to redirect
those error messages elsewhere so that it doesn't bother you.

Here is a way to redirect the error messages

[uday@station ~]$ find / -type f –iname "*.txt" 2>errorsfile

This above command would search for regular files having an extension of txt starting at the
/ directory recursively and whatever errors are generated while executing the find command
are added to a file named ' errorsfile ' rather than being displayed on the screen.

Remember that 2 is the standard error file descriptor. Thus ' 2> ' means redirect the error
output.

[uday@station ~]$ find / -type f –iname "*.txt" 2>>all_errors_till_now

The above command would be useful in case you have been saving all the error messages
for some later use. This time the error messages would append to the file rather than create
a new file.

You might realize that in the above case since I wasn't interested in the error messages
generated by the program I redirected the output to a file. But since those error messages
don't interest me I would have to go and delete that file created every time I run that
command. Else I would have several such files created all over whenever I redirect my
unwanted error output. An excellent way around is shown below
Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 5 of 8
___________________________________________________________________________

[uday@station ~]$ find / -name "s*.jpg" 2>/dev/null

What's /dev/null ????? it is something like a black hole. Whatever is sent to the ' /dev/null '
never returns. Neither does one know where it goes. It simply disappears. Isn't that fantastic
!! So remember.. whenever you want to remove something.. something that you don't want
...you could just send it to /dev/null

Different ways to use Redirection Operators

Suppose you want to create a text file quickly

[uday@station ~]$ cat > filename


This is some text that I want in this file
^D

That's it!! Once you type the ' cat ' command, use the Redirection operator and add a name
for a file. Then start typing your line. And finally press Ctrl+D. You will have a file named '
filename ' in the same directory.

Suppose you want to add a single line to an existing file.

[uday@station ~]$ echo "this is a new line" >> existing_file

That would add the new line to the file named ' existing_file ' . Remember to use ' >> '
instead of ' > ' else you would overwrite the file.

Suppose you wanted to join 2 files

[uday@station ~]$ cat file2 >> file1

This is a much neater way than to open a text editor and copy paste. The contents of ' file2 '
would be added to ' file1 ' .

Suppose you want to join a couple of files

[uday@station ~]$ cat file1 file2 > file3

This would add the contents of ' file1 ' and ' file2 ' and then write these contents into a new
file named ' file3 ' .

It is possible to re-direct both standard input and output. If you have a report containing
many spelling mistakes you may wish to keep a list of the mistakes in a file. You can do this
using the following command:

[uday@station ~]$ aspell list < report > errors

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 6 of 8
___________________________________________________________________________

Combining stdout and stderr to the same file: Old School

Often, someone would like to redirect the combined stdout and stderr streams to a single
file. As a first attempt, I try the following command.

[uday@station ~]$ head -1 /etc/network/* > netsummary.both 2> netsummary.both

Upon examining the file netsummary.both, however, I don't find what I expect.

The bash shell opened the file netsummary.both twice, but treated each open file
independently. When stdout and stderr both wrote to the file, they clobbered each other's
information. What is needed instead is some way to tell bash to effectively combine stderr
and stdout into a single stream, and then redirect that stream to a single file. As you would
expect, there is such a way.

[uday@station ~]$ head -1 /etc/network/* > netsummary.both 2>&1

Although awkward, the last token 2>&1 should be thought of as saying "take stderr, and
send it wherever stdout is currently going". Now netsummary.both contains the expected
output.

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 7 of 8
___________________________________________________________________________

Combining stdout and stderr to the same file: New School

Using 2>&1 to combine stdout and stderr was introduced in the original Unix shell, the
Bourne shell (sh). Because bash is designed to be backwards compatible with sh, it supports
the syntax as well. The syntax, however, is inconvenient. Besides being difficult to write, the
order of the redirections is important. Using ">out.txt 2>&1" and "2>&1 >out.txt" does not
have the same effect!

In order to simplify things, bash uses >& to combine both stdout and stderr, as in the
following example.

[uday@station ~]$ head -1 /etc/network/* >& netsummary.both

Summary

The following table summarizes the syntax used by the bash shell for redirecting stdin,
stdout, and stderr.

Table: Redirecting stdin, stdout, and stderr in bash

syntax effect
cmd < file Redirect stdin from file
cmd > file Redirect stdout into file, overwriting (clobbering) file if it exists.
cmd >> file Redirect stdout into file, appending to file if it exists.
cmd 2> file Redirect stderr into file, overwriting (clobbering) file if it exists.

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc
School of ITCI Page 8 of 8
___________________________________________________________________________

syntax effect
cmd 2>> file Redirect stderr into file, appending to file if it exists.
cmd > file 2>&1 Combine stdout and stderr, and redirect both into file. (Portable syntax)
cmd >& file Combine stdout and stderr, and redirect both into file. (Convenient syntax)

Exercises:

1. The hostname command reports your computer's currently assigned hostname. Run
the command (without arguments), and redirect the output to the file
~/stdoutlab.txt.
2. The uptime command reports how much time has passed since your machine was
booted, and other system utilization information. Run the uptime command
(without arguments), using redirection to append the output to the file
~/stdoutlab.txt.
3. The uname -a command lists information about your current kernel version. Run the
command, using redirection to append the output to the file ~/stdoutlab.txt.
4. Use the following command line to display the contents of all files within the
/etc/X11 directory.

[uday@station ~]$ cat /etc/X11/*

5. Repeat the above command line, but redirect stdout to a file called
~/stderrlab.out and stderr to a file called ~/stderrlab.err.
6. Repeat the command line in step 4 again, but combine stdout and stderr into a single
stream, and redirect the stream to the file ~/stderrlab.both.

Version Date 09/2012

Author : Uday Vaidya


Filename: Input, Output and Error Redirection.doc

You might also like