Organizing Files in Unix

Unix File Structure

The UNIX operating is can be complex. It is a multi-user and multi-processing system. There can be thousands of files in a UNIX system. These files are organized by using a structure of a up-side-down tree. Root is the highest level. Your file hello.cpp is one of the lowest level files. By successfully finishing the previous two labs, you should be able to find the file hello.cpp for user john in the following tree.

Each node in the tree has a path name. Each user on hercules has a home directory. For example, the user john has the home directory /home/hercules/j/john . For each user, the home directory is indicated by the sign ~. The full name for user john's file first.cpp is
OR in the other way,
If you are in the directory
the relative name of the file is first.cpp.

Each user in CS110 class should have the following file structure in his/her home directory by now.

Now you need to know how to organize information in your home directory.

Organizing Information in Unix

Having been exposed to some basic Unix commands and the use of UNIX pico or vi 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	Print working directory - i.e. where am I?	
cd		Delete a file.
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		Change file permissions.

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

    Using Hercules account, 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 cs110 directory: Acctually, you did this in your previous lab exercise.

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

Suppose you are in your cs110 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 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:

    test.txt        hello.cpp        cs110/

From here, think about how you can move around:

	1.	How do you change to cs110 ?

		Answer: 	cd cs110  

	2.	From cs110 , how do you change to lab2?
 		Answer: 	cd labs/lab2

	3.	From lab2, how do you change back to cs110  ?

		Answer: 	cd ../../

	4. 	From cs110 , how do you change to  assignment2?

		Answer 1:	cd 	 and then
		cd cs110/assignments/assignment2  

		Answer 2:	cd assignments/assignment2

	5.	How do you change from assignment2 to assignment3 
		Answer:		cd ../assignment3 
	6.	How do you change from assignment3 to your home directory?

		Answer:		cd

Working with files:  copying, deleting, and renaming

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 john   csugrd   0  Dec 15 10:40 ex1.cpp
-rw-------    1 john   csugrd   0  Dec 15 10:41 ex1.bak
-rw-------    1 john   csugrd   0  Dec 15 10:40 instruction.txt

Right now, john -- 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, it is important to review the script command.

In CS110, 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.
Make sure you type
exit to get out of script when done.  Or you will record all your terminal sessions in your script file!

The following example demonstrates how to use the script commamd.
Remember that you want your script output to contain the compile AND the run of your C++ program.

hercules[25]% script hello.log
Script started, file is hello.log
Script on hercules[1]% g++ hello.cpp -o hello
(or CC hello.cpp -o hello from hercules )
Script on hercules[2]% hello
Welcome to the world of C++
What is your name? Ada

Hi Ada! Welcome to CS110 class.
Script on hercules[3]% exit
Script done, file is hello.log

Copyright: Department of Computer Science, University of Regina.