CS115 Lab: Essential Command Line Techniques


Highlights of this lab:

In this lab,  you will:

Lab Exercise:

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.


Controlling Information Flow in Unix.

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:


Pop Quiz

Match the command in the list on the left (below) with the description in the list on the right.
COMMAND DESCRIPTION
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?

"New" Unix/Linux Commands

Here are some more Unix/Linux commands that you need to learn. Remember that you can enter man command to get a complete description of any Unix/Linux command and its options.

Command Description
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.

Notes:

Redirecting: getting input from files and sending output to files

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:

Symbol: >

Redirect standard output to a named file. May overwrite the file. eg:
hercules[33]% cal 9 2007 > file
hercules[34]% 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[35]% 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.

Symbol: >>

Redirect standard output to a named file. Appends to the file. eg:
hercules[43]% cal 9 2006 >> file2
file2: No such file or directory.
hercules[44]% cal 9 2006 > file2
hercules[45]% 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[46]% cal 10 2006 >> file2
hercules[47]% 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.

Symbol: >!

Redirect standard output to a named file. Always overwrites the file. eg:
venus[59]% cal 9 2007 >! file3
venus[60]% 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[61]% cal 10 2007 >! file3
venus[62]% 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.

Symbol: <

Redirect a named file to standard input.

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[1]% cat namefile
John_Jacob_Jingleheimerschmidt
hercules[2]% ./hello < namefile
Welcome to the world of C++
What is your name?
Hello John_Jacob_Jingleheimerschmidt! Welcome to CS170.
hercules[3]%

Pipelining: Sending the output of one program to the input of another

Sometimes the output of one program is exactly what you want to use as input to another program. If that is the case you could do something like this:
program1 > temp
program2 < temp
but 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 | program2
One possible use of pipe is to print output directly. You could print the calendar for 2007 for instance:
cal 2007 | lpr -Pcl122

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
hercules[9]%

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 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.

Lab Exercise -- Advanced Unix

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.

Part 1: "cal" and Redirection


Part 2: "who", "sort", and "wc" with Redirection and Piping

Description of "who", "sort" and "wc":

Please 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.

  1. How can you redirect the output of who to a file? __________________________________

  2. How can you redirect the input of sort from that file? __________________________________

  3. How would you use a pipe to combine those last 2 steps? __________________________________

  4. How would you redirect the output of the last step to another file? __________________________________

  5. How would you use wc to count the number of users logged in (include duplicates in your count)? __________________________________
You can expect questions like this on the lab test.

Part 3: File Permissions and Web pages (Just for Fun)

For this exercise you will download two files and publish one of them on the web.


This page last modified:
Wednesday, 22-Feb-2012 12:12:11 CST

CS Dept Home Page
CS Dept Class Files
CS115 Lab Files

Copyright: Department of Computer Science, University of Regina.