CSC161 2010F Imperative Problem Solving

Laboratory: GNU/Linux II

Summary: In this laboratory you will continue exploring the functionality provided by several utility programs and the bash shell.

Prerequisites: GNU/Linux I

Preparation

a. Log in to the Linux system.

b. Open a terminal window.

c. Make sure that you are in your home directory.

Exercises

Exercise 1: File Management

Here is a list of commands that are useful for organizing and managing your files and directories on a Linux system. You already saw ls and pwd in the previous lab. In this lab, you'll get some practise with the others.

Utility Description
ls "list" files and directories
pwd "print working directory"
cd "change (your working) directory"
mkdir "make directory"
rmdir "remove directory"
cp "copy" a file or directory
mv "move" (or rename) a file or directory
rm "remove" a file (i.e., delete it)

a. Make a directory called lab2 with the following command:

mkdir lab2

b. Move to your new directory (i.e., make it your working directory) with the command:

cd lab2

c. Now check that you were successful by issuing the command to print your working directory.

d. Copy a file from my directory to yours with the following command. (Note that the final period is necessary: it specifies that the file should be copied into your current directory. Since we have not specified a new filename for the copy, it will be called afile just as it was in my directory.)

cp ~rebelsky/share/linux/afile . 

e. Verify that the copy was successful by issuing the command to print a listing of the files in your current directory.

f. Now rename afile to call it bfile with this command.

mv afile bfile

Note that we can also move a file from one directory to another with the same command. It turns out that to Linux, moving a file and renaming are essentially the same task. Thus, we use a single command to do both.

g. Let's try to delete the directory. What do you expect to have happen when you enter the following commands?

cd ..
rmdir lab2

h. Verify the answer experimentally.

i. Return to the lab directory with

cd lab2

j. Now delete the file as follows. Then print a listing of your files to verify.

rm bfile

k. Finally, move up one level in the directory structure (ie, make the parent of your lab2 directory your current directory) and then delete the lab2 directory as follows.

cd ..
rmdir lab2

l. At this point, you should have tried all the commands in the table above. Note that each of these commands can also be used with "option flags" that modify their behavior slightly.

Exercise 2: Some Key Commands

a. My colleagues find the following variants of these commands handy. Please try them out or look them up in the man pages to determine what they do.

b. My colleagues strongly recommend that you make a directory named trash within your home directory. Then get in the habit of moving files that you no longer want to trash, rather than deleting them directly with rm. It is amazing how easy it is to decide one split second after deleting a file (or a directory full of files) that you really still want them -- especially when working late at night and over-tired.

If you don't already use such a scheme, please make a trash directory now, and start to use it!

c. Two more commands that can be quite useful for moving around the file system are pushd and popd. They let you jump back and forth between distant directories quickly and easily. For example, suppose your current working directory is courses/csc161/hw/ and you want to jump to a directory in another branch of your file system tree, say public_html/courses, and afterward you want to return to your original directory.

The following command will push the name of your current directory onto a stack of directory names that Linux maintains behind the scenes, and then change your current directory to the one named in the command:

pushd public_html/courses

When you are ready to return to your previous directory, you simply type popd. This pops the most recent directory name off the stack and then makes it your current working directory.

Go ahead and give these commands a try. Of course, if you like, you can use pushd several times in a row (pushing multiple directory names onto the stack), and then backtrack through the sequence in reverse order.

Can you think of tasks for which this ability might come in handy? Do you ever find yourself jumping back and forth between two distant directories to complete some task? My colleague, Marge Coahran, likes to use pushd and popd when preparing labs or homework assignments. She edits the files in one (private) directory, then move to a public directory to post the files and make other updates to the course webpage, and then jumps back to my original directory to continue her work. (Note that the !pushd command makes it easier for her to jump to the release directory.)

d. You have probably printed files from the MathLAN before. This is frequently done from within another program, say a web browser, pdf reader, or text editor. With the following commands, you can also print files, and manage print queues, from the command line.

Utility Description
a2ps file
a2ps -Pescher file
a2ps --sides=duplex file
prints file to default printer (handles many standard file formats)
same, for printer named escher
same, but double-sided
lpq
lpq -Pescher
displays jobs in print queue on default printer
same, for printer named escher
lprm 585
lprm -Pescher 585
cancels (removes) print job number 585 from default printer queue
same, for printer named escher

Please don't test these out now, unless you have something you really want printed, but keep them in mind for the next time you do.

