3.8. Manipulating Files with cat

Red Hat Enterprise Linux has a utility which can help you keep short lists, gather lists together, and even show you information about your system.

The utility is called cat, short for concatenate, which means to combine files.

The command cat also displays the contents of an entire file on the screen (for example, type cat filename.txt). If the file is fairly long, it quickly scrolls past you on the screen. To prevent this, use the cat filename.txt | less command.

Using the pipe (|) and the less command 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. For more on using pipes to combine two separate functions, refer to Section 3.9 Pipes and Pagers.

3.8.1. Using Redirection

Redirection means causing the shell to change what it considers to be standard input or where the standard output should be going.

To redirect standard output, use the > symbol. Placing > after the cat command (or after any utility or application that writes to standard output) directs its output to the file name following the symbol.

For example, using cat by itself outputs whatever you input to the screen as if it were repeating the line you just typed. The following example shows cat repeating every line that is entered:

Figure 3-5. The cat Command

To redirect the output of cat to a file, type the following at a shell prompt (pressing the [Enter] key takes you to the next blank line):

cat > sneakers.txt

Figure 3-6. Redirecting Output to a File

Press [Enter] to go to an empty line and use the [Ctrl]-[D] key combination to quit cat.

Do you notice anything different in Figure 3-6? There are no repeated entries. That is because the standard output from cat was redirected. That redirection was to a brand new file you made called sneakers.txt.

You can find the file in the directory you were in when you started cat (type ls if you want to see it listed).

As you learned earlier, you can also use cat to read the file you have created. At the prompt, type:

cat sneakers.txt


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.

Use output redirection again for another file and call it home.txt. For this example, type the command cat > home.txt, then [Enter], followed by:

bring the coffee home
take off shoes
put on sneakers
make some coffee

Now, on an empty line, use the [Ctrl]-[D] key combination again to quit cat.

Next, use cat to join home.txt with sneakers.txt and redirect the output of both files to a brand new file called saturday.txt (as seen in Figure 3-7). Type the following:

cat sneakers.txt home.txt > saturday.txt

Figure 3-7. Joining Files and Redirecting Output

You can see that cat has added home.txt where sneakers.txt ended.

3.8.2. Appending Standard Output

You can use output redirection to add new information to the end of an existing file. Similar to when you used the > symbol, you tell your shell to send the information somewhere other than standard output.

However, when you use >>, you are adding information to a file, rather than replacing the contents of a file entirely.

The best explanation is a demonstration. Take two files which have already been created (sneakers.txt and home.txt) and join them by using the append output symbol. To add the information in home.txt to the information already in sneakers.txt, type:

cat home.txt >> sneakers.txt

Now check the file using the command cat sneakers.txt. The final output shows the contents of home.txt at the end of the file:

buy some sneakers
then go to the coffee shop
then buy some coffee
bring the coffee home
take off shoes
put on sneakers
make some coffee

The command you typed appended the output from the file home.txt to the file sneakers.txt.

By appending the output, you save yourself time (and a bit of disk clutter) by using existing files, rather than creating a new file.

Compare the results of the files sneakers.txt and saturday.txt, and you can see that they are identical. To make your comparison, type:

cat sneakers.txt; cat saturday.txt

The contents of both files are displayed — first sneakers.txt, then saturday.txt (as shown in Figure 3-8).

Figure 3-8. Stringing Commands and Comparing Files

3.8.3. Redirecting Standard Input

Not only can you redirect standard output, you can 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.

Use a file you have already created to demonstrate this idea. Type:

cat < sneakers.txt

Because you used the less-than symbol (<) to separate the cat command from the file, the output of sneakers.txt was read by cat.

Figure 3-9. Redirecting Standard Input