The ACM Introductory Linux Tutorial, or Clueless at the Prompt

The purpose of this tutorial is to introduce you to the basic Linux command-line usage on the UW CSE department Linux machines. Before beginning this tutorial you should be able to use TeraTerm or telnet to connect to one of the department's Linux machines (i.e, tahiti, fiji, ceilon, or sumatra) , and be able to log into the Linux machine with your username and password. If you are a new student, your password should have been issued to you during the new students orientation.

This page uses JavaScript and simple style sheets to aid the presentation, so it is best viewed with a graphical browser which supports stylesheets, such as Netscape, or Internet Explorer. You should be able to muddle along even if you are using a non-graphical web browser, such as lynx, though.

After completing this tutorial you should know:

While some of this tutorial is geared specifically towards Linux on the CSE instructional machines, much of the material here should be applicable to Unix and Unix-like systems, even though no effort is currently made to designate UW-CSE specific vs generic Unix/Unix-like material.

Logging in. Your home directory. The command prompt.

Go ahead and log into one of the department's Linux machines (tahiti, sumatra, ceylon, or fiji). When you log into a Linux machine, you are normally placed in your home directory. Unlike Windows, where it's not uncommon for users to roam all over the disk in the course of their everyday work, Linux users usually do all their work below their home directory. Your Linux home directory is not the same as your Windows NT/2000 Profiles directory/folder (aka your "Z:" drive), even though it is possible to map your home directory from Windows NT/2000.

You will also see the command prompt, indicating that the system is ready to accept the commands you type.

Seeing what files are in a directory.

Let's use the ls command to see what's in your home directory. Go ahead and try it now:
tahiti% ls
You may not see very many things listed (in fact, you may see nothing at all listed) -- that's OK.

Creating and removing directories

Linux organizes files into directories. The concept of "directory" under Linux is exactly equivalent to the concept of "folder" under Windows. You may remember that DOS used the term "directory" (in exactly the same sense that Linux uses it), but starting with Windows95, the Microsoft marketing people decided they could make more money by stealing the term "folders" from the Macintosh people.

Now you could store all your work right below your home directory, but you'll probably be much better off if you organize the things you work on by creating additional directories below your home directory. The command to do so is mkdir Go ahead and make three directories like so:
mkdir tutorial www junk
You can get rid of the empty directories you no longer want with the rmdir command. Let's use it to remove the "junk" directory we had created above:
rmdir junk
The rmdir command will fail unless the directory you are trying to remove is empty (does not contain any files).

The paths we follow

Now let's take a moment to talk about paths (no, not enlightenment, the file paths). In Linux, you address the files by their paths (and files address you as... eh, never mind). Anyways, to work on a file, you have to know a path to it. There are two kinds of paths: absolute and relative. You'll know an absolute path when you see one because it always starts with a "/", which stands for the root of the file hierarchy.

To find the ls command, we start at the top of the file hierarchy, descend into the directory named bin, and viola -- we're looking at the ls command. So the absolute path to the command is /bin/ls. To find the mkdir command, we start at the root of the file hierarchy (/), descend into the usr directory, and then descend into the bin directory below usr, so the path to the mkdir command is /usr/bin/mkdir.

So far, we have been dealing with absolute paths -- all of them start from the top of the file hierarchy. Relative paths, on the other hand, start from the "current directory". When you first log in, your Linux home directory will be your current directory. You don't need to use any special characters to designate the current directory in the path -- any path whose first element is not a slash(/) is assumed to be a relative path. On the other hand, if you do need to explicitly address the current directory, you can use a single dot(.).

Going "down" the directory hierarchy is easy -- you just list the directories you have to go through separated by slashes, for example the path to the index.html file in the www directory below your home directory is www/index.html (or, equivalently, ./www/index.html). So that was easy, but how do you go up? Two dots (..) stand for "the parent of this directory". For example, if your current directory were the tutorial directory (below your home), and wanted to address the file index.html below the www directory, you could do it like so: ../www/index.html.

Changing the current directory

That last example may have left you wondering -- it assumes your current directory is the tutorial directory(below your home), but if your current directory is set to your home directory whenever you log in, how would you end up with the tutorial directory being current to start with? The cd command is used to change the current directory. The complement command, pwd tells you what your current directory is. Let's try using it:


tahiti% cd   # make sure we are in the home directory
tahiti% cd www
tahiti% pwd
/u4/evgenyr/www
tahiti% cd ../.login  # fails
../.login: Not a directory.
tahiti% pwd
/u4/evgenyr/www
tahiti% cd ../tutorial
tahiti% pwd
/u4/evgenyr/tutorial
tahiti% cd  # back to home directory
tahiti% pwd
/u4/evgenyr

