|
Chapter 25 Showing What's in a File
|
|
The
cat
command may well be the first command new users
hear about, if only because of its odd name.
cat
stands for con
cat
enate or, as some would say,
catenate. Both words mean the same thing: to connect
in a series. The
cat
command takes its filename arguments,
and strings their contents together. Essentially,
cat
takes
its input and spits it out again.
cat
has many uses, but the four most basic applications are
described in the following list.
In many ways, they don't illustrate
cat
so
much as they illustrate the shell's
output redirection (
13.1
)
mechanism.
-
First form:
%
cat
file
%
cat
file1 file2 file...
Use this form to display one or more files on the screen.
The output doesn't pause when the screen is full. As a result,
if your files are more than one screenful long, the output
will whiz by without giving you a chance to read it. To read
output by screenfuls, use the
more
(
25.3
)
command or some other pager, like
less
(
25.4
)
.
[1]
-
Second form:
%
cat
files
>
new_file
Use this form when you want to combine several smaller files into
one large file. Be sure the destination file does not already exist;
otherwise, it will be replaced by the new contents (effectively destroying the
original). For example, the command:
%
cat chap1 chap2 chap3 > book
creates a new file,
book
, composed of three files,
one after the other. The three component files still exist
as
chap1
,
chap2
, and
chap3
.
-
Third form:
%
cat
file
>>
existing_file
%
cat
files
>>
existing_file
Use this form to add one or more files to the end of an existing
file. For example:
%
cat note1 note2 > note_list
%
cat note3 >> note_list
-
Fourth form:
%
cat >
newfile
Use this form as a quick-and-dirty way to create a new file.
This is useful when you aren't yet familiar with any of the
standard text editors. With this command, everything
you type at the keyboard goes into the new file.
(You won't be able to back up to a previous line.)
To finish your input, enter CTRL-d on a line by itself.
Well, that was just in case there are some beginners on board.
Articles
13.13
,
25.7
,
25.10
,
and
25.21
give some more useful tips about
cat
options.
|
|