• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

4.2. Introduction to Unix

If you work with the Web long enough, chances are you'll run into the need to communicate with a Unix machine. On Macs and PCs, most functions can be performed using tools with graphical interfaces; however, at times there is no substitute for a good old-fashioned telnet session. Besides, a little Unix never hurt anyone.

Telnet is a "terminal emulation" protocol that allows you to log in to other computer systems on a network (such as the Internet). It also refers to any application used for communicating with the telnet protocol. A telnet program will give you a character-based terminal window on another system from which you can enter simple command-line instructions. Figure 4.1 shows a typical telnet session.

Figure 4.1. Typical telnet session

This section provides a very brief introduction to basic terminology and commands that will enable you to manipulate files and directories on a Unix machine. If you are interested in learning more, read Learning the Unix Operating System and Unix in a Nutshell, both published by O'Reilly & Associates, Inc.

4.2.1. Logging In

When you open a connection to a remote server with your telnet program, the first thing you see is a prompt to log in to the system. Before you can access the server, the system administrator has to set up a Unix account for you. You will be given a name and password you can enter at the login: and password: prompts respectively.

When you log in successfully, you will get a Unix system prompt, either a $ or a % (or sometimes #) depending on the flavor of Unix the server is running, which tells you that the system is "ready." At this point, you are using a program called the shell, which interprets the commands you type and invokes the programs you ask for. You will automatically be placed in your home directory.

To end the telnet session, simply type "logout" or "exit."

4.2.2. Directory Structures

Because the Web was spawned from the Unix environment, it follows many of the same conventions. For instance, writing correct URL pathnames in hyperlinks relies on an understanding of how directory structures work on the Unix platform.

Directories ("places" to store files) are organized into a hierarchical structure that fans out like an upside-down tree. The top-most directory is known as the root and is written as a forward slash ( / ). The root can contain several directories, each of which can contain subdirectories; each of these can contain more subdirectories, and so on. A subdirectory is said to be the "child" of the directory that holds it (its "parent"). Figure 4.2 shows a system with five directories under the root. The directory users has two subdirectories, jen and richard. Within jen are two more subdirectories, work and pers, and within pers is the file art.html.

Figure 4.2. Example of a directory hierarchy

A pathname is the notation used to point to a particular file or directory; it tells you the path of directories you must travel to get to where you want to go. There are two types of pathnames: absolute and relative. Absolute pathnames

An absolute pathname always starts from the root directory, which is indicated by a slash ( / ). So, for example, the pathname for pers is /users/jen/pers, as shown in Figure 4.3.The first slash indicates that we are starting at the root, and is necessary for indicating that a pathname is absolute.

Figure 4.3. Visual representation of the path /users/jen/pers Relative pathnames

A relative pathname points to a file or directory relative to your current working directory. When building a web site on a single server, relative pathnames are commonly used within URLs to refer to files in other directories on the server.

Unless you specify an absolute name (starting with a slash) the shell assumes you are using a relative pathname. Starting in your current location (your working directory), you can trace your way up and down the directory hierarchy. This is best explained with an example.

If I am currently working in the directory jen and I want to refer to the file art.html, the relative pathname would be pers/art.html because the file art.html is in the directory pers, which is in the current directory, jen. This is illustrated in Figure 4.4.

Figure 4.4. Visual representation of the path pers/art.html relative to the jen directory

Going back up the hierarchy is a bit trickier. You go up a level by using the shorthand .. for the parent directory. Again, let's use an example based on Figure 4.2.

If I am currently in the jen directory, and I want to refer to the directory richard/work, the pathname would be .. /richard/work. The two dots at the beginning of the path takes us back up one level to the to the users directory, and from there we find the directory called richard and the subdirectory called work, as shown in Figure 4.5.

Figure 4.5. Visual representation of the path ../richard/work, relative to the jen directory

If I am currently in my pers directory and I want to refer to Richard's work directory, I need to go up two levels, so the pathname would be .. /.. /richard/work, as shown in Figure 4.6.

Note that the absolute path /users/richard /work accomplishes the same thing. The decision whether to use an absolute versus a relative pathname generally comes down to which is easiest from where you are.

Figure 4.6. Visual representation of the path .. /.. /richard/work, relative to the pers directory

4.2.3. Basic Unix Commands

The general format for Unix commands is:

						option(s) filename(s) 

The command in bold must be typed in exactly as shown and the values in italics stand for variables that you supply. Some general guidelines for Unix syntax are:

  • Commands are entered in lowercase.

  • Options modify the way in which a command works. They are often single letters prefixed with a minus (-) sign and set off by any number of spaces or tabs. The options listed in this chapter typically can be indicated in one command line by setting them off individually or combining them after a single minus sign. Note: This rule may vary depending on the type of Unix system you are using. Ask your server administrator for help if you run into problems.

  • The argument filename is the name of a file you want to manipulate in some way.

  • Spaces between commands, options, and filenames are important; e.g., "ls -al" works, "ls-al" doesn't.

  • The asterisk symbol (*) is a wildcard character that can be used to substitute for any string of characters in a file or directory name. For instance, a search for *.html will retrieve all documents ending in .html.

The following is a brief list of Unix commands that are useful for navigating through directories and managing files.



This changes your working directory to the specified directory. cd alone (without arguments) will take you to your home directory.


[old ] [new ]

This makes a copy of a file where old is a pathname to the original file and new is the name you want to give the copy of the file. (This is similar to the "save as" function in an application.) The following sample code makes a copy of the file art.html and names the copy art2.html.

     % cp art.html art2.html

cp  [file ] [directory ]

This puts a copy of the original file into an existing directory. The following sample code makes a copy of art.html and puts it in Richard's work directory:

% cp art.html /user/richard/work


[option(s)] [name(s)]

This gives you a listing of the files and subdirectories (denoted with a slash) in your working directory. Adding a directory name after the options gives you the list of files within the named directory.



shows all the files within the directory, including normally hidden files.


lists files by creation/modification time.


displays long format listing (includes permissions, owner, size, modification time, etc.)


marks directories by adding a slash at the end of the name


displays the list in rows going across the screen.


A simple listing of names in the current directory:

% ls

List directory contents across the screen with directories indicated with a slash ( / ):

% ls -px
work/     pers/     ch01     ch02     ch03



This command creates a new directory in the working directory, where dirname is the name of the new directory. You must have write permission in the parent directory in order to create a directory.

% mkdir nutshell



This displays the contents of a file (or files), one screenful at a time. After each screen is displayed, press RETURN to display the next line or press the spacebar to display the next screenful. Press ? for help with additional commands. Press q to quit.


[source] [target]

This basic command moves files and directories around on the system or renames them. Table 4.1 shows the results of various mv operations.

Table 4.1. Results of Various mv Operations
Source Target Result
File name Rename file as name
File Existing file Overwrite existing file with the source file
Directory name Rename directory as name
Directory Existing directory Move directory to be a subdirectory of the existing directory
One or more files Existing directory Move files to directory


Rename a file:

% mv art.html artists.html

Move a file from one directory to another:

% mv art.html ../richard/work


[>options] [filename]

This displays the contents of the file on the screen, one page at a time. After each screen is displayed, you are prompted to display the next page by pressing the RETURN key. Press h (or ?) for help with additional commands. Press q to quit.

Useful pg Commands


Display one more line


Move to page n

+ or -n

Move forward or backward by n pages


[user ]

This creates or changes a password associated with a user name. Only the owner or a privileged user can change a password.



This will tell you what directory you are currently in (your working directory).

% pwd



rm deletes files (called by name). Be very careful when removing files because it cannot be undone.



This allows you to remove a directory (dirname) as long as it doesn't contain any files.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint