When we execute any program, by default, its output or error isdisplayedon the screen. We canredirectthe text output of a program to a file using the input/outputredirectionoperator or toanotherprogram 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 withstdin
(numbered as 0) used to read input from keyboard. The second file stream is associated tostdout
(numbered as 1) used by program to print output on screen, and the last file stream isstderr
(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+
Redirectingstdout
/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
tof1
by executing following command:
$ tail -n 10 /var/log/message > f1
-
Append the
date
to the file namedf1
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 namederror
:
$ find /etc/ -name passwd > result 2> error
- Save the output or error received upon execution of the
find
command in the common file named asboth
, 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
Pipes
The Unix philosophy is to have simple and short programs (commands) used together to solve a complex problem, instead of having a complex program with several options. To accomplish this goal, pipes were created, which fetch the output of one command as input to another command. The symbol used for pipes is a vertical bar (|
) between two or more commands, as follows:
$ command1 | command2 | command3 ..
Here are a few example showing the usage of pipes (redirecting the output from one program as an input to another program):
- Paginate the long output of any command using pipes, as shown in the following example:
$ ls -l /bin | less
- Pipe multiple commands together as shown in the following example:
$ ls | head -3 | tail -1
-
Combine piping and redirection, as shown in the following example:
$ ls | head -3 | tail -1 > output
- Send standard output, as well as standard errors from the command, through a pipe to another process, as shown in the following example:
$ find /etc -name passwd |& wc -l or find /etc -name passwd 2>&1 | wc -l
tee command
The advantage of using pipes is that you don’t have to save the output of a command in temporary files before passing it to another subsequent command for processing. This saves disk space and improves the time of execution, since reading and writing to disk are generally the slowest bottlenecks in the system. And, if you require the input to be saved in a file before passing it to a subsequent command, Linux has another beautiful command, known astee
. The syntax of the tee
command is as follows:
$ command1 | tee <log_filename> | command2 ......
Here is an example of tee
command usage:
- Send the output of the
ls
command to thewc
command and to a file namedlistoffile
:
$ ls | tee listoffile | wc
- Send standard output, as well as standard errors from the command, through a pipe-through tee to another file and then to a subsequent command:
$ find /etc -name passwd |& tee logfile | wc -l