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

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

  




 

 

13.9. Manipulating Files with cat

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 Section 13.10 Pipes and Pagers.

13.9.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 filename 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 13-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 13-6. Redirecting Output to a File

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

Do you notice anything different in Figure 13-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 then use cat to read the file. At the prompt, type:

cat sneakers.txt

CautionCaution
 

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
relax!

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 (you will find an example in Figure 13-7). Type the following:

cat sneakers.txt home.txt > saturday.txt

Figure 13-7. Joining Files and Redirecting Output

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

13.9.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. 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:

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!

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 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 13-8).

Figure 13-8. Stringing Commands and Comparing Files

13.9.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 13-9. Redirecting Standard Input

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