Redirecting output to files and programs

When we execute any program, by default, its output or error is displayed on the screen. We can redirect the text output of a program to a file using the input/output redirection operator or to another program using pipes. For this, when any command is executed, there are three standard file streams (file descriptors) created and opened by the operating system. The streams are known as standard input (stdin), standard output (stdout), and standard error (stderr).

The first stream is associated with stdin (numbered as 0) used to read input from keyboard. The second file stream is associated to stdout (numbered as 1) used by program to print output on screen, and the last file stream is stderr (numbered as 2), used by our program to print errors onscreen.

The following table list the different file descriptors (also known as channel) along with their numeric value, default connection and symbolic name:

Channel description

Channel symbolic name

Default connection

Descriptor value

Standard input

stdin

Keyboard

0

Standard output

stdout

Terminal

1

Standard error

stderr

Terminal

2

Other files

filename

Other files

3+

 

Redirecting stdout/stderr to a file prevents any process output from appearing on the Terminal. Linux has got a special file such as /dev/null which discards any channel output redirected to it. The less than symbol (<) is used for input redirection from a file, the greater than symbol (>) is used for output redirection to a file, and if we repeat the use of the output redirection symbol (>>) twice instead of once, then it appends the contents to the filename suffixed to it.

The following table explains the use of input/output redirection operators:

Operator usage

Explanation

Cmd > file

Redirects the command output to a file

Cmd >> file

Redirects and append the command output to the current file content

Cmd 2> file

Redirects the command standard error to a file

Cmd 2>> file

Appends the command standard error to the current file contents

Cmd 2> /dev/null

Discards standard error messages by redirecting them to /dev/null

Cmd &> file or

Cmd >file 2>&1

Redirects both standard output and standard error messages to one file

Cmd >>file 2>&1

Appends both standard output and standard error messages to one file

 

Here are some examples of output redirection:

  • Save the last 10 lines of /var/log/messages to f1 by executing following command:

    $ tail -n 10 /var/log/message > f1
  • Append the date to the file named f1 by executing following command:

    $ date >> f1
  • Save the errors in a file named error and display the output onscreen by executing following command:

    $ find /etc/ -name passwd 2> error
  • Save the output in a file named result and the error in a file named error:

    $ find /etc/ -name passwd > result 2> error
  • Save the output or error received upon execution of the find command in the common file named as both, as shown in the following command line:

    $ find /etc/ -name passwd &> both or
    $ find /etc/ -name passwd > both 2>&1
  • Save the output in a file named result and discard the error messages by executing the following command:

    $ find /etc/ -name passwd > result 2> /dev/null
  • Read from a file using input redirection by executing the following command:

    $ wc -l < /etc/hosts
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.90.131