Exercise 3: Displaying Text Files

It is often convenient to look at the contents of a text file without having to open it in an editor. In the previous lab, we saw that cat can be used for this purpose, but it is most useful for short files that can be viewed all on one screen.

a. For a quick review, try "cat ~/.bash_profile". Later in this lab, we will talk more about the contents of this file.

b. GNU/Linux provides several other utilities that are useful for "paging" through text files (i.e., for viewing files one page at a time). For example, more allows you to page forward through a file, and less is a new and improved version that allows backward paging as well.

If you haven't used it before, give less a try with the command "less ~rebelsky/share/linux/sciencefac.txt". You should be able to use the up-arrow, down-arrow, Page Up, and Page Dn keys to move around in the file. Type "q" when you are ready to quit using less.

c. At times it is also useful to view just the first few, or just the last few, lines of a file. This can be done with head and tail, respectively.

Try these:

head ~rebelsky/share/linux/socialfac.txt
head ~rebelsky/share/linux/humanfac.txt

Now try the following. (Many utilities that accept a file as input will also happily accept multiple files.)

   
tail ~rebelsky/share/linux/socialfac.txt ~rebelsky/share/linux/humanfac.txt

If you look up tail in the man pages, you will see the following synopsis: "tail [OPTION]... [FILE]...". The second set of elipsis points (after [FILE]) tells us that tail accepts multiple input files.

Exercise 4: File Permissions

In the Unix and Linux worlds, people using the system are divided into three categories:

We can then set permissions on individual files and directories such that people in each of these categories either do or don't have three types of capabilities:

Let's reconsider the following listing of files from a previous lab.

total 40
drwxr-xr-x 4 coahranm mathfac 4096 2007-04-25 16:32 csc105
drwxr-xr-x 6 coahranm mathfac 4096 2007-01-19 16:04 csc152
drwxr-xr-x 4 coahranm mathfac 4096 2007-01-19 19:10 csc201
drwxr-xr-x 4 coahranm mathfac 4096 2007-09-01 15:43 csc211
drwxr-xr-x 4 coahranm mathfac 4096 2007-08-31 17:56 csc301
-rw-r--r-- 1 coahranm mathfac 5808 2007-09-01 20:38 index.html
-rw-r--r-- 1 coahranm mathfac 5808 2007-08-31 19:02 index.html~
drwxr-xr-x 2 coahranm mathfac 4096 2008-01-02 15:55 mmc_files

In the listing, the first character indicates whether the item is a directory (d) or a regular file (-). Thus, in this example csc211 is a directory (line starts with d), and index.html is a regular file.

The next characters list specific file permissions (r=read, w=write, x=execute) for the user, group, and world, in that order. Thus, immediately after the "d" character for the csc105 directory, the sequence "rwx" indicates that user (coahranm) has permission for all three capabilities. However, both the group and "others" have permissions "r-x", which indicates that they can read and execute the directory, but they cannot modify (write) the directory.

You can set the permissions of the files you own using the chmod command. The simplest approach is to assign numbers to each capability (4 for read, 2 for write, 1 for execute) and then to use addition when combining numbers. Thus, 6 = 4+2 (read plus write permission), and 7 = 4+2+1 (all three permissions added together).

Within this framework, you set permissions for a file by specifying the desired capabilities for the user, group, and others (in that order). Thus, in setting up the directory named csc105, I would have issued the following command.

chmod 755 csc105

This gives the user (me) full permissions (7=read+write+execute), while letting everyone else read and execute, but not write (5).

Now on to the exercises.

a. To do this portion of the lab, you must allow others to execute your home directory, but you don't need to allow others to read or write to it. (Part of the exercise is to determine what execute permission really means when applied to a directory, so I'll leave that till later...) To give others execute permission to your home directory, use the following command (where you replace username with your own username):

chmod 711 /home/username

If you like, at the end of the lab you can remove execute permission with the following. (Please be careful not to remove your own permission to read or execute your home directory.)

chmod 700 /home/username

b. If you have not already done so, please make a directory called csc161 within your home directory. (Note that if your current directory is not your home directory, you may find it helpful to move to your home directory first.)

c. Copy the following file into your csc161 directory: ~rebelsky/share/linux/afile

d. For the remainder of this exercise, you will need to work with a partner. Please find someone to work with now. Preferably it should be someone sitting near you who is also ready to work on this problem.

e. Attempt to determine what files are in your partner's csc161 directory. (For example, you could try to create a listing of those files.) What happens?

