OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

cat – Concatenate Files

The cat command reads one or more files and copies them to standard output like so:


cat [file...]

cat [file...]


In most cases, you can think of cat as being analogous to the TYPE command in DOS. You can use it to display files without paging, for example:



[me@linuxbox ~]$ cat ls-output.txt

[me@linuxbox ~]$ cat ls-output.txt


will display the contents of the file ls-output.txt. cat is often used to display short text files. Since cat can accept more than one file as an argument, it can also be used to join files together. Say we have downloaded a large file that has been split into multiple parts (multimedia files are often split this way on Usenet), and we want to join them back together. If the files were named:

movie.mpeg.001 movie.mpeg.002 ... movie.mpeg.099

we could join them back together with this command:



cat movie.mpeg.0* > movie.mpeg

cat movie.mpeg.0* > movie.mpeg


Since wildcards always expand in sorted order, the arguments will be arranged in the cor- rect order.

This is all well and good, but what does this have to do with standard input? Nothing yet, but let's try something else. What happens if we enter “cat” with no arguments:



[me@linuxbox ~]$ cat

[me@linuxbox ~]$ cat


Nothing happens, it just sits there like it's hung. It may seem that way, but it's really doing exactly what it's supposed to.

If cat is not given any arguments, it reads from standard input and since standard input is, by default, attached to the keyboard, it's waiting for us to type something! Try adding the following text and pressing Enter:

Redirecting Standard Input


[me@linuxbox ~]$ cat

The quick brown fox jumped over the lazy dog.

[me@linuxbox ~]$ cat

The quick brown fox jumped over the lazy dog.


Next, type a Ctrl-d (i.e., hold down the Ctrl key and press “d”) to tell cat that it has reached end of file (EOF) on standard input:



[me@linuxbox ~]$ cat

The quick brown fox jumped over the lazy dog.

The quick brown fox jumped over the lazy dog.

[me@linuxbox ~]$ cat

The quick brown fox jumped over the lazy dog.

The quick brown fox jumped over the lazy dog.


In the absence of filename arguments, cat copies standard input to standard output, so we see our line of text repeated. We can use this behavior to create short text files. Let's say that we wanted to create a file called “lazy_dog.txt” containing the text in our exam- ple. We would do this:



[me@linuxbox ~]$ cat > lazy_dog.txt

The quick brown fox jumped over the lazy dog.

[me@linuxbox ~]$ cat > lazy_dog.txt

The quick brown fox jumped over the lazy dog.


Type the command followed by the text we want in to place in the file. Remember to type Ctrl-d at the end. Using the command line, we have implemented the world's dumbest word processor! To see our results, we can use cat to copy the file to stdout again:


[me@linuxbox ~]$ cat lazy_dog.txt

The quick brown fox jumped over the lazy dog.

[me@linuxbox ~]$ cat lazy_dog.txt

The quick brown fox jumped over the lazy dog.


Now that we know how cat accepts standard input, in addition to filename arguments, let's try redirecting standard input:



[me@linuxbox ~]$ cat < lazy_dog.txt

The quick brown fox jumped over the lazy dog.

[me@linuxbox ~]$ cat < lazy_dog.txt

The quick brown fox jumped over the lazy dog.


Using the “<” redirection operator, we change the source of standard input from the key- board to the file lazy_dog.txt. We see that the result is the same as passing a single filename argument. This is not particularly useful compared to passing a filename argu- ment, but it serves to demonstrate using a file as a source of standard input. Other com- mands make better use of standard input, as we shall soon see.


Before we move on, check out the man page for cat, as it has several interesting options.


Top OS Cloud Computing at OnWorks: