How to Redirect Output to a File or Process?

The output of a process can be either redirected to a file or to another process i.e., the output of a command will not appear on the screen but will be saved in a file or it can act as an input of another command. The redirection operators like >, >>, &>, 2> etc are used to redirect output to a file and the concept of pipes (|) is used to redirect output to another process.
Before learning how to Redirect Output to a File or Process? let’s understand the basics first.

Standard Input, Standard Output and Standard Error

The figure below shows the channels via which a process can receive input, send output and error messages.

How to Redirect Output to a File or Process?

Standard Input (stdin): is the input channel which is used by the process to receive an input. The file descriptor reserved for this channel is ‘0’.

Standard Output (stdout): is the output channel which is used by the process to send an output. The file descriptor reserved for this channel is ‘1’.

Standard Error (stderr): is the channel which is used by the process for an error message. The file descriptor reserved for this channel is ‘2’.

For any communication (either receiving data or sending data) with the user files, file descriptor from 3 onwards are used.

The table below summarizes the channels, their default connection and usage.

NumberChannel NameDescriptionDefault ConnectionUsage
0stdinStandard InputKeyboardRead only
1stdoutStandard OutputTerminalWrite only
2stderrStandard ErrorTerminalWrite only
3+filenameOther filesnoneread and/or write

By default, the stdout and stderr channels use terminal as the output device. Now if you want that the output of a process is not displayed on the screen, use redirection to save the output in a file.

Redirect stdout to a file

How to Redirect Output to a File or Process?

To redirect output to a file use the ‘>’ symbol followed by the filename. For example,

$ls >output.txt

will save the output of ls command in the file output.txt. Compare the output with using ls alone. If the file doesn’t already exist, using the redirection symbol will create it.
Note: Using the redirection (>), will overwrite the original contents of the file.

Redirect stdout to a file; append to current file content

appending stdout to file

If you do not want to overwrite the contents of the existing file then use the double redirection like

$date >>output.txt

will append the output of date command to the original contents of the file output.txt.

Redirect stderr to a file

Just as redirecting the output, it is also possible to redirect the error from a process into a file.

redirecting stderr to file

Now, instead of using just the ‘>’ symbol use ‘2>’ followed by file name. For example, suppose a file ‘file.txt’ does not exist in your system. So if you use

$ls file.txt

it will show an eror

ls: cannot access 'file.txt': No such file or directory

Now, if you want to save this error message into a file, you need to redirect the stderr into a file like,

$ls file.txt 2>output.txt

Now, check the content of output.txt. The error message will be here.

Discard stderr messages by redirecting to /dev/null

How to Redirect Output to a File or Process?

This option allows to discard error messages. So the only change required is instead of giving a file name to store the file just write /dev/null. Foe example

$ls file.txt 2>/dev/null

Combine stdout and stderr to a file

redirecting stdout and stderr to file

Next, both the stdout and stderr can be redirected towards the same file. To do this use ‘&>filename’. For example

$ls file.txt &>output.txt

Now, it doesn’t matter whether file.txt exists or not. In either case the output will be in output.txt. So, execute the above command in two ways:
1) when file.txt doesn’t exist
2) create the file file.txt and then rerun the command.

Combine stdout and stderr to a file; append to current file content

Now, the last thing is if you want to redirect stdout and stderr into the same file and append it to the existing contents of the target file. The method will be:

>>filename 2>&1

For example:

$ls file.txt >>output.txt 2>&1

Redirecting output to another Process – Pipes

Pipes are used to send output of a process to another process i.e., the Output of one process becomes the input of another process. The symbol used is ‘|’.

For example, if you want to count the number of files and sub-directories in your current working directory, you will have to use the following combination of commands

$ls >file1
$wc -l file1

How it works?

The output of ls command i.e., all the files and sub-directories gets saved in file1. Then we count the number of lines using wc -l, which gives the count of total number of files and sub-directories. The alternative is, use pipes

$ls | wc -l

Now, the output of ls is send as an input to the wc -l command.

More Examples

Q1. Show the first ten entries of the ls command output.

$ls | head

Q2. Sort the output of ls command output as per the months and save the output in the file result.txt

$ls | sort -M > result.txt

PPT on Pipes and Redirection

Video on Pipes and Redirection

Practice Questions on How to Redirect Output to a File or Process?

Q1. Write the command to display only the 6th and 7th lines of a file.

Q2. Write the command to display the output of ls command on the screen and also save the output in a file result.txt.

Other Relevant Commands

touch
ls
head
tail

Leave a Reply

Your email address will not be published. Required fields are marked *