f. Attempt to read the afile file in your partner's csc161 directory, perhaps by using less. What happens?

g. Next, both you and your partner should make afile readable and make the csc161 directory executable (by everyone).

h. Now attempt to determine what files are in your partner's csc161 directory. What happens?

i. Attempt to read afile in your partner's csc161 directory. What happens?

j. Both you and your partner should make afile unreadable and make the csc161 directory readable.

k. Now attempt to determine what files are in your partner's csc161 directory. What happens?

l. Attempt to read afile in your partner's csc161 directory. What happens?

m. Restore permissions so that your partner can list the files in your csc161 directory and can also read afile.

n. Summarize for yourself what this exercise has taught you about permissions, and please ask if you are not sure!

o. Finally, please make sub-directories in your csc161 directory called labs and homework. Make sure the permissions on the homework directory are set so that others can not read the files there. Then set the permissions on your labs directory however you like.

Exercise 5: Standard I/O Files and I/O Re-direction

In the GNU/Linux system, there are 3 standard files that are always open and available for use, called stdin, stdout, and stderr. (We've put quotation marks around "files" because, while Linux treats them like files behind the scenes, they do not seem much like files to the user. For example, they can not be found in the file system tree.)

stdin - standard input by default means input from the keyboard
stdout - standard output by default means output to the screen
stderr - standard error is used for printing error messages (which are also typically sent to the screen)

However, we can use I/O re-direction to re-assign any of these files (also called channels) elsewhere.

a. For example, many Linux commands send their output to stdout, so by default their output is sent to the screen. However, with output re-direction we can send the output to a file instead.

Give the following command a try. (Think of the "greater than" symbol as an arrow in this context, re-directing the output from ls to the file dirlist.txt.) This should create a new file in your directory, which you can view with the commands from an earlier exercise.

ls -l > dirlist.txt

Now try the following.

ls >> output.txt

Can you discover the difference between what the two symbols > and >> do? (If you want a hint, it may be helpful to use both of these commands repeatedly and look at the results.)

b. To explore the meaning of stdin, let's consider the Linux utility cat one more time. Like many Linux utilities that accept input from a file, specifying an input file for cat is actually optional. If we invoke cat without a file name, it takes its input from stdin (i.e., from the keyboard) instead. This comes in handy when you want to create short text files because it allows you to do so without firing up a more substantial text editor.

To try this, type "cat > temp.txt", and then follow that with some text you want to insert into the file temp.txt. For example, your session could look something like the following:

$ cat > temp.txt
Here is the text that I want to insert.
It is ok to include multiple lines.
How do we stop? Just type ctrl-d.
<ctrl-d>

Recall from the previous lab that ctrl-d is used in Linux as the "end of input" character. Thus, typing ctrl-d at the end of your session indicates to cat that it has received all the input you are going to send it, which causes the cat command to end.

Now consider the file you just created. Do you expect that the final ctrl-d character has been stored in the file? Has it? Do you understand why (or why not)?

As a final note, here is the synopsis for cat from the man pages: "cat [OPTION] [FILE]...". The fact that FILE is enclosed in square brackets tells us the input file for cat is optional. (You should also be able to see that cat can be given multiple input files instead.)

c. Just as we can re-direct stdout to a file, we can also re-direct stdin to come from a file with the < symbol. Thus, if a program expects to receive its input from stdin, we can cause the input to come from a regular file instead. This ability is not always very useful in conjunction with Linux utilities (since we can often indicate input files for them directly on the command line). However, we will find it useful when we start writing C programs, and we will explore it further then.

 

History

January 2007 [Marge M. Coahran]

  • Created.

January 2008 [Marge M. Coahran]

  • Major revision (expansion).
  • Some of the material in Exercise 3 (file permissions) was written by Henry Walker and Sam Rebelsky.

Monday, 30 August 2010 [Samuel A. Rebelsky]

  • Reformatted.
  • Updated some text that expresses opinions.
  • Reorganized numbering (including making the file permissions exercise exercise 4).
  • Made directory deletion exercise a bit more complex.
  • Added "Preparation" section.

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Aug 30 11:20:59 2010.
The source to the document was last modified on Mon Aug 30 11:20:58 2010.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/linux-lab-2.html.
A PDF version of this document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2010F/Labs/linux-lab-2.pdf

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright © 2010 Marge Coahran and Samuel A. Rebelsky. Please contact us for permission to reuse materials.