[lug] Newbie Column #6 - Viewing File Contents
Wayde Allen
wallen at boulder.nist.gov
Tue Feb 8 11:50:06 MST 2000
In my last post, we took a look at the overall organization of the UNIX
file system. In other words, I tried to describe what directories you
could expect to find and the kinds of files that these directories should
contain. What I didn't explain is how you could open a file to read its
contents.
In UNIX, as always, there are many possible ways to accomplish this. You
can use the "cat" command, for instance, or one of several paging programs
such as "more". Additionally, you can use any one of a number of text
processors such as "emacs" or "vi". For now, let's look only at the first
two possibilities I've mentioned, namely cat and more.
The cat command is used to concatenate and display the contents of files.
To see how this works try listing out the system's hosts database file by
typing:
cat /etc/hosts
This should print the entire contents of the host database to your screen.
If you have many entries in your host file you will notice that the entire
file will scroll by faster than you can read it though. For this reason,
it is usually more useful to use a paging program to display the contents
of a file one page at a time. One such program is called more. Try it by
typing:
more /etc/hosts
You should now see the first page of the host file printed to your screen.
Hitting the space bar will take you to the next page. Pressing the "q" key
will cause the more program to exit.
This should give you enough information to be able to see what is
contained in any text-based file you have permission to view. You can try
to view binary files too, but these will simply display a screenful of
seemingly random garbage.
As a final note, there is quite a bit of functionality embedded in these
commands that I'm not going to talk about. You can read the details for
yourself by accessing the online man pages. To see what the more command
can do type:
man more
Also, these commands allow only single direction browsing of the contents
of a file. If you want to move both forward and backward through a file,
you may want to use something like "less" or "vi", but that is a
discussion for another day.
- Wayde
More information about the LUG
mailing list