The cat Command


The cat Command

When learning Unix, it's common to first learn the cat command. An easy abbreviation to remember if you are pet friendly, this command will concatenate the elements you specify into one package. The easiest way to look at files is to use cat, the concatenate command.

Concatenated, What's That? Most words in the computer industry can be overwhelming to learn because most of what you hear is acronym based, defined in a complicated way, and so on. In this case, the word concatenate is nothing more than the theory of combining elements for efficiency sake.

In Unix, we use concatenates because Unix has the capability to string or place together two or more files to create a single file. Data elements (strings) and contents (files) can be combined together into a new element or file. Think of concatenation as the ability to combine things together for ease of use.

The cat command is a byproduct of this definition. The cat command will allow you to concatenate all data specified together into one package.


Now that you are comfortable with the definition and what cat does, let's look at the operation of the cat command. Now, there are a couple of ways to use the cat command. We will look at it in the light of reading files and will touch on it in Lesson 12, "Input and Output," later in the book. Until then, let's just focus on using cat to read files on your system.

To display a file, just supply cat with the file(s) you want to see. If you want to see how powerful Unix is, cat a file like the b1.jpg (JPEG image file). Unix will look inside it and report back to you. Unix will report in a language you can't read or understand, but this shows you that Unix is literal in everything you ask it to do. Unix will do it or crash and die trying!

One example that I am sure you will be able to find on your system is your hosts file located in your /etc directory. I have left out most of the output as the hosts file can be quite large, but this is exactly one of the main benefits of the cat command. I didn't need to do anything but specify the cat command and either the directory (if I am not in it) and the file.

 >cat hosts # # hosts     This file describes a number of hostname-to-address____________________ #        mappings for the TCP/IP subsystem. It is ______________________________ #        mostly used at boot time, when no name servers are running.____________ #        On small systems, this file can be used instead of a Named name server. # Syntax: # IP-Address Full-Qualified-Hostname Short-Hostname # 127.0.0.1  localhost (Output removed) 

In this example, while in our /etc directory, we used the cat command to view the hosts file for its current entries. We had only one entry because I removed the long stream of output. This can be seen with other commands that we will learn about shortly in this lesson. With cat, you can easily and quickly see the contents of a file.

Another item of importance is that you must know what you can and can't use cat with. The cat command will return an error message to you if you try to cat a directory. The error message will tell you that you are trying to cat a directory and that this is not allowed. Be aware that cat is for files, not for directories. You can also specify multiple filenames. However, when you receive the results back from your query, you will be given a longer list of the combined concatenates. Do not specify any directories when specifying multiple filenames with the cat command or your query may fail. Also make sure that you test out your wildcards and attempt to build them into your cat command queries. For instance, if you want to search for all the files in your /etc directory that may be hosts files, run a query on it, narrow down your search criteria, and cat the file you want to see. You can also cat multiple files.

You can cat all the files simultaneously, but the query results would be so large that your results would scroll off the screen too quickly for you to even digest the first sentence shown. Having said that, let's still look at a way to view the entire result so you don't miss anything. First find what you want to look at:

 >find host* hosts hosts.conf hosts.allow hosts.deny 

Since my query specified looking for host*, everything that starts with the word host will be shown with whatever comes after it because of the use of the wildcard. You can then cat your files to see which one is relevant for use.

 >cat host* 

This will show you more output than you can possibly even read. We will now move on to managing what you view in a better way, so you can actually read the data! You can use pagers to slow down the output to make it readable.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net