A Linux Primer


Since most of the systems in the African Public Health system will be running Linux, we've included here a document designed to aid in forming a basic proficiency in using Linux (and therefore Unix). Note that this guide will not be sufficient to install Linux, only to use it. However, it should provide enough facility in the Linux operating system to do World Wide Web authoring.

Paths

The first concept that is essential to understand Linux is that of paths. A path is the means by which one indicates a file. There are two kinds of path: absolute and relative. An absolute path is one that indicates a file's location from the 'root directory.' An absolute path for a file in my directory might look like this:

/home/lester/xjewel.scores

This means that the file I'm interested in refering to (xjewel.scores) is in my directory (lester/), which is in the direcory containing user directories (home/) which is in the root directory (/). Alternatively, if I were in my directory (/home/lester), I could refer to xjewel.scores simply by typing it's name, thus:

xjewel.scores

This is an example of relative paths. But, from /home, I could refer to the same file by using this path:

/lester/xjewel.scores

There are two other 'directories' in each directory. One is . , which indicates the current directory, so ./xjewel.scores is the same as xjewel.scores, and, more importantly, .. refers to the directory in which the current directory is contained, called the parent directory. That way, if my current directory was /home/lester/whitesocks, I could call the example file ../xjewel.scores.

While all this may seem somewhat involved and nebulous, paths are central both to using Linux and to programming in HTML. For instance, why don't we start with a bunch of commands that use paths as arguements.

Path Commands

cd - Change directories
cd is one of those commands that's used with alarming frequency. What cd does is to change one's current directory to the directory indicated by the path one gives to cd. For instance: cd will take you to your home directory. cd ~[username] will take you to the directory of the user [username]. cd .. will move one up to the parent directory of the current one. cd / will change the current directory to the root directory. cd . has no effect. The idea expressed is 'change from this directory to this directory.'
mkdir dirname
mkdir is short for make directory. (If you were wondering how these directories came from, mkdir is the answer.) Typing mkdir dirname adds dirname to the current directory. If one calls mkdir with the -p option, thus: mkdir -p onedir/twodir/dirname mkdir will make all the non-existant directories as well. If onedir existed, but twodir didn't, mkdir would create twodir in onedir, and then dirname in twodir.
pwd
pwd is an acronym for 'present working directory,' and returns the path of the directory one is in at the moment, viz: whiskey:lester:7:pwd /usr/people/lester
cp source dest
cp isshort for copy. The cp command works two different ways depending on what source and dest are.
  1. If source and dest are both paths that indicate a file, cp copies the contents of source into dest. Be careful, because the result will be that you have two copies of source, and the information in dest is gone.
  2. If source is a file, and dest is a path to a non-existant file, cp will make a copy of a file at dest.
  3. Ifsource is a list of files and dest is a directory, cp will make copies of the files with the same names, but in the dest directory.
mv source dest
mv is short for move. Like cp, mv works differently depending on the arguments passed to it. Thus:
  1. If source and dest are paths to files, the source is moved into dest, erasing dest and replacing it with source. Note that source will no longer exist under that name, and that the contents of dest will be replaced with those of source.
  2. If source is a file, and dest is a path to a non-existant file, mv will change the name of source to dest (changing it's path if neccesary.) Think of this as moving the file between file folders.
  3. If source is a list of files, and dest is a directory, the files in source will be moved into dest, using the same filenames.
ls - list contents of a directory
ls is another command that is used with frightening regularity. Again, ls works differently under different conditions.
  1. If typed by itself ('ls'), ls will display a list of the contents of the current directory.
  2. If entered with the -l option ('ls -l'), ls displays a list of all files, with additional information about each, like owner's name, the permissions of the file, modification date, what kind of file it is, and its size. (Some of these ideas will be explained later, others aren't terribly important, and others are assumed to be evident.)
  3. The -a option ('ls -a') gets ls to display all the contents of a directory, including any file that begins with a '.' (including .login, . and ...) Note that ls -la displays a long view (i.e. with lots of semi-relavent information) of all the contents of a directory.
  4. If you give ls a directory (ls /home/lester,) it will list the contents of that directory. (Adding the -a and -l options (or -la) will do appropriate things to the listing.)
  5. Giving ls a wildcard expression (one that contains * or ?), it will return a list of all files that match that expression in the current directory. Further, if the expression is a directory, all the contents of that directory that match the expression will be listed. Also, any directories that match the expression will have their contents listed, with the name of the directory as a header.

