Laboratory: Getting Started with Linux
CSC 105 - The Digital Age

Summary: In this laboratory, you will explore some of the basic Linux commands.



Exercise 0: Preparation

Open a second tab in your browser (by selecting New Tab from the File menu). Navigate to the "Linux Commands" page that I posted on the course website.

If you are not familiar with having multiple tabs open in your broswer, note that you can switch between them either by clicking on the "tab" for the page you want, or by pressing the Page Up and Page Down keys while holding a Ctrl key down. (In future, I will denote this action by Ctrl-"Page Up" or something similar.)

Exercise 1: Getting Your Bearings

Open up a new terminal window. Recall that this gives you the ability to type commands to a program called the shell. When working in shell, you always have a current location (properly called a working directory) within the file system. If you ever lose your bearings, you can check your current location with the command pwd which stands for "print working directory."

Give that a try now by typing pwd in the terminal window and pressing enter.

You should find that immediately after logging in, you are always located in your "home directory," denoted by /home/username (Note that here and elsewhere in this lab, when we say username, you should substitute your own MathLAN username.)

Exercise 2: Printing a File List

If you want to know what files you have in your working directory, you can print a listing of them with the command ls (Note that the first character is the letter 'l', not the number '1'.)

Give that a try now.

Most Linux commands have options that we can specify to modify their default behavior in various ways. These options are usually specified by using syntax like the following:

   ls -l
Here again, after the hyphen is a letter 'l', not a number '1'. Try out this command to see the difference in the behavior of ls when you add the option specifier "-l".

You should have made a file called "lab1-test.odt" in our last lab session. Can you determine exactly what time you saved the file?

You can also list the files in other directories, by specifying what directory you are interested in. For example, try the following to list the files in a directory I have made for you in my account:

   ls -l ~weinman/courses/CSC105/

Exercise 3: Copying Files

Use the "Linux Commands" webpage to find the command used for copying files, then use it to make a copy of your file lab1-text.odt. You could name the copy lab1-backup.odt if you like.

Check that your backup copy was made successfully, by printing a listing of your files.

Now copy the file called faculty.txt you just saw in my directory into your directory. (Recall when specifying the name of that file in your copy command, that the full name includes both the directory where the file is located and the name of the file itself. In contrast, when specifying the name of the new file you are creating, giving the file name alone is sufficient. Do you remember why that is? Answer: If you give only a filename, Linux will interpret that as a relative reference to the file, and look for it in your current directory.)

After copying the file, check your work to make sure you now have the file in your directory.

Did you use the filename faculty.txt for the new copy of the file you just made in your directory? If so, great. If not, please make yet another copy, and this time call it faculty.txt (because we will use this file in the next exercise).

Exercise 4: Displaying Text Files

If you want to look at the contents of a text file, you could certainly open it in a word processor, but in Linux you can also "page" through the file right from the command line. To see what I mean by this, try the following command:
  less faculty.txt
Note that you can move forward and backward in the file by pressing the Page Up and Page Down keys. How many Grinnell faculty are there with the last name Jacobson?

When you are ready to quit using less, type the letter 'q' -- it stands for 'quit'.

You may wonder why this command would be called "less." Did I mention before that Linux folks can be a bit geeky? There is an older command called "more" that worked similarly -- you could use Page Down to move forward through (see more of) a file. less is a new and improved version that also lets you use Page Up to move backward.

As a final note, be aware that less can only be used with text files. You can not (successfully) display a word processor file, such as lab1-test.odt using less. This is because the word processor includes special characters, called "control characters," that are not printable using less. We can talk more about why this is true a little later in the course.

Exercise 5: Creating a Directory

Find a command on the "Linux Commands" page that will let you make a new directory, and then make a new directory called "lab2" within your current directory. If you have any difficulty doing this, please ask myself, our lab T.A., or a colleague for help.

After you have made your directory, check your work by listing the files in your current directory. Hmmm... now you will find that directories are also included in the file listings you print. If you print the "long form" of the file listing, you will get output similar to the following. Note that the very first character of each line is either a 'd' or a hyphen. The 'd' tells you that the associated "file" is really a directory.

   drwxr-xr-x 2 weinman mathfac 4096 2009-01-23 10:55 csc105
   drwxr-xr-x 2 weinman mathfac 4096 2009-01-22 21:46 csc201
   -rw------- 1 weinman mathfac    9 2009-01-23 11:30 fileA.txt

Exercise 6: Renaming and Moving Files

Find the command mv on the "Linux Commands" page. You will see that the description of the command says that it renames a file, from sourcefile to targetfile. This may seem like a funny thing for the "move" command to do, right? I will explain why shortly.

In the meantime, let us practice renaming files. At this point, you should have a file named lab1.backup.odt. Use the mv command to rename it backup.odt (and check your work).

Next, move backup.odt into your new directory (lab2) with the following command, and then check your work by printing listings of both your current directory and the directory lab2.

  mv backup.odt lab2/backup.odt
Why can this same command be used to rename files and move them, you may ask. The key is in the second file name of the example, "lab2/backup.odt". Note that it includes both a directory name and a "base" file name (backup.odt). In fact, recall that the full file name of any file includes the "directory path" where it is located. The full file name of the new file you just made is /home/username/lab2/backup.odt. In the example, we used a relative reference to the file, shortening the name to lab2/backup.odt relative to the current directory "/home/username/".

