Redirection in Linux/Unix

What is Redirection?

Redirection is a feature in Linux such that when executing a command, you can change the standard input/output devices. The basic workflow of any Linux command is that it takes an input and give an output.

  • The standard input (stdin) device is the keyboard.
  • The standard output (stdout) device is the screen.

With redirection, the above standard input/output can be changed.

In this tutorial, we will learn-

Output Redirection

The cat command reads the file music.mp3 and sends the output to /dev/audio which is the audio device. If the sound configurations in your PC are correct, this command will play the file music.mp3

File Descriptors (FD)

In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are files. Every File has an associated number called File Descriptor (FD).

Your screen also has a File Descriptor. When a program is executed the output is sent to File Descriptor of the screen, and you see program output on your monitor. If the output is sent to File Descriptor of the printer, the program output would have been printed.

Error Redirection

Whenever you execute a program/command at the terminal, 3 files are always open, viz., standard input, standard output, standard error.

Redirection in Linux/Unix - Demystified!

These files are always present whenever a program is run. As explained before a file descriptor, is associated with each of these files.

FileFile Descriptor
Standard Input STDIN0
Standard Output STDOUT1
Standard Error STDERR2

By default, error stream is displayed on the screen. Error redirection is routing the errors to a file other than the screen.

Why Error Redirection?

Error re-direction is one of the very popular features of Unix/Linux.

Frequent UNIX users will reckon that many commands give you massive amounts of errors.

  • For instance, while searching for files, one typically gets permission denied errors. These errors usually do not help the person searching for a particular file.
  • While executing shell scripts, you often do NOT want error messages cluttering up the normal program output.

The solution is to re-direct the error messages to a file.

Example 1

$ myprogram 2>errorsfile

Redirection in Linux/Unix - Demystified!

Above we are executing a program names myprogram.

The file descriptor for standard error is 2.

Using “2>” we re-direct the error output to a file named “errorfile”

Thus, program output is not cluttered with errors.

Example 2

Here is another example which uses find statement –

find . -name 'my*' 2>error.log

Using the “find” command, we are searching the “.” current directory for a file with “name” starting with “my”

Redirection in Linux/Unix - Demystified!

Example 3 Let’s see a more complex example,

Server Administrators frequently, list directories and store both error and standard output into a file, which can be processed later. Here is the command.

ls Documents ABC> dirlist 2>&1

Here,

  • which writes the output from one file to the input of another file. 2>&1 means that STDERR redirects to the target of STDOUT (which is the file dirlist)
  • We are redirecting error output to standard output which in turn is being re-directed to file dirlist. Hence, both the output is written to file dirlist

Redirection in Linux/Unix - Demystified!

Summary

  • Each file in Linux has a corresponding File Descriptor associated with it
  • The keyboard is the standard input device while your screen is the standard output device
  • “>” is the output redirection operator. “>>” appends output to an existing file
  • “<” is the input redirection operator
  • “>&”re-directs output of one file to another.
  • You can re-direct error using its corresponding File Descriptor 2.
top
X
Welcome to Our Website
Welcome to WPBot
wpChatIcon