The discussion of ls brings up several topics of confusion which will now be expounded upon:

Permissions, Owners and Wildcards

In Unix, and in Linux, files have permissions. Permissions indicate who gets to use the file, and what they can do with it. There are nine permissions that can either be granted or removed: The owner's permission to read the file, write to it, or execute it, the owner's group's (a concept seldom used) permissions to read, write or execute, and the global permissions do the same. (Global permissions apply to everyone, except the owner and his group.)

Permissions are usually written one of two ways. The first way is used by ls to describe permissions. It consists of ten characters, the first indicates the type of file (- for regular files, d for directories, and l for symbolic links (don't worry too much about links)), the next three indicate the user's permissions, the next three are group, and the next three are global permissions. In this context, permissions are indicated either by a - or a letter, an r in the read possision, a w in the write possision, and an x in the execute possision. Thus an example permission string: -r-xrw--x.

The second way of indicating permissions is the one used to send a permission set to various commands, notably chmod (which we'll get to soon). This method uses three digits, where the first digit is the owner's permissions, the second is his groups's, and the last is the world's. Two determine what a digit is, add the values of the permissions: 4 is read, 2 is write, and 1 is execute. Therefore, the permission of the previous example in this form is 561. This form is used whenever a permission set is sent to a command.

Another important concept are those of wildcards. Wildcards save time, effort, and allow one to limit the breadth of one's search. Essentially, a wildcard expression is a regular path, but with *'s and ?'s in place of letters. A command to which a wildcard expression is passed will be applied to all paths which match the expression. An * matches any number of characters, with any character in it's place, while a ? matches any one character. pl* matches ply, plague, place, plateau, etc. pl? matches pla, plb, plc etc.

Equally important are regular expressions. Regular expressions (or regexs) are like wildcards, but more powerful. There are three basic elements of regexs: ., *, and []. '.' allows for matching of any number of the character that follows it. Thus foo.b matches foo, foob, foobb, foobbb, etc. * matches any single character. foob*r matches foobar, foobbr, foobcr, etc. [] contain ranges to be matched. For example [a-z] matches any lower case letter, [a-z, A-Z] matches any letter, [0-9] matches any number. foo[0-9] matches foo0, foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, and foo9. These elements can be combined: .* will match any number of any character, .[A-Z] will match any number of upper case letters.

Permissions Commands

chmod permission file -change permissions
chmod is the means by which the permissions of files changes. The permission argument is a numerical representation permission, and the file is the file whose permissions you want changed.
chown user file
This command changes the owner of a file. Usually the person who created the file is it's owner, but sometimes you have to make a file for someone else, and then 'give' it to them. This is accomplised via chown.

Generally Really Useful Commands

man commandname
man is short for manual. It produces (usually) a helpful, if somewhat cryptic descriptions of what various commands do. man accepts the name of the command you need information about as its argument. If one's question is of the type "What command does ... ", there are two options: 1) Enter a realted command. man pages often have See Also sections at the bottom, or 2) man -k word, where the word realtes to what you want done. The result will be a list of pages that contain that word, from which you can select likely candidates. man is exceptionally useful for increasing one's facility with Linux. As an exercise, use man to get more information about the command is this tutorial.
passwd
Typing passwd will begin a brief dialog that will allow you to change your passwd, something that should be done with relative frequency. Keep in mind that a password should not be any actual English word, and that the use of non-letter characters (i.e. numbers and puntuation) is encouraged, as this will make the password more difficult to guess. Contrarywise, you need to be able to remember your password, because: a) you shouldn't write it down and b) if you forget it, you'll need to get the sysadmin to reset your password.



Click here to go back to the top.