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.
COMMAND DESCRIPTION pwd Change file permissions. 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 Print working directory - i.e. where am I?
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 locationIf 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:
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:
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
Here, we still make use of Figure 1. Suppose you want to edit a C++ program for your lab assignment but also keep the first draft in case you lose the original. You could enter the following:
hercules[1%] cd cs110/labs/lab3 hercules[2%] ls instruction.txt ex1.cpp hercules[3%] cp ex1.cpp ex1.bak hercules[4%] ls instruction.txt ex1.cpp ex1.bak
When it works, cp prints no output (that is, it does not give any error messages), following the UNIX tradition that "no news is good news."
We won't spend too much time here talking about copying files from one directory to another, because this should not be difficult after we have talked about moving around between different directories. Think about the following question: suppose your current directory is ~/cs110/labs/lab3 and you want to copy the ex1.cpp to your assignment1 directory. How do you do this?
cp ex1.cpp ../../assignments/assignment1/
Answer 2: cp ex1.cpp ~/cs110/assignments/assignment1
(Note: ~/ stands for your home directory)
To remove the file ex1.bak, enter the following command:
hercules[ 5%] rm ex1.bak
Like cp, rm displays no message when it works.
Hint: you can also delete directories that you no longer need, but the command is different from the one you use to delete files. For example, to delete the lab1 directory in Figure 1, you type:
rmdir lab1Remember: to make rmdir work, the directory being deleted must be empty; that is, it should include neither files nor directories. If it does, you have to delete all the files and subdirectories first. A quicker way to delete a certain directory and all the files and subdirectories that it contains is to use the -r option in the rm command. For example, suppose the lab1 directory has other two subdirectory test1 and test2. If you want to delete the lab1 directory including its subdirectories test1 and test2 and any possible files in these two subdirectories, type
rm -r lab1in your labs directory.
Be careful with rm
-r [dir]. It will recursively delete
everything from [dir]down!
pico assignment2.cppand want to give your fingers a rest. Instead, you can enter the following:
hercules[ 6%] mv assignment2.cpp asgmt2.cpp
There is an important difference between cp and mv: cp leaves the original file in its place, but mv removes it. Suppose that you enter the following command:
hercules[ 7%] mv asgmt2.cpp ~/cs110/assignments/assignment2/
This command removes the copy of asgmt2.cpp . So, if you want to retain your original file, use cp instead of mv.
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% 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 hercules%
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.
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:
SYNOPSIS script [ -a ] [ file ] DESCRIPTION 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% script hello.log Script started, file is hello.log Script on hercules% g++ hello.cpp -o hello (or CC hello.cpp -o hello from hercules ) Script on hercules% hello Welcome to the world of C++ What is your name? Ada Hi Ada! Welcome to CS110 class. Script on hercules% exit exit Script done, file is hello.log hercules% hercules%
Copyright: Department of Computer Science, University of Regina.