Things we can learn from a files listing

In the next couple of sections we'll take a look at the "long" listing of your directory. Go ahead and produce a long listing of your home directory now:
ls -la

Users and groups


total 8
drwxr-xr-x   60 root     root         1024 Oct  2 13:57 ../
drwxr-xr-x    3 evgenyr  ugrad_cs     1024 Oct 17 11:48 ../
-rw-r--r--    1 evgenyr  ugrad_cs     1178 Oct 17 11:49 .cshrc
-rw-r--r--    1 evgenyr  ugrad_cs      649 Oct 17 11:52 .forward
-rw-r--r--    1 evgenyr  ugrad_cs      489 Oct 17 11:49 .login
drwxr-xr-x    2 evgenyr  ugrad_cs     1024 Oct 17 11:53 tutorial/
drwxr-xr-x    2 evgenyr  ugrad_cs     1024 Oct 17 11:49 www/

Each user on a Linux system has a user name (aka login name). The user name is unique on a given system. Individual user names are collected into groups. All undergraduate students at the UW CSE department belong to either the ugrad_cs or ugrad_ce group, depending on whether they are CS or CE major. Additional Linux groups may be created for classes that have team projects. Groups that are created for classes will normally be named something like cse326a, cse326b, etc. (Actually, Linux deals with numeric user IDs and group IDs, but since each user name maps to exactly one numeric user ID, and each group name maps to exactly one numeric group ID, we can continue to use symbolic user/group names rather than having to bother with numeric user/group IDs).

Each file on a Linux system is owned by exactly one user and exactly one group. In the listing above, the user name is highlighted in red and group name is highlighted in green. You can use the chown command to change the user and/or group who owns the file.

Controlling access to files -- permissions.


total 8
drwxr-xr-x   60 root     root         1024 Oct  2 13:57 ../
drwxr-xr-x    3 evgenyr  ugrad_cs     1024 Oct 17 11:48 ../
-rw-r--r--    1 evgenyr  ugrad_cs     1178 Oct 17 11:49 .cshrc
-rw-r--r--    1 evgenyr  ugrad_cs      649 Oct 17 11:52 .forward
-rw-r--r--    1 evgenyr  ugrad_cs      489 Oct 17 11:49 .login
drwxr-xr-x    2 evgenyr  ugrad_cs     1024 Oct 17 11:53 tutorial/
drwxr-xr-x    2 evgenyr  ugrad_cs     1024 Oct 17 11:49 www/

permissions control who can do what to a file or directory. Each file or directory under Linux has three sets of permissions associated with it. The first set, highlighted in red on the example above, tells what the user who owns the file is allowed to do to it. The second set, highlighted in green in the example above tells what the members of the group who owns the file are allowed to do to the file. The third set, highlighted in yellow in the examples above, tells what the other users, i.e., those who neither own the file nor are member of the group that owns the file can do to the file. For example, let's say Alice (with the username of alice) is majoring in CS (and thus belongs to the ugrad_cs group), Bob (with the username of bob) majors in CE (and thus belongs to the ugrad_ce group), and Charlie (with the username of charlie) majors in CS as well (and thus belongs to the ugrad_cs group, together with alice). Now here is an (imaginary) listing of two of the files in Alice's directory:


-rw-r-----    1 alice  ugrad_cs      489 Oct 17 11:49 paper1
drwxr-x--x    2 alice  ugrad_cs     1024 Oct 17 11:53 cse378/

The question is, who can do what? Alice can read as well as change the contents of paper1, while Charlie can only read it (because he is not alice, but he is in the ugrad_cs group), while Bob can neither read nor modify paper1. Alice can list the contents of the cse378 directory, create and delete files in it, and access the files in it. Charlie can list the contents of the cse378 directory and access the files in it, while Bob can access the files in the directory (if he happens to know the file's name), but cannot list the directory.

Changing permissions -- chmod

You use the chmod command to change permissions on files and directories. Here are a couple more examples of chmod usage:

Disallow access to file by anyone but the user:


tahiti% chmod go-rwx file.

Make sure the members of the group owning the file can read and change its contents:
tahiti% chmod g+rw file

Let's make it so no one can look at your .login file:
tahiti% chmod go-rwx .login

Along with the "symbolic" form of the chmod command, explained above, there is also a "numeric" form. The biggest difference from symbolic form is that, with numeric form, you can only set permissions and only set all permissions at once. Numeric permissions are expressed by a 4-digit octal number (12 bits), whose most significant digit is usually 0, or omitted, like this:
special user group other
set-UIDset-GIDsticky-bit
421
readwriteexecute
421
readwriteexecute
421
readwriteexecute
421
You produce the numeric permissions by simply summing the values for the permissions you want to be present, for each set. For example, to produce a -rwxr-x--x permissions you would sum like this:
field bitsresult
special: 0 0
user: 4(r) + 2(w) + 1(x) 7
group: 4(r) + 1(x) 5
other: 1(x) 1
,giving you the numeric permissions of 0751, or, omitting the leading 0, simply 751. You'd set the permissions like so:
tahiti% chmod 751 tutorial

Files, and directories, and named pipes, oh my...


-rw-r-----    1 alice  ugrad_cs      489 Oct 17 11:49 paper1
drwxr-x--x    2 alice  ugrad_cs     1024 Oct 17 11:53 cse378/

Now you may be wondering what the highlighted letters in the very first column of the file listing stand for. The answer is that a dash(-) means "ordinary file" while a "d" means "directory", and that's all you are likely to see, at least until you become quite familiar with Linux. In case you are curious, I provided a more complete listing.

Copying files

You use the cp command to copy files. Not much to say about that, really, other than you have to be careful because cp will overwrite existing file(s) without prompting unless you give it the -i option. Also, for copying to be successful the you must have permission to create files in the destination directory and to modify the contents of the file you are overwriting.

You use the mv command to move file between directories or to rename a file. Again, mv will overwrite the destination without prompting unless you give it the -i option, and you have to have the permission to create files in the destination directory and/or to modify the contents of the file you are overwriting.

You use the rm command to remove a file. Removing a file is, for all intents and purposes, an irreversible act under Linux, so be careful. If given the -i option rm will prompt you for each file it's about to remove.

Printing

As we all know, the paperless office is just around the corner. That must be the reason we use ever increasing quantities of printer paper. So, at the risk of being obsoleted with the advent of paperless computing, here is some information about printing in the interium.

Under Linux, you'll probably want to use the enscript command to print plain text files (including your programs's text) and lpr command to print PostScript files. Both enscript and lpr accept a -P option to specify the printer you want to print to. In the UW CSE Department, printers are usually named psROOM_NUMBER, so if you are in Sieg 329, you'd normally want to print to the printer named ps329, if you are in Sieg 232, you'd normally want to print to the printer named ps232, etc.

Listing and stopping processes

While there are many possible definitions of what a process is, the operational definition I'll use here is that a process is a running program (as opposed to the file on disk that contains the program's executable). Since Linux is a multi-tasking system, more than one process can be running at the same time. You can use the ps command to see what processes are running.

Occasionally you may want to stop a process you have running. If you know the process's process ID (PID), you can use the kill command to stop it. If you only know the name of the program that runs in the process, you may try using the killall command to stop the process, even though you might be better off obtaining the PID first (e.g. with "ps -C").

Getting more information - man and info

In a Unix tradition, documentation has been distributed in form of manual pages, or man pages for short. Different sections of the manual traditionally held different kinds of pages. Thus, section 1 described user commands, section 3 described library functions, section 2 described system calls (low-level functions, calls directly into the kernel), section 5 described common configuration file formats, section 6 described games, and section 8 described system administrator's commands. The man command is used to read the manual pages online, or to produce a hardcopy. Linux borrows from the Unix tradition, and some Linux commands are documented in the manual pages.

The GNU project, on the other hand, has long preferred to produce documentation in the Texinfo format. Since many of the commands comprising a Linux system come from the GNU project(so many, in fact, that it had been suggested "Linux" systems should more properly be called "GNU/Linux"), those commands's documentation comes in Texinfo format. Texinfo format allows one to produce either printed documentation or hypertext documentation to be viewed online from the same source. You use the info command to read the online(hypertext documentation).

Let's practice using info:


info libc
I'd like to draw your attention especially to the "Function Index" menu item. If you want to know which files to #include for a particular function, look at the "Library Summary" menu item.

Shell niceties

When you are typing commands at the command prompt you are in fact talking to the program known as the Unix shell. The shell has quite a few nice features besides being able to run the commands you type for you. Tab completion is one such feature. Say you are in your home directory and you want to go to the tutorial directory. You can just type "cd t<TAB>", and the shell should complete the rest for you. The shell will try to complete as much as it can. Depending on how your shell is set up, hitting <TAB> two times in a row may list all the possible completions.

Some characters have special meaning to the shell. It's perfectly valid to have * or ? appear more than once and/or in the middle of a word. For example
tahiti% ls */*.html
will list all the .html files residing in all the directories "one below" the current directory.


Evgeny Roubinchtein