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 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.)
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.)
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 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/
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?
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 helloworld.py 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 helloworld.py 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 helloworld.py (because we will use this file in the next exercise).
less helloworld.pyNote 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.
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
In the meantime, let us practice renaming files. At this point, you should have a file named helloworld.py. Use the mv command to rename it hello.odt (and check your work).
Next, move hello.py
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 hello.py linux-tutorial/hello.pyWhy 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/hello.py
". Note that it includes both a
directory name and a
"base"
file name (hello.py
). 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/hello.py. In the example, we
used a
relative reference to the file, shortening the name to linux-tutorial/hello.py
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/hello.py) to a new name in a new location (/home/username/compsys/projects/00/linux-tutorial/hello.py). 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 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.
-rw------- 1 davisjan mathfac 9 Jan 31 2011 fileA.txtThe owner of each of these files is davisjan and the group the file belongs to is mathfac.
-rw-rw-rw- 1 davisjan mathfac 9 Jan 31 2011 fileB.txt
-rwxr-xr-x 1 davisjan mathfac 31 Jan 25 14:14 greeting
What is the owner and group of the files in your home directory?
In addition, each file has three types of permissions:
drwxrwxrwx 1 davisjan mathfac 2 Jan 25 14:16 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 ~davisjan/share/greeting .
chmod ugo-rwx greetingCheck that this removed all permissions from your file by using the ls -l command.
chmod ugo+r greetingWhat effect does this have? (check using ls -l)
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?
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. cat linux-tutorial/message.txtNote you will not be changing the permissions of message.txt.
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). Created January 21, 2016
Last revisedJanuary 17, 2018, 07:29:10 AM PST
This work is licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License.