Summary: In this laboratory, you will explore some of the basic Linux commands.
Contents
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 browser, 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.)
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.)
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 -lHere 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/
lab1-test.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 iowazipcodes.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 iowazipcodes.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).
less iowazipcodes.txtNote that you can move forward and backward in the file by pressing the Page Up and Page Down keys. How many zip codes does Davenport have?
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 Downless 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.
lab2 within your
current directory. If you have any difficulty doing this, please ask
the instructor, class mentor, 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
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.odtWhy 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?
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.
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?
-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 greetingThe 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:
drwxrwxrwx 1 weinman mathfac 2 2008-12-31 14:14 mydirThe 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.
cp ~weinman/courses/CSC105/greeting ./
chmod ugo-rwx greetingCheck that this removed all permissions from your file by using the
ls -l command.
less to read the file?
chmod ugo+r greetingWhat effect does this have? (check using
ls -l)
less to read the file?
chmod u+w greetingWhat effect does this have on the permissions?
./greeting(Note that the leading characters
./ tell the computer to look in the current directory for the program.) Why do you think that happens?
chmod ugo+x greetingWhat effect does this have on the permissions?
chmod og-r greetingWhat effect does this have on the permissions?
~username/greetingwhere
username is your username, not theirs. What happens?
less? Why do you think that happens based on the
current permissions of your file?
greeting.
message.txt from ~weinman/courses/CSC105/ 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.
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
less lab2/message.txtNote you will not be changing the permissions of
message.txt.
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).
With thanks to Janet Davis for the iowazipcodes.txt and
message.txt files.