-->

Previous | Table of Contents | Next

Page 56






/usr/local/netscape

|-- dynfonts

|-- java

|   `-- classes

|-- movemail-src

|-- nethelp

|   `-- netscape

|       |-- collabra

|       |-- composer

|       |-- confernc

|       |-- home

|       |-- messengr

|       |-- navigatr

|       |-- netcastr

|       |-- nscal

|       `-- shared

|-- plugins

`-- spell



17 directories

If you're interested in this utility (or other file utilities), you'll find a compressed archive containing its source, manual page, and ready-to-run binary at




http://sunsite.unc.edu/pub/Linux/utils/file

Listing and Combining Files with the cat Command

The cat (concatenate file) command is used to send the contents of files to your screen. This command may also be used to send files' contents into other files. Hour 6 covers terms such as standard input, standard output, and redirection, and this section shows you some basic uses for this command.

Although cat may be useful for reading short files, it is usually used to either combine,

create, overwrite, or append files. To use cat to look at a short file, you can enter


# cat test.txt

This text file was created by the cat command.

Cat could be the world's simplest text editor.

If you read this book, you'll learn how to use cat.

This is the last line of text in this file.

The cat command also has a number of options. If you'd like to see your file with line numbers, perhaps to note a specific phrase, you can use the -n option:


# cat -n test.txt

  1. This text file was created by the cat command.
  2. Cat could be the world's simplest text editor.
  3. If you read this book, you'll learn how to use cat.
  4. This is the last line of text in this file.

You can also use cat to look at several files at once, because cat accepts wildcards, for example:


# cat -n test*

Page 57

  1. This text file was created by the cat command.
  2. Cat could be the world's simplest text editor.
  3. If you read this book, you'll learn how to use cat.
  4. This is the last line of text in this file.
  5. This is the first line of test2.txt.
  6. This file was also created by cat.
  7. This is the last line of test2.txt.

As you can see, cat has also included a second file in its output, and has numbered each line of the output, not each file. Note that you could also see both files with


# cat test.txt test2.txt

The output will be exactly the same as if you'd used a wildcard. But looking at several files is only one way to use cat. You can also use the cat command with the redirection operator > to combine files. For example, if you would like to combine test.txt and test2.txt into a third file called test3.txt, you can use


# cat test* > test3.txt

You can check the result with


# ls -l test*

-rw-rw-r--   1 bball    bball         190 Nov 12 17:39 test.txt

-rw-rw-r--   1 bball    bball         108 Nov 12 17:45 test2.txt

-rw-rw-r--   1 bball    bball         298 Nov 12 18:00 test3.txt

But what if you want to combine test.txt and test2.txt without creating a larger, third file? In this case, you'd first decide whether you want the contents of test.txt to go into test2.txt, or the contents of test2.txt to go into test.txt. Then, using cat with the >> redirection operator, you might type


# cat test.txt >> test2.txt

This appends the contents of test.txt to the end of the test2.txt. To check the results, use cat again:


# cat test2.txt

This is the first line of test2.txt.

This file was also created by cat.

This is the last line of test2.txt.

This text file was created by the cat command.

Cat could be the world's simplest text editor.

If you read this book, you'll learn how to use cat.

This is the last line of text in this file.

Note that if you had entered the command


# cat -n test.txt >> test2.txt

The test2.txt file would look like


# cat test2.txt

Page 58


This is the first line of test2.txt.

This file was also created by cat.

This is the last line of test2.txt.

  1. This text file was created by the cat command.
  2. Cat could be the world's simplest text editor.
  3. If you read this book, you'll learn how to use cat.
  4. This is the last line of text in this file.

Finally, here's a trick you can use if you want to create a short text file without running a word processor or text editor. Because the cat command can read the standard input (more about this in Hour 6), you can make the cat command create a file and fill it with your keystrokes. Here's how:


# cat > myfile.txt

Now, enter some text:


# cat > myfile.txt

This is the cat word processor.

This is the end of the file.

Then, when you're done typing, press Ctrl+D to close the file. To see if this works, try


# ls -l myfile.txt

-rw-rw-r--   1 bball    bball          61 Nov 12 18:26 myfile.txt



# cat myfile.txt

This is the cat word processor.

This is the end of the file.

You should also know that the cat command will print out the contents of any file, and not just text files. Although cat may be useful to look at one or several short files, what do you do when you want to read longer text files? Read on, and you'll learn about pager commands, which will make life easier when reading longer files.

Reading Files with the more Command

The more command is one of a family of Linux commands called pagers. Pager commands let you browse through files, reading a screen or line at a time. This can be extremely helpful, especially if you read a lot of manual pages, because the man command will use a pager to display each page.

The more command is a traditional pager in the sense that it provides the basic features of early pagers. You can use more on the command line with


# more longfile.txt

If you need help, you can tap the H key to see a help screen. You can also run other commands from inside more by using the exclamation character (!). Reading through a text file is easy because you can advance one screenful (the current screen size) by tapping

Previous | Table of Contents | Next