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.

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.

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.”

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.

Example of a directory hierarchy

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.

Visual representation of the path /users/jen/pers

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.

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

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.

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

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.

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

Figure 4-6. Visual representation of the path .. /.. /richard/work, relative to the pers directory

Basic Unix Commands

The general format for Unix commands is:

                  command 
                  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.

Get Web Design in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.