How to use command redirection under Linux

Shell metacharacters

Shell metacharacters are specific characters, generally symbols, that have special meaning within the shell. The metacharacters supported in bash are listed as follows:

  • | : Sends the output of the command to the left as the input to the command on the right of the symbol
  • & : Runs the process in the background, allowing you to continue working on the command line
  • ; : Allows you to list multiple commands on a single line, separated by this character
  • () : Groups commands and sends their output to the same place
  • : Gets input for the command to the left from the file listed to the right of this symbol
  • > : Sends the output of the command on the left into the file named on the right of this symbol
  • space tab
Caution: Do not use these metacharacters when creating file and directory names. These characters hold special meaning in the shell.

Redirection Metacharacters

Command redirection is enabled by the following shell metacharacters:

  • Redirection of standard input (
  • Redirection of standard output (>)
  • Redirection of standard error (2>)
  • The pipe character (|)

The File Descriptors

Each process works with file descriptors. File descriptors determine where the input to the command originates and where the output and error messages are directed to. The table explains the file descriptors.

File Descriptor Number File Descriptor Abbreviation Definition
0 stdin Standard command input
1 stdout Standard command output
2 stderr Standard command error

Command Redirection

By default, the shell receives or reads input from the standard input, the keyboard and displays the output and error messages to the standard output, the screen. Input redirection forces a command to read the input from a file instead of from the keyboard. Output redirection sends the output from a command into a file instead of sending the output to the screen.

Redirecting Standard Input

The less than () metacharacter processes a file as the standard input instead of reading the input from the keyboard.

# command 

or

# command 0

For example, use the data.txt file as the input for the mailx command.

# mailx -s test test@example.com 

Redirecting Standard Output

The greater-than (>) metacharacter directs the standard output to a file instead of printing the output to the screen.

# command > filename

or

# command 1> filename

If the file does not exist, the system creates it. When you use a single greater-than (>) metacharacter, the command overwrites the original contents of the file, if the file already exists. When you use two greater-than (>>) characters, the command appends the output to the original content of the file.

$ command >> filename

Redirecting Standard Error

A command using the file descriptor number (2) and the greater-than (>) sign redirects any standard error messages to the /dev/null file.

$ command 2> /dev/null

The following example shows the standard output and the standard error redirected to the data.txt file.

$ ls /tmp 1> data.txt 2>&1
Note: The syntax 2>&1 instructs the shell to redirect stderr (2) to the same file that receives stdout (1).

The Pipe Character

The pipe character redirects the standard output from one command to the standard input of another command. The first command writes the output to standard output and the second command reads standard output from the previous command as standard input.

$ command | command

For example, use the standard output from the who command as the standard input for the wc -l command.

 $ who | wc -l 
35

To view a list of all the subdirectories located in the /etc directory, enter the following command.

$ ls -F /etc | grep "/" X11/
acct/
apache/
apache2/

Use the output of the head command as the input for the 'xargs ls' command.

head -10 data.txt | xargs ls 
Related Post