Red Hat Linux 7.2: The Official Red Hat Linux Getting Started Guide | ||
---|---|---|
Prev | Chapter 10. Shell Prompt Basics | Next |
Red Hat Linux has a utility which can help you keep short lists, gather those lists together and, at the same time, show you a little of the power behind your system.
The utility is called cat, short for "concatenate," which means that it strings files together.
The command cat will also display the contents of an entire file on the screen (for example, type cat filename.txt). Using cat can be handy if the file is fairly short. But if a file is fairly long, it will quickly scroll past you on the screen, since cat displays the whole file.
But cat can also perform a quick demonstration of two important terms: standard input and standard output.
Standard input and standard output direct input and output (often referred to as I/O) to the user. If a program reads from standard input, it will by default be reading from the keyboard. If a program writes to standard output, by default it will be writing to the screen.
Start cat to see what this means. At the shell prompt, type:
cat |
The cursor moves to a blank line. Now, in that blank line, type:
stop by sneaker store |
and press the
[newuser@localhost newuser]$ cat stop by sneaker store stop by sneaker store |
To quit cat, move the cursor to a blank line by
pressing
The cat command has just demonstrated the definition of standard input and standard output; you typed words (standard input) and they appeared on the screen (standard output).
Redirection means causing the shell to change what it considers to be standard input or where the standard output is going.
You used cat before to demonstrate the idea behind standard input and standard output. Now use cat to see how standard output can be redirected.
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.
Try it. In a shell prompt type:
[newuser@localhost newuser]$ cat > sneakers.txt buy some sneakers then go to the coffee shop then buy some coffee |
Now press
Notice the difference (see Figure 10-8)? For one thing, there are no double 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).
You can even use cat to read the file, by typing:
cat sneakers.txt |
at the prompt.
Do Not Overwrite Files | |
---|---|
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. |
Let's use output redirection for another file and call it home.txt. Type the following:
[newuser@localhost newuser]$ cat > home.txt bring the coffee home take off shoes put on sneakers make some coffee relax! |
Now, on an empty line, use the
We can check the file again by typing:
cat home.txt |
at the prompt.
Use cat again to join home.txt with sneakers.txt and redirect the output of both files to a brand new file called saturday (you will find an example in Figure 10-9). Type the following:
[newuser@localhost newuser]$ cat sneakers.txt home.txt > saturday |
Now it is time to check your work. Type:
[newuser@localhost newuser]$ cat saturday |
and you should see something like this:
[newuser @localhost newuser]$ cat saturday 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! [newuser @localhost newuser]$ |
You can see that cat has added home.txt where sneakers.txt left off.
Combining Files With cat | |
---|---|
Creating and combining short files with cat can be a convenient alternative to using a text editor like Pico. |
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, rather than replacing it.
The best explanation is a demonstration, so 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 by typing:
cat sneakers.txt |
And there it is, with the contents of home.txt at the end.
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 a step or two (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 now, and you will see that they are identical. To make your comparison, type:
cat sneakers.txt; cat saturday |
The contents of both files will be displayed — first sneakers.txt, then saturday (as shown in Figure 10-10).
Do Not Replace When You Append | |
---|---|
Remember that when you append output, have to include two greater-than symbols (>>). Otherwise, you will end up replacing the very file to which you want to append information! |
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.