How to Use Command Redirection in Linux
Using Command Redirection
You can redirect input to and output from commands by using redirection. There are two redirection commands: the greater than (>) and the less than (<) metacharacters. Both of these commands are used with the pipe (|) character. Typically, the shell receives or reads command input from the keyboard, and displays or writes the command output to the screen. The figure below shows standard input and output.
You can instruct the shell to redirect command input from and command output to files, on the command line, or within shell scripts. 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. As the command generates error messages, these messages are sent to the standard error. Usually, error messages are sent to the terminal screen.
The File Descriptors
Each process that the shell creates works with file descriptors. File descriptors determine where the input to the command originates and where the output and error messages are sent. Table 6-3 shows the file descriptors. Table 6-3 File Descriptors
File Descriptor Number | File Description Abbreviation | Definition |
---|---|---|
0 | stdin | Standard command input |
1 | stdout | Standard command output |
2 | stderr | Standard command error |
All commands that process file content read from the standard input and write to the standard output. The following example shows the standard input (bold text) and the standard output (plain text) for the cat command. Press Control-D to exit the cat command.
$ cat (Read from stdin)
First line (Read from stdin)
First line (Write to stdout)
What’s going on? (Read from stdin)
What’s going on? (Write to stdout)
Control-d (Read from stdin)
$
The cat command takes the standard input from the keyboard and sends the standard output to the terminal window. You can modify the default action of the standard input, standard output, and standard error within the shell by redirecting stdin, stdout, and stderr.
Redirecting Standard Input
The following syntax example shows a command using the less than (<) metacharacter to process a file as the standard input instead ofreading the input from the keyboard:
$ command < filename
or
$ command 0 < filename
To use the dante file as the input for the mailx command, perform the command:
$ mailx user1 < ~/dante
Redirecting Standard Output
The following syntax example shows a command using the greater than (>) metacharacter to direct the standard output to a file instead of printing the output to the screen. If file does not exist, the system creates it. If the file exists, the redirection overwrites the contents of the file.
$ command > filename
or
$ command 1>filename
To list the current contents of your home directory and redirect that list of files and directories into a file called directory_list, perform the commands:
$ cd
$ pwd
/export/home/user1
$ ls -l > directory_list
The following example shows a command using two greater than (») characters to direct the standard output to the end of existing content in a file. The standard output gets appended to the existing content. If the file does not exist, the system creates it.
$ command >> filename
To append the string That’s my directory_list file to the end of the my_file file, perform the command:
$ ls -l > my_file; cat my_file
-rw-r--r-- 1 user1 staff 1319 Jun 28 2002 dante
-rw-r--r-- 1 user1 staff 368 Jun 28 2002 dante_1
drwxr-xr-x 5 user1 staff 512 Jun 28 2002 dir1
drwxr-xr-x 3 user1 staff 512 Jun 28 2002 dir2
drwxr-xr-x 3 user1 staff 512 Jun 28 2002 dir3
drwxr-xr-x 2 user1 staff 512 Jun 28 2002 dir4
drwxr-xr-x 2 user1 staff 512 Jun 28 2002 dir5
... (output truncated)
$ echo "That’s my directory_list file" >> my_file; cat my_file
-rw-r--r-- 1 user1 staff 1319 Jun 28 2002 dante
-rw-r--r-- 1 user1 staff 368 Jun 28 2002 dante_1
drwxr-xr-x 5 user1 staff 512 Jun 28 2002 dir1
drwxr-xr-x 3 user1 staff 512 Jun 28 2002 dir2
drwxr-xr-x 3 user1 staff 512 Jun 28 2002 dir3
drwxr-xr-x 2 user1 staff 512 Jun 28 2002 dir4
drwxr-xr-x 2 user1 staff 512 Jun 28 2002 dir5
... (output truncated)
That’s my directory list file $
Redirecting Standard Error
The following example shows a command using the file descriptor number (2) and the greater than (>) character to redirect any standard error messages to the /dev/null file. This redirection is useful to suppress error messages so that no error messages appear on the screen.
$ command 2> /dev/null
The following example shows you how to direct error messages to the /dev/null file and list the errors you would see if not for the redirection of stderr.
$ find /etc -type f -exec grep PASSREQ {} \; -print
# PASSREQ determines if login requires a password.
PASSREQ=YES
/etc/default/login
grep: can’t open /etc/inet/mipagent.conf-sample
grep: can’t open /etc/inet/mipagent.conf.fa-sample
grep: can’t open /etc/inet/mipagent.conf.ha-sample
grep: can’t open /etc/security/dev/audio
grep: can’t open /etc/security/dev/fd0
... (output truncated)
$ find /etc -type f -exec grep PASSREQ {} \; -print 2> /dev/null
# PASSREQ determines if login requires a password.
PASSREQ=YES
/etc/default/login
$
The following example shows a command redirecting the standard output to a file and the standard error to the same file. The syntax 2>&1 instructs the shell to redirect stderr (2) to the same file that receives stdout (1).
$ command 1> filename 2>&1
To print the standard output and the standard error to the dat file, perform the command:
$ ls /var /test 1> dat 2>&1
$ more dat
/test: No such file or directory (stderr)
/var: (stdout)
adm (stdout)
audit (stdout)
cron (stdout)
... (output truncated)
The Pipe Character
The following example shows a command using the pipe (|) character to redirect the standard output to the standard input of another command:
$ command | command
You can insert a pipe character between any two commands the first command writes the output to standard output and the second command reads standard output from the previous command as standard input. To use the standard output from the who command as the standard input for the wc -l command, perform the command:
$ who | wc -l
35
$
The output of the who command never appears on the terminal screen because it is piped directly into the wc -l command. To view a list of all the subdirectories located in the /etc directory, perform the command:
$ ls -F /etc | grep "/"
X11/
acct/
apache/
apache2/
apoc/
You can use pipes to connect numerous commands. To use the output of the head command as the input for the tail command and print the results, perform the command:
$ head -10 dante | tail -3 | lp
request id is printerA-177 (Standard input)
Command-Line Interpreter
When you type a command on the command line, the shell interprets the command by parsing the line, handling metacharacters and redirection, and controlling the execution of commands. It then searches for the command and executes it. The shell analyzes each typed line and initiates the execution of the requested program.
For example, the shell takes the command:
$ ps -ef | sort +1 | more
and does the following:
- The shell breaks up the command line into its components, called tokens, as follows: ps, -ef, |, sort, +1, |, and more.
- The shell determines that ps, sort, and more are commands, -ef and +1 are arguments, and the pipe (|) is an I/O operation.
- Based on the shell parse order, the shell sets up stdout from ps to be stdin to sort and stdout from sort to be stdin to more.
- The shell locates the ps, sort, and more commands and then execute them in order, applying the arguments as specified.