Basics of the UNIX Operating System

Introduction

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.

We will be using PC's and logging in over a network. One way to do this is to:

  1. Go to the start menu
  2. Select Run ...
  3. Type telnet when the window for the program appears
  4. Under the Connect menu, select one of the wpi machines (ask us which they are!) or select Remote System and type wpi.wpi.edu and select connect.
  5. Type your User Name and Password when prompted to do so.

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.

tree.jpg
Figure 1: 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 of a user is represented by their username.

/ This refers to the root directory.
~ This refers to the users home directory.
. This refers to the current working directory.
.. This refers to the super directory of the current working directory, or the directory up one level.
- This refers to the previous directory.

root.jpg
Figure 2: Sample Directory Tree

Figure 2 represents a sample directory tree. ryan's home directory contains two items: a subdirectory, myDir, and a file, file1. Every time ryan 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 '~username'.

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

UNIX is case-sensitive; that means file1, File1, and FILE1 all refer to different files. A '/' is used to separate subdirectories. A pathname is a sequence of symbols used to identify a file or directory. Every file has a filename. The simplest type of pathname is just a filename. If you specify a filename as the pathname, the operating system looks for that file in the current working directory. If the file resides in a different directory, you must tell the operating system how to find that directory by specifying a pathname.

There are 2 types of pathnames. If the pathname starts form your current working directory then it is a relative pathname. If it starts from the root directory then it is an absolute pathname.

For Figure 2, the pathname of file1would be:

If user ryan is in his home directory:
file1
If user ryan is outside his home directory:
~/file1
If user >kate wants to refer to ryan's file:
~ryan/file1
Absolute pathname for ryan's file:
/ryan/file1

Similarly, the pathname of file2 would be:

If user ryan is in his home directory:
myDir/file2
If user ryan is outside his home directory:
~/myDir/file2
If user kate wants to refer to ryan's file:
~ryan/myDir/file2
Absolute pathname for ryan's file:
/ryan/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 working directory to a different directory.

For example, in order to change myDir in ryan's home directory:

If user ryan is in his home directory:
cd myDir
If user ryan is outside his home directory:
cd ~/myDir
If user katewants to access ryan's files in myDir:
cd ~ryan/myDir
cd can also perform the following specific tasks:

cd ~
takes users to their home directory.
cd ..
takes a user up one directory. (If ryan is in myDir, this command will take him to the directory above, which is his home directory.)
cd -
takes users to the directory they were previously in. (If kate were previously in her home directory, but had changed to ryan's (via cd ~ryan,) then cd - would take her back to her home directory.)
cd /
takes users to the root 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
ls ~ryan
would display:
myDir/       file1
Thus, the contents of the home directory of ryan 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 ~ryan
might produce the following output:
drwx-w---x 2 ryan 100 4096 Jun 14  2000 myDir 
-rwx-w---x 1 ryan 100 1450 Jun 14 13:01 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, ryan. Following this is the userid of the user; ryan is user 100. Following this is the size. file1 is 1450 bytes. A byte is the amount of space needed to store 1 character in a computer. myDir is 4096 bytes (all directories are 4096 bytes). Next is the month, followed by the day, followed by either the year or time the item was created. If the file way created during the current year, the time is displayed, if not the year is displayed. 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 ryan's current directory is myDir,

pwd
would display:
/ryan/myDir/

rm (Remove)

The remove command deletes a file. For example, if ryan wishes to delete file1 he can type:
rm file1
It is important to remember that once a file is deleted, it is gone forever. Use this command carefully!

cp (Copy)

The copy command, is used to copy a file or group of files. If kate wishes to copy file1 to myDir she types:
cp file1 ~/myDir/
We can use cp to make a copy of a file. If kate 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 kate 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. mv is often used to rename a file.

mkdir (Make Directory)

The mkdir command creates a new directory. For example, if ryan 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 ryan wishes to delete the empty directory newDir he types:
rmdir newDir

more

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 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 ryan, the command whoami displays:

ryan
Possible output for the command who:
kate tty0 Jul 14 12:00
ryan tty1 Jul 14 11:45
mike 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

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 ryan
Login name: ryan In real life: Ryan Doe
Phone: 555-1234
Directory: /usr1/ryan   Shell: /sh/tcsh
No unread mail.
User    Real Name Idle TTY Host
ryan    Ryan 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 ryan (uid 100):
Filesystem blocks quota limit
/ryan      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:

-rwxr----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, 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 Permission

When we create our web pages, we will give the Public permission to access our pages, but not change them. chmod is a unix command to change the permissions of a file. there are two methods to use chmod, one is to use the binary/octal method, the other is to use arguments to specify how to change the permissions.

Binary/Octal Method:

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, (rwx r-- 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 (rwx rwx ---) would be 770; and the value for permission for all 3 groups to perform all 3 operations (rwx rwx rwx) would be 777.

The other syntax for this command is:

chmod who[+/-]permissions file
who has to be a single letter signifying which set of permissions to affect: user, group, or other. The codes are as follows:
u = user
g = group
o = other
a = all
The [+/-] designates whether to allow or disallow the following permissions. The permissions, like octal mode, are for reading, writing, and executing. The codes for these are:
r = read
w = write
x = execute
There can even be more than one argument to specify how to set all the permissions. These arguments must be separated by a comma:
chmod who[+/-]permissions,who[+/-]permissions file
If I have a file named foo, and I want to make sure everyone has permission to read it, I would execute the following command:
chmod a+r foo
This means, for the file foo, I want to add the read permission to all the sets of permissions. Suppose I want to give everyone read and write, and execute permission (this means anyone can erase it too!). The command would look like:
chmod a+rwx foo
Now, suppose I want to give everyone read permission, but only give myself write and execute permissions, I would type:
chmod a+r,u+wx foo
The advantages of using this method over the octal method is that it is easier to understand, and you can set one permission regardless of the other permissions. The disadvantage is that a complicated command takes longer to type and think out (once you know the octal codes, the octal method is almost always faster, but if you forget them, the letter method is usually easier to understand). The best way to learn how to use chmod is to use it. Try the examples in your unix account. You can create a dummy file named foo by typing:
touch foo
you can check the permissions on foo by typing:
ls -l foo