Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Reading a File

July 19, 1998

Okay, by now you are ready to do more than just find files and check out or modify their permissions. The next step, of course, is to actually look at the contents of the files you've discovered in the various directories, provided you have permission.

There are several ways to read files in the UNIX system besides using a word processor such as vi or emacs. (We won't cover those here because they require a tutorial all in themselves, but there are plenty of good books out there on the processors.)

The "cat" Utility

The cat utility provides a very simple way to read the contents of a file, preferably a small one. It is also pretty useful when piping the contents of a file to a command.

An example usage of each is shown below:

[cat Example]

As you can see, the cat command simply outputs the contents of the file. If the file is longer than the available screen space, then the file will simply print past the bottom of the screen. This can be a problem if you are interested in the initial contents of the file that has become invisible.

A solution to this problem is to use the "wc -l filename" command to find out how many lines are in the file and if it is greater than the screen height, use one of the other display utilities such as "pg" or "more" that we will discuss in just a bit.

Some UNIX systems also include the "tac" utility which behaves exactly like "cat" except that it displays the file in reverse order, from bottom to top.

As with most utilities, the "cat" utility has several useful options that are explored in the table below:

Option Explanation
-e newlines are preceded by a dollar sign if combined with -v
filelist A list of files that may be concatenated.
-s Messages about files being unreadable are censored
-t Displays tabs and form feeds if combined with the -v option.
-u Output is unbuffered
-v Displays control characters in the file

Give it a try, Go ahead and "cat" a file.

The "more" Utility

The "more" utility is much like cat except that it displays the contents of a file screen by screen. As each screen concludes, the utility will query the user for the next screen. Thus, more is much better at reading through larger files. Take a look at the following example:

[more Example]

As usual, the more utility comes with several options and arguments and follows the generic form of:

more [options] [+linenumber] [+/pattern] filelist

The options are explained in the following table:

Option Explanation
-c Does not clear the screen before displaying a new one but redraws line by line.
-d prompts the user to "hit space to continue".
-f Displays according to logical lines (wrapping lines are not counted) instead of screen lines.
-l Ignores ^L
-n Sets the number of lines per screen size to n.
-r Displays carriage returns as ^M
-s Drops multiple occurrences of blank lines.
-w When the entire file is displayed, more will wait for user-prompted quit rather than quitting itself.
+/pattern Begins at the first occurrence of the specified string.
+linenumber The line of the file to begin at.

It is also common to see more used at the end of a pipeline when the utilities involved may produce output greater than the length of a screen. For example, to restrict the listing of a full directory, you might use the more utility in a pipeline such as in the following example:

ls -l | more

By the way, Some UNIX systems also include the "less" utility that behaves exactly like "more" except that it displays the file in reverse order, from bottom to top.

Give it a try, go ahead and use "more" to read a file.

The "pg" Utility

The "pg" utility works just like the more command with some of its own options.

The "head" Utility

The "head" utility prints out the first number of specified lines of a given file and follows the basic syntax of:

head -lines filelist

The "tail" Utility

The "tail" utility prints out the last number of specified lines of a given file and follows the basic syntax of:

tail -lines filelist

Check out the following example:

[pc, head and tail Examples]

The "ls" Utility (And Hidden Files)
Introduction to UNIX for Web Developers | Table of Contents
Creating a File


Up to => Home / Internet / UNIX / Intro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers