left-icon

Linux Succinctly®
by Jason Cannon

Previous
Chapter

of
A
A
A

CHAPTER 10

I/O Redirection

I/O Redirection


The Linux operating system features a concept called I/O streams. The three default I/O streams are standard input, standard output, and standard error. When a process is launched, it is connected to these three I/O streams, also called standard streams. By default, standard input comes from your keyboard while standard output and standard error are displayed on your screen. By convention, standard output is used for normal output while standard error is reserved for error messages.

Each stream is assigned a file descriptor. A file descriptor is referenced by a number and represents an open file. Standard input is assigned file descriptor 0, standard output is assigned file descriptor 1, and standard error is assigned file descriptor 2. This effectively means that your keyboard and display are treated as files. As a matter of fact, your keyboard and display can be substituted for actual files. This layer of abstraction allows you to save output that would normally appear on your screen to a file. It also allows you send input to a command from a file. You can even use the output of one command as the input for another command.

  1. Standard Streams

Stream

Abbreviation

File Descriptor

standard input

stdin

0

standard output

stdout

1

standard error

stderr

2

Many Linux commands allow you to provide input by specifying a file as an argument or by accepting standard input. In the absence of a file, many commands expect standard input. Files, as well as standard input, are terminated with an end of file (EOF) marker. You can produce this EOF marker using your keyboard by typing Ctrl-d.

As an example of this behavior, let's look at the sort command. To have sort operate on a file, supply that file as an argument as in the following example.

$ cat test.txt

e

a

c

b

d

$ sort test.txt

a

b

c

d

e

$

To have sort operate on standard input, run the sort command without any arguments and start typing text. When you are finished, type Ctrl-d to send the EOF character. The standard input you provided will then be sorted.

$ sort

e

a

c

b

d

<Ctrl-d>

a

b

c

d

e

$

To send the standard output of one command as the standard input to another command, use a pipe symbol (|) between the commands. The following example demonstrates sending the output of cat text.txt as the input to the sort command.

$ cat test.txt | sort

a

b

c

d

e

$

To use the contents of a file as standard input, separate the command from the file with a less than sign (<).

$ sort < test.txt

a

b

c

d

e

$

To redirect the output of a command to a file, use the greater than sign (>) followed by a file name. If the file doesn't exist it will be created. If it does exist, it will be overwritten.

$ sort test.txt  > sorted.txt

$ cat sorted.txt

a

b

c

d

e

$

If you want to append output to a file, use the double greater than sign (>>). If the file doesn't exist, it will be created, but if it does exist, the output from the command will be appended to the file.

$ sort test.txt  >> sorted.txt

$ cat sorted.txt

a

b

c

d

e

a

b

c

d

e

$

You are not limited to just redirecting input or just redirecting output—you can do both at the same time. The following example demonstrates reading standard input from test.txt while redirecting standard output to sorted.txt.

$ sort < test.txt > sorted.txt

$ cat sorted.txt

a

b

c

d

e

$

  1. Redirecting I/O

Action

Format

Operator

Create or overwrite file with standard output from cmd.

cmd > file

>

Create or append to file with standard output from cmd.

cmd >> file

>>

Use the contents of file as standard input to cmd.

cmd < file

<

By default, input redirection operates on file descriptor 0 and output redirection operates on file descriptor 1. You can explicitly declare a file descriptor to use with redirection by immediately preceding the operator with the file descriptor number. Do not use a space between the file descriptor number and the redirection operator. If the file descriptor does not immediately precede the redirection operator, it will be interpreted as another item on the command line.

To capture error messages to a file while displaying standard output to your screen, use 2> file. You can also redirect standard output to one file while redirecting standard output to another.

$ ls test.txt no-such-file

ls: cannot access no-such-file: No such file or directory

test.txt

$ ls test.txt no-such-file 2>errors

test.txt

$ cat errors

ls: cannot access no-such-file: No such file or directory

$ ls test.txt no-such-file 1>normal-output 2>errors

$ cat normal-output

test.txt

$ cat errors

ls: cannot access no-such-file: No such file or directory

$

Not only can you redirect output to a file, you can redirect it to another file descriptor using an ampersand followed by the file descriptor number. Using this method, you can combine standard output and standard error using 2>&1. Do not use spaces.

The following command means, "send the standard output of the ls command to the combined-output file and append standard error to standard output."  All output will be sent to the combined-output file because standard error is redirected to standard output and standard output is redirected to combined-output.

$ ls test.txt no-such-file > combined-output 2>&1

$ cat combined-output

ls: cannot access no-such-file: No such file or directory

test.txt

$

If you do not want to display the output of a command to your screen or save it to a file, you can redirect the output to the null device, /dev/null. This special file simply discards any input that is sent to it. The null device is sometimes referred to as the "black hole" or "bit bucket". The following example redirects the errors from sort to the null device.

$ ls test.txt no-such-file

ls: cannot access no-such-file: No such file or directory

test.txt

$ ls test.txt no-such-file 2>/dev/null

test.txt

$

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.