Thus, what you really just did in this example is to change the file's name from the original (/home/username/backup.odt) to a new name in a new location (/home/username/lab2/backup.odt). Indeed, when you understand full path names in Linux, you see that moving a file to a new directory is exactly the same thing as renaming it. So it is no longer surprising that we can do both with the same command, right?

Exercise 7: Creating a Trash Directory

As I am sure you are aware, in the Windows and MacIntosh operating systems when you delete a file, there is an automatic safety net in place for you: the file is not really deleted, it is moved to a folder called "trash" or something similar. Linux does not provide this automatic safety net for you. Rather, Linux works under the assumption that if you tell it to delete a file, you want it to delete the file -- and it does so. That said, we are quite capable of making our own safety net in Linux.

Your next task is to create a directory called /home/username/trash (after which, you should of course check your work).

Now move the file backup.odt from /home/username/lab2/ into your new trash directory. I suggest that whenever you want to delete a file, you really move it to the trash directory instead. That is what I do, and then every so often I "empty the trash" by actually deleting the files there.

Exercise 8: Changing Your Working Directory

Verify the name of your current working directory, with the command pwd. At this point, at least for most of you, it should be your home directory /home/username/. (If this is not the case for you, and you are not sure why, please ask for help.)

Find a command on the "Linux Commands" page that lets you move to a different directory (i.e., it lets you change what directory is your current working directory). Then use the command to move to the directory /home/username/lab2.

Verify your new location.

Now move to the trash directory that you just made. (For a challenge, do this by specifying a relative path name for the trash directory rather than the full path name.)

Finally, try using the command cd without specifying a directory to move to. What does the command do when used that way?

Exercise 9: Changing File Permissions

Each file and directory in Linux belongs to both an owner and a group. Usually, the owner is the person who created the file. You can see both the owner and group for a file by listing files with the "long form" option. Here is a sample, much like the one above:
   -rw------- 1 weinman mathfac    9 2009-01-23 11:30 fileA.txt
   -rw-rw-rw- 1 weinman mathfac    9 2009-01-23 11:30 fileB.txt
   -rwxr-xr-x 1 weinman mathfac   31 2008-12-31 14:14 greeting
The owner of each of these files is weinman, and the group the file belongs to is mathfac.

What is the owner and group of the files in your home directory?

In addition, each file has three types of permissions:

We can either allow or deny each of these permissions to three categories of people: You can read these permissions from the long file listing as follows:
   drwxrwxrwx 1 weinman mathfac    2 2008-12-31 14:14 mydir
The first triplet of rwx (in blue) indicates the user's permissions, the second triplet (in black) indicates the group's permissions, and the third triplet (in green) indicates the world permissions. The presence of an r, w, or x means that this permission is allowed for the particular category. If the permission is denied, the spot will be occupied by a "-" (dash).

Remarkably, you can give yourself no permissions (though you still have the ability to change them). The following exercises will give you some practice with Linux file permissions.

  1. Copy the file greeting from my shared directory with the following command:
      cp ~weinman/courses/CSC105/greeting .
  2. Let's start by having you deny all permissions to the file by giving the command:
      chmod ugo-rwx greeting
    Check that this removed all permissions from your file by using the ls -l command.
  3. What happens if you try to use the command less to read the file?
  4. Change the permissions with the command:
      chmod ugo+r greeting
    What effect does this have? (check using ls -l)
  5. Now what happens if you try to use the command less to read the file?
  6. Change the permissions with the command:
      chmod u+w greeting
    What effect does this have on the permissions?
  7. What happens if you try to run the file as a program by issuing the command:
    (Note that the leading characters ./ tell the computer to look in the current directory for the program.) Why do you think that happens?
  8. Change the permissions with the command:
      chmod ugo+x greeting
    What effect does this have on the permissions?
  9. Now what happens if you try to run the file as a program?
  10. Change the permissions with the command:
      chmod og-r greeting
    What effect does this have on the permissions?
  11. Ask a neighbor to try to run the your file as a program with the command
    where username is your username, not theirs. What happens?
  12. While they are at it, what happens when they try to read your file with less? Why do you think that happens based on the current permissions of your file?
  13. How can you give members of the file's group read permission? Do this to the file greeting.
  14. Now can your neighbor read the file? What can you conclude about the group your neighbor belongs to?

For those with extra time

Read and execute permissions operate a little differently on directories than they do with files.
  1. Copy the faculty.txt to your lab2 directory and double-check that it has user (owner) read permissions. If you need help on this step, refer to exercises 3 and 9 above or ask the instructor or a colleague.
  2. Now, see if you can find a set of permissions for your lab2 directory that forbids you from seeing the files in that directory (i.e., with an ls command) and forbids you from reading the file faculty.txt (e.g., with the command
      less lab2/faculty.txt
    Note you will not be changing the permissions of faculty.txt.
  3. Here's something even more interesting. See if you can find a set of permissions for your lab2 directory that forbids you from seeing the files in that directory (i.e., with an ls command), but does allow you to read a file you know is in the directory (e.g., with the same less command as above).

Written: Marge M. Coahran, January 2008
Revised: Jerod Weinman, December 2008