eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to UNIX for Web Developers
Reading a File  
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:

[Spacer]

[cat Example]

[Spacer]

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 passed 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 is 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 pipeling 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]

Previous | Next | Table of Contents