Red Hat Linux 8.0: The Official Red Hat Linux Getting Started Guide | ||
---|---|---|
Prev | Chapter 11. Shell Prompt Basics | Next |
Red Hat 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 will also display the contents of an entire file on the screen (for example, type cat filename.txt). If the file is fairly long, it will quickly scroll 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, see the Section called Pipes and Pagers.
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) will direct its output to the filename following the symbol.
For example, using cat by itself simply 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:
[sam@halloween sam]$cat buy some sneakers buy some sneakers then go to the coffee shop then go to the coffee shop then buy some coffee then buy some coffee |
To redirect cat output to a file, type
the following at a shell prompt (pressing the
[sam@halloween sam]$cat > sneakers.txt buy some sneakers then go to the coffee shop then buy some coffee |
Press
Do you notice anything different in Figure 11-5? 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 then use cat to read the file. At the prompt, type:
cat sneakers.txt |
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. |
Use output redirection again for another file and call it home.txt. For this example, type the following:
[sam@halloween sam]$cat > home.txt bring the coffee home take off shoes put on sneakers make some coffee relax! |
Now, on an empty line, use the
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 (you will find an example in Figure 11-6). Type the following:
[sam@halloween sam]$cat sneakers.txt home.txt > saturday.txt |
You can see that cat has added home.txt where sneakers.txt ended.
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. You want to add the information in home.txt to the information already in sneakers.txt, so 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.
[sam@halloween sam]$cat sneakers.txt 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 relax! [sam@halloween sam]$ |
The command you typed told the system to append 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 now, and you will see that they are identical. To make your comparison, type:
cat sneakers.txt; cat saturday.txt |
The contents of both files will be displayed — first sneakers.txt, then saturday.txt (as shown in Figure 11-7).
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. Just 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.