Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

NOTE: CentOS Enterprise Linux is built from the Red Hat Enterprise Linux source code. Other than logo and name changes CentOS Enterprise Linux is compatible with the equivalent Red Hat version. This document applies equally to both Red Hat and CentOS Enterprise Linux.

4.9. Manipulating Information from the Shell

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.

4.9.1. Pipes and Pagers

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 [/] key.

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.

4.9.2. Using Redirection

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 [Ctrl] -[D] key combination to quit cat.

The following screen is an example, redirecting three lines of text into the file foo.txt.

Figure 4-4. Redirecting Output to a File

The following screen is another example, redirecting three more lines of text to create the file bar.txt.

Figure 4-5. Redirecting Output to a Second File

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.

Figure 4-6. Concatenating Two Files

The following screen displays the contents of example1.txt, so that the user can see how the files were added together.

Figure 4-7. Contents of example1.txt

Caution Caution
 

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.

4.9.3. Appending Standard Output

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.

Figure 4-8. Appending bar.txt to 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.

Figure 4-9. diff Comparison of example1.txt and foo.txt

4.9.4. Redirecting Standard Input

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:

Figure 4-10. Redirecting Standard Input

 
 
  Published under the terms of the GNU General Public License Design by Interspire