In this lab, you will:
Click the little computer above for a detailed description.
If you need help during the week, you are welcome to go to CL119 during the Office Hours posted for lab instructors.
Having been exposed to some basic UNIX commands and the use of the 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.
Before you can master these commands you should have a firm grasp of file management basics as covered in CS 110. Click here for a review The topics in this section include:
|a)||pwd||1)||Change file permissions.|
|b)||cd||2)||Delete a file.|
|c)||mkdir||3)||List filenames and directories.|
|d)||cp||4)||Move a file to another location.|
|e)||mv||5)||Change to another directory.|
|f)||rm||6)||Make a directory.|
|g)||rmdir||7)||Copy a file.|
|h)||ls||8)||Delete a directory.|
|i)||chmod||9)||Print working directory - i.e. where am I?|
|cal [month #] year||Prints a calendar of the specified year. e.g. cal 2010
If a month number is specified, prints only that month. e.g. cal 3 2010 (for March 2010)
|cat file1 [file2 ...]||Concatenate (join together) specified files and direct
the output to the standard output device - the screen.
This command is commonly used to display the contents of one file on the screen. (It's simpler than getting in and out of an editor.)
|date||Print the current time and date.|
|who||Lists who is logged into a machine. It provides information such as the user's login name and the time when the user logged on.|
|w||Lists who is logged into a machine. Provides information such as the user's login name and the time when the user logged on. It also provides information about what the user is curently doing.|
|sort||Sorts the input stream or the contents of files. To sort the contents of a file, use sort filename.|
|wc||Displays the number of lines, words and characters in a file. To display only the number of lines, you can use wc -l.|
|file file||Perform tests on a file to determine its type. Useful if you want to make sure a file is not an executable before you try to edit it.|
|cmp file1 file2||Compare two files to see if they are the same. Reports just the first difference unless you specify -l|
|diff file1 file2||Displays the differences between file1 and file2. This lists the changes necessary to convert file1 to file2.|
|find path option||Search down directories for a file. e.g. find ./ -name gold.cpp would search in the current directory and in all subdirectories for the file called gold.cpp|
|grep [option] string [file(s)]||Search for a string pattern in a file. There are several options. e.g. grep namespace *.cpp would search the current directory for the string "namespace" in all .cpp files and show the lines in each file where the string occurs. e.g. grep -n namespace *.cpp would perform the same search but also give the line numbers in which the string was found.|
|ps||Lists the processes that are running for a terminal. To see all the processes that are running for you, use ps -fu yourusername. This command is often used with kill.|
|kill [option] processid||Kill the process specified. e.g. kill -9 1455 would perform a "sure kill" (option 9) on process id "1455". This is a handy command if you change your mind after sending a job to the printer and want to delete it from the queue. See the lpq command to see how you can query the print queue for process ids.|
|lpq -P[printername]||Query the specified printer to see active jobs. Reports process ids of jobs. e.g. lpq -Pcl122|
|quota -v||Show how much disk space you are using ("usage") on a multi-user Unix system and what your limit is ("quota"). The numbers given refer to kilobytes of space.|
You can redirect the output of a program to a file. You can also redirect the input of a program so that it reads a file instead of the keyboard. There are four symbols you should know that will help you do this:
hercules% cal 9 2007 > file hercules% cat file September 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 hercules% cal 10 2007 > file file: File exists.
The first redirect put the normal output of the cal command into the file file. The second failed. This failure is a CS Department configuration. A > redirect will not always fail.
hercules% cal 9 2006 >> file2 file2: No such file or directory. hercules% cal 9 2006 > file2 hercules% cat file2 September 2006 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 hercules% cal 10 2006 >> file2 hercules% cat file2 September 2006 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 October 2006 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
The >> would not create a file, returning an error instead. This is a CS Department configuration. Don't rely on it. Unlike the first example, after the append redirect the file has both September and October in it.
venus% cal 9 2007 >! file3 venus% cat file3 September 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 venus% cal 10 2007 >! file3 venus% cat file3 October 2007 S M Tu W Th F S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
See how the contents of file3 are completely overwritten by the second command.
The < symbol is used less often than the other 3, but it is still useful. You can use it to automate a program for testing purposes. It is used that way in the operator overloading lab to test exercise 2. This can be demonstrated using hello.cpp from lab 1 and a file that has a name and a newline character at the end. eg:
hercules% cat namefile John_Jacob_Jingleheimerschmidt hercules% ./hello < namefile Welcome to the world of C++ What is your name? Hello John_Jacob_Jingleheimerschmidt! Welcome to CS170. hercules%
program1 > temp program2 < tempbut Unix makes it much easier. You have the | symbol, called pipe. It is located just above the Enter key on most keyboards. It connects the standard output of one program to the standard input of another like so:
program1 | program2One possible use of pipe is to print output directly. You could print the calendar for 2007 for instance:
cal 2007 | lpr -Pcl122
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 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 hercules%
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.
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 644 filename
To make that web page available to the web from hyperion it would need to be in your public_html directory and both your home ~ and public_html would need their permissions set to 755.
The lab exercises are worth 2 marks allocated as your lab instructor sees fit. In general 1 mark will be for effort and the second will be for a good solution. Most labs are due by the end of the lab period. The material in labs is likely to be on the lab test. It may also be on class tests.
Description of "who", "sort" and "wc":
ssh a044882 for this part:
Make a script with the answers to the following questions. If your command creates a file use cat to show its contents.
cp /net/data/ftp/pub/class/170/ftp/html/visible.html visible.html cp /net/data/ftp/pub/class/170/ftp/html/invisible.html invisible.html
The path: pub/class/170/ftp/html The files: visible.html invisible.htmlPLEASE avoid copying and pasting code. It makes a mess of formatting and may render the code uncompilable.
Home directory: rwx--x--x public_html and all subdirectories: rwxr-xr-x html files: rw-r--r--HINT: You will be modifying permissions on the two directories listed above and visible.html
Wednesday, 22-Feb-2012 12:12:11 CST