|Red Hat Enterprise Linux 4: Red Hat Enterprise Linux Step By Step Guide|
|Prev||Chapter 4. Shell Prompt Basics||Next|
From the shell's point of view, there are three kinds of information: standard input, standard output, and standard error. In simple terms, standard input is the information that a user enters from the keyboard for the shell to use, such as commands and filenames. Standard output is essentially the information the shell prints on the screen after evaluating a user's commands. Standard error is the information that indicates that something has gone wrong. Collectively, these three types of information are referred to as standard I/O (input/output).
The ability to manipulate and use information from the shell is one of the strengths of Red Hat Enterprise Linux. This section discusses the simpler aspects of standard I/O manipulation.
In Section 4.8.5 Viewing and creating files with cat, we saw that using cat to view a large file causes the contents to scroll off the screen. Using a pipe, we can control that behavior. A pipe is the ( | ) symbol. It is used to connect the standard output of one command to the standard input of another command. Essentially, it allows a user to string commands together. Pagers are commands (such as less) that display text in the terminal window.
Using cat, the pipe (|), and less together displays the file one page at a time. You can then use the up and down arrow keys to move backward and forward through the pages.
cat <filename> | less
The above command opens the file named <filename> using the cat command, but does not allow it to scroll off the screen.
Using the pipe with a pager is also useful when examining large directories with ls. For example, view the /etc/ directory with the ls command.
ls -al /etc/
Notice that the contents scroll past too quickly to view. To get a closer look at the output of the ls command, pipe it through less.
ls -al /etc/ | less
Now you can view the contents of
/etc/ one screen at a time. Remember
that you can navigate forward and backward through the screens
and even search for specific text using the
You can combine redirection with wildcards.
ls -al /etc/ a* | less
The above displays all files and directories in /etc/ that start with the letter "a" one screen at a time.
Redirection means changing where standard input comes from or where the standard output goes.
To redirect standard output, use the > symbol. Placing > after the cat command (or after any utility that writes to standard output) redirects its output to the file name following the symbol.
Remember that the cat command echoes the
text you enter on the screen. Those echoes are the standard
output of the command. To redirect this
output to a file, type the following at a shell prompt and
press Enter: cat > foo.txt. Enter a few
more lines of text, and use the
The following screen is an example, redirecting three lines of text into the file foo.txt.
The following screen is another example, redirecting three more lines of text to create the file bar.txt.
The following screen demonstrates cat's concatenate function, adding the contents of bar.txt to the end of foo.txt. Without redirection to the file example1.txt, cat displays the concatenated contents of foo.txt and bar.txt on the screen.
The following screen displays the contents of example1.txt, so that the user can see how the files were added together.
Be careful when you redirect the output to a file, because you can easily overwrite an existing file! Make sure the name of the file you are creating does not match the name of a pre-existing file, unless you want to replace it.
The symbol >> appends standard output. This means it adds the standard output to the end of a file, rather than over-writing the file.
The following screen shows the command to append bar.txt to foo.txt. The contents of bar.txt are now permanently added to the end of foo.txt. The cat command is called a second time to display the contents of foo.txt.
To compare example1.txt and the modified foo.txt, use the diff command. diff compares text files line-by-line and reports the differences to standard output. The following screen shows the comparison between example1.txt and foo.txt. Because there are no differences, diff returns no information.
You can also perform the same type of redirection with standard input.
When you use the redirect standard input symbol <, you are telling the shell that you want a file to be read as input for a command.
The following screen shows foo.txt being redirected as input for cat: