Afternoon Module 1

Basics of the UNIX Operating System

by Jesse Banning and Professor Lemone

We will be using the UNIX operating system, a powerful network operating system, or OS. The user interface of a UNIX system is called a shell. Shells are what actually respond to what you type and tell the OS what you want it to do.

Logging In

This module will introduce you to some fundamental ideas and techniques for using the UNIX environment. (***I will write this section when we know what machines we are using***)

Understanding Directories

The directory structure of the UNIX system is very similar to that of a personal computer. Folders on a personal computer are called directories in UNIX. Directories can contain files and other directories called subdirectories that, in turn, can contain files and subdirectories and so on. The UNIX directory structure is analogous to the trunk and roots of a tree.

Figure 3: Directory Structure Example

Every UNIX system contains a 'root' directory which is the trunk of the tree. The files and directories branch out from the trunk. Users have their own space set aside on the disk for their files and directories. This space is called the user's 'home directory.' The home directory (sometimes called the user's root directory) of a user is represented by their username.

Figure 4: Sample Directory Tree

This example represents the home directory of user johndoe. johndoe's home directory contains two items: a subdirectory, MyDir, and a file, File1. Every time johndoe logs into this computer, he will begin at his home directory. The home directory of a user can be represented by a '~,' (called a tilde). Similarly, the home directory of any other user can be represented by '~' followed by the username.

For example, for johndoe, '~' would represent his own home directory, while ~janedoe would represent the home directory of user janedoe.

UNIX is case-sensitive; that means file1, File1, and FILE1 all refer to different files. A '/' is used to separate subdirectories. A pathname is used to refer to files and subdirectories. For Figure 4, the pathname of File1 would be:

If user johndoe is in his home directory: File1
If user johndoe is outside his home directory: ~/File1

If user janedoe wants to refer to johndoe's file:

~johndoe/File1

Similarly, the pathname of File2 would be:

If user johndoe is in his home directory: MyDir/File2
If user johndoe is outside his home directory: ~/MyDir/File2
If user janedoe wants to refer to johndoe's file: ~johndoe/MyDir/File2

Managing Files

Now that you've seen the UNIX directory structure, we can look at some commands to use. The following is an overview of some useful commands.

cd (Change Directory)

The command cd switches the user from the current directory to a different directory. For example, in order to change from johndoe's home directory to MyDir:

If user johndoe is in his home directory: cd MyDir
If user johndoe is outside his home directory: cd ~/MyDir
If user janedoe wants to access johndoe's files in MyDir:

cd ~johndoe/MyDir
cd also has some variants that perform specific tasks:

cd ~ takes users to their home directory.
cd .. takes a user up one directory. (e.g., If johndoe is in MyDir, cd .. takes him to the directory which is the directory above MyDir.)

cd - takes users to the directory they were previously in. (e.g., If janedoe were previously in her home directory, but had changed to johndoe's (via cd ~johndoe,) then cd - would take her back to her home directory.)

ls (List)

ls displays either the contents of the current directory, if no path is specified, or the contents of the specified directory. For example, if there were 1 file called File1 and 1 subdirectory called MyDir in johndoe's home directory, then ls ~johndoe would display:

MyDir/ File1

Thus, the contents of the home directory of johndoe is the subdirectory, MyDir, and the file File1.

ls also has some options. One of the most common is ls -l which displays the contents of the specified or current directory with more information. For example:

ls -l ~johndoe might produce the following output:

drwx-w---x 2 johndoe 100 8192 07/14/97 12:00p MyDir

-rwx-w---x 1 johndoe 100 1450 07/14/97 12:00p File1

The first character in each line represents whether the item is a file or a directory (d for directory, - for file). The next nine characters represent the permissions set to that item (permissions will be discussed below). The next number represents the number of links of the file. (Not discussed here.) Next is the owner of the file, johndoe. Following this is the userid of the user; johndoe is user 100. Following this is a size. File1 is 1450 bytes. A byte is the amount of space needed to store 1 character in a computer. MyDir is 8192 bytes (all directories are 8192 bytes). Next is the date and time that the item was created. And finally, the last column is the full filename.

pwd (Present (or Print) Working Directory)

This command displays the pathname of the current directory. For example, if johndoe's current directory is MyDir, pwd would display: /johndoe/MyDir/

rm (Remove)

The remove command deletes a file (and we don't even know how to create one yet!). For example, if johndoe wishes to delete File1 he can type: rm File1

(NOTE: It is important to remember that once a file is deleted, it is gone forever. Use this command carefully!)

cp (Copy)

The copy command, as its name implies, is used to copy a file or group of files. If janedoe wishes to copy File1 to MyDir she types: cp File1 ~/MyDir/

We can use cp to make a copy of a file. If janedoe wants another file just like File1 in her home directory called File1save, she can type: cp File1 File1save

mv (Move)

Move performs the exact same function as cp, except that after the file is copied, it is deleted from the original location. For example, if janedoe wishes to move File1 to MyDir, (and keep the same name) she can type: mv File1 ~/MyDir/

If she is in her home directory and wants to move it to MyDir AND give it a different name, she can type: mv File1 ~/MyDir/newFile1

An ls of the home directory would no longer show File1.

mkdir (Make Directory)

The mkdir command creates a new directory. For example, if johndoe wishes to create the directory NewDir within his home directory he types: mkdir NewDir

rmdir (Remove Directory)

The rmdir command removes an empty directory (so you have to remove the files in the directory first with rm ). For example, if johndoe wishes to delete the empty directory NewDir he types: rmdir NewDir

more filename

The command more displays the contents of files one screen at a time. For example, to display the contents of File1 one screen at a time use the following command: more File1 (Then hit the space bar to get another screenful)

Getting Information

who username or whoami

who and the related command whoami give information about users logged into the computer. whoami informs you of your username while who lists all the people currently logged into the same computer that you are logged into.

For example, for user johndoe, the command whoami displays:

johndoe

Possible output for the command who:

janedoe tty0 Jul 14 12:00
johndoe tty1 Jul 14 11:45
someguy tty8 Jul 14 05:45

The first field is the username, the second field is the terminal port they are connected to, and the final two fields are the date and time of connection.

finger username

An easy way to find out more about someone is to 'finger' them from your terminal. 'finger'ing someone will tell you if they're currently logged on, or when they last logged off, their home directory, real name, shell, and their mail status. The following is sample output for the command finger johndoe

Login name: johndoe In real life: John Doe
Phone: 555-1234
Directory: /usr1/johndoe Shell: /sh/tcsh
No unread mail.
User Real Name Idle TTY Host
johndoe John Doe 0:03 p1 moose.wpi.edu

Typing finger with no arguments returns a list of all users on the system.

quota

quota displays your disk quota. A disk quota is the amount of disk space that you are allocated. Every once in awhile, you should check to see if you are running out of disk space: The following is sample output from the command quota.

Disk quotas for user johndoe (uid 100):
Filesystem blocks quota limit
/johndoe 3498 4000 5000

Getting Help

man command_name

The man command accesses the online unix manual pages. Because they are hard to read, we often try every other option before resorting to the man pages (ask your friend, try the web, look it up in a book, etc.) In the above, command_name is the name of the command you want to look up. If you don't know the name of the command, you can do a keyword search by adding the "-k" option before the command_name. If the command you are looking for is anywhere in the man pages, this will find it.

Understanding File Permissions

Unlike personal computers where usually only one person has access to the information contained within the computer, UNIX allows many people to access the computer and its information. In order to ensure the privacy and security of data and programs on a UNIX system, the file system is designed to allow users access only to that information that they have permission to access.

Every file is stored with a file permission that designates who has the ability to (1) read from, (2) write to, and/or (3) execute that particular file.

A file permission consists of nine items: three groups of the three letters, r, w and x where r means read, w means write and x means execute.

For example: -rwx-w---x File1.txt

The three groups are Owner, Group and Public. For the above: Owner: rwx Group: r-- Public: --x

Here, the Owner (usually the creator) of the file has permission to read, write (change) and execute File1.txt. We won't be using groups, but here the Group has permission to Read (look at) the file, but not to Write (change) or Execute the file. And the final three characters represent the level of access that the Public (sometimes called the World) has to the file.

Therefore, in our example the Owner of the file has permission to read, write to, and execute the file. The Group has the ability only to read from the file, and the Public has the ability only to execute the file.

Changing File Permissions

When we create our web pages, we will give the Public permission to access our pages, but not change them. We do this with:

chmod permission filename

where permission is a three digit number with each digit an octal number (0 - 7). The first digit represents the Owner's permissions, the second represents the Group's permissions, and the third represents the Public's permissions. Each digit is a combination of the three possible permissions: read, write, and execute.

We have to look at each octal digit in binary (base 2) to understand the meaning. Let's do this with the example chmod 755 File1.txt

In binary, 755 is 111 101 101 (spaces for clarity). Each group of three binary digits represents the triple rwx. The first group, 111, means that the owner can read, write and execute File1.txt. The next group of three, 101 indicates that the Group can read and execute, but not write (change) File1.txt. The third group, also 101 indicates that the Public can read and execute, but not write to File1.txt.

Therefore, the value for a permission of Owner to read, write, and execute, Group read, and Public read, (rwxr--r--) would be 744; the value for a permission of Owner to read, write, and execute, Group to read, write, and execute, and Public none (rwxrwx---) would be 770; and the value for permission for all 3 groups to perform all 3 operations (rwxrwxrwx) would be 777.

Go on to EMACS