Laboratory: Getting Started with Linux

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 directory called "compsys" in our last lab session. Can you determine exactly what time you created it?

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 your project 0 directory:

ls -l ~/compsys/projects/00/

Or look at the files that I have shared with you:

ls -l ~davisj/share/

Exercise 3: 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/compsys/projects

Verify your new location and list the contents of the directory.

Now move to the 00 directory. (For a challenge, do this by specifying a relative path name for the 00 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 4: 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 Xor.hdl. You could name the copy Xor-backup.hdl if you like.

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

Now copy the file called you just saw in my share 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 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 (because we will use this file in the next exercise).

Exercise 5: 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:
Note that you can move forward and backward in the file by pressing the Page Up and Page Down keys.

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 or a binary executable file 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 6: 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 "linux-tutorial" 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 4 davisjan mathfac 4096 Jan 25 10:19 csc105
drwxr-xr-x 2 davisjan mathfac 4096 Nov 19 2011 csc211
-rw------- 1 davisjan mathfac 0 Jan 25 10:20 fileA.txt

Exercise 7: 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 Use the mv command to rename it hello.odt (and check your work).

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

  mv linux-tutorial/
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, "linux-tutorial/". Note that it includes both a directory name and a "base" file name ( 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/compsys/projects/00/linux-tutorial/ In the example, we used a relative reference to the file, shortening the name to linux-tutorial/ relative to the current directory "/home/username/compsys/projects/00/".

Thus, what you really just did in this example is to change the file's name from the original (/home/username/compsys/projects/00/ to a new name in a new location (/home/username/compsys/projects/00/linux-tutorial/ 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 8: 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 Xor-backup.hdl from /home/username/compsys/projects/00/ 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 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 davisjan mathfac    9 Jan 31  2011 fileA.txt
-rw-rw-rw- 1 davisjan mathfac 9 Jan 31 2011 fileB.txt
-rwxr-xr-x 1 davisjan mathfac 31 Jan 25 14:14 greeting
The owner of each of these files is davisjan 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 davisjan mathfac    2 Jan 25  14:16 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 ~davisjan/share/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?
  15. If your neighbor still cannot read your file, then examine the permissions for your home directory using the command ls -ld
  16. Give members of your group permission to read and execute your home directory. (Do not give anyone else permission to write your home directory - that's dangerous!)
  17. Ask your neighbor once again to try running and reading the greeting file.

For those with extra time

Read and execute permissions operate a little differently on directories than they do with files.
  1. Copy the message.txt file to your linux-tutorial directory and double-check that it has user (owner) read permissions. If you need help on this step, refer to the exercises 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 message.txt (e.g., with the command
      cat linux-tutorial/message.txt
    Note you will not be changing the permissions of message.txt.
  3. Here's something even more interesting. See if you can find a set of permissions for your linux-tutorial 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 cat command as above).

Janet Davis ( with thanks to Marge M. Coahran and Jerod Weinman

Created January 21, 2016
Last revisedJanuary 17, 2018, 07:29:10 AM PST
CC-BY-NC-SA This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.