CS170 Lab: More Unix Commands

Organizing Information in Unix:

Having been exposed to some basic unix commands and the use of Linux emacs editor, you are now prepared for more commands in UNIX. This lab will take you through many useful, and comparatively advanced, features of UNIX, which you will find very useful later on in this lab and in other CS courses.
The topics in this section include:

Pop Quiz
Match the command in the list on the left (below) with the description in the list on the right.
pwd		Change file permissions.
cd		Delete a directory.
mkdir		List filenames and directories.
cp		Move a file to another location.
mv		Change to another directory.
rm		Make a directory.
rmdir		Copy a file.
ls		Delete a directory.
chmod		Print working directory - i.e. where am I?

Where am I? (pwd)
Moving around between directories.

    After you have used your Hercules account for a long time, you probably will find a lot of files accumulated in your account.  One way of solving this problem is to create directories and organize these files into a structure of an up-side-down tree.  You can create directories in your home directory, and subdirectories under any of the directories you have already created.  For example, if you choose to, you can create several subdirectories under labs. Let's say you want to create the following subdirectories for labs -- lab1, lab2, lab3. This is how you do it. Suppose you are in your home directory:

pwd			// verify your location
cd labs
mkdir lab1
mkdir lab2
mkdir lab3

Suppose you are in your home directory and you want to change to lab3, type:

cd labs/lab3
pwd			// again, verify your location
If for some reasons you want to change to labs from lab3 (note that labs is one level higher in the tree than lab3), this is how you do it:
cd ..

In Unix,  .. means the directory which is immediately above your current working directory.

Now that you know how to move around between directories, let's do an exercise.  Suppose your user name is joe and your home directory has the following structure:

(Figure 1)

Suppose you start from your home directory, that is, when you do an ls, the following is what you see:

    labs/        mail/        temp/

From here, think about how you can move around:

    1.  How do you change to temp?

         Answer:   cd temp

    2.  From temp, how do you change to test2?

        Answer: cd test/test2

    3.  From test2, how do you change back to temp?

        Answer: cd ../../

    4. From temp, how do you change to lab2?

        Answer 1 :   cd
           cd labs/lab2
   Answer  2:   cd ../labs/lab2

  5.  How do you change from assignment2 to your home directory?

       Answer:  cd

Working with files:  copying, deleting, and renaming

3.  Keeping Secret: File and Directory Permissions

   UNIX is a multiuser operating system, which means that you share the system with other users. As you accumulate files, you'll find that the information that some contain is valuable; some files you want to share, and others you prefer to keep private. UNIX file and directory permissions give you a flexible way to control who has access to your files.

     All UNIX files have three types of permissions—read, write, and execute—associated with three classes of users—owner, group and other (sometimes called world)

     Read permission enables you to examine the contents of files with commands such as cat, write permission enables you to alter the contents of a file or truncate it, and execute permission is necessary to run a file as a command. Each of the three permissions can be granted or withheld individually for each class of user. For instance, a file might be readable and writable by you, readable by other members of your group, but inaccessible to everyone else, or it might be readable and writable only by you.

The ls command shows your file and directory permissions, and the chmod (change mode) command changes them.

The -l option tells ls to make a long listing, such as the following:

hercules[8]% ls -l
total 0
-rw-------    1 joe   csugrd   0  Dec 15 10:40 ex1.cpp
-rw-------    1 joe   csugrd   0  Dec 15 10:41 ex1.bak
-rw-------    1 joe   csugrd   0  Dec 15 10:40 instruction.txt

Right now, joe -- the owner of the three files has the read and write permissions to the files, but the group (csugrd) and world user do not have any permissions to these files.

Limited by the length of this lab, we won't get into too many details about setting modes.  The following diagram may help you understand how permissions work and help you decide what kind of permission you should set for your files.  Remember: a dash "-" means the value of that particular position is 0, while a d, r, w, or  x means the value for that position  is 1.

Unix Permission

As a result, the mode for the above files is 754.

To set permissions so that your files can be viewed by the world, as you would want for web pages, you would use the command

chmod 755 filename

And now, for something a little different: the script command.

In CS170, as well as in many other CS courses, we sometimes require that you hand in a screen capture of the execution of your program together with your source code when you submit your assignments.  This is to make sure that your program works.  We call the screen capture file the "script file".  Unix has a simple yet handy application to record the running of your program -- script.  The Unix man page gives a very good description of the usage of the script command:

     script [ -a ] [ file ]


Script makes a typescript of everything printed on your terminal. The typescript
is written to file, or appended to file if the -a option is given. It can
be sent to a printer later. If no file name is given, the 
typescript is saved in the file typescript.

Note: 1) To specify a script file name, type:

script filename
     2) Make sure you type exit to get out of script when done.  Or you will record all your terminal sessions in your script file!