Summary: In this laboratory you will continue exploring the functionality provided by several utility programs and the bash shell.
Prerequisites: GNU/Linux I
a. Log in to the Linux system.
b. Open a terminal window.
c. Make sure that you are in your home directory.
Here is a list of commands that are useful for organizing and
managing your files and directories on a Linux system. You already saw
pwd in the previous lab. In this
lab, you'll get some practice with the others.
listfiles and directories
||print working directory|
||change (your working) directory|
||copy a file or directory|
move(or rename) a file or directory
||remove a file (i.e., delete it)|
Make a directory called
lab2 with the following command:
b. Move to your new directory (i.e., make it your working directory) with the command:
c. Now check that you were successful by issuing the command to print your working directory.
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
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.
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. Check your answer experimentally.
i. Return to the lab directory with
j. Now delete the file as follows. Then print a listing of your files to verify.
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.
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.
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
Using this scheme, when you want to remove a file name, for example,
zebra.txt, you would type
mv zebra.txt ~/trash
Later, when you want to empty your trash, you would type
rm -rf ~/trash/*
If you don't already use such a scheme, please consider making a
trash directory now, and starting to use it!
Two more commands that can be quite useful for moving around the
file system are
let you jump back and forth between distant directories quickly
and easily. For example, suppose your current working directory
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:
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
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 the original directory to continue her work.
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.
a2ps -Pescher file
a2ps --sides=duplex file
|prints file to default printer (handles many standard file
same, for printer named escher (not readily available)
same, but double-sided
|displays jobs in print queue on default printer
same, for printer named escher
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.
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.
For a quick review, try
cat ~/.bash_profile. Later in this lab,
we will talk more about the contents of this file.
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
a new and improved version that allows backward paging as well.
If you haven't used it before, give
less a try with the
less ~rebelsky/share/linux/sciencefac.txt. You
should be able to use the
Page Up, and
Page Dn keys to move around in
the file. Type
q (for quit) when you want to
stop using less.
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 ~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
tail [OPTION]... [FILE].... The second set of elipsis
[FILE]) tells us that
tail accepts multiple
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.
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
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
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.)
Copy the following file into your csc161 directory:
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?
Attempt to read the
afile file in your partner's csc161
directory, perhaps by using
less. What happens?
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?
Attempt to read
afile in your partner's csc161 directory. What
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?
Attempt to read
afile in your partner's csc161 directory. What
Restore permissions so that your partner can list the files in your
csc161 directory and can also read
n. Summarize for yourself what this exercise has taught you about permissions, and please ask if you are not sure!
Finally, please make sub-directories in your csc161 directory called
homework. Make sure the permissions on the
homework directory are set so that others can not read the files
there. Set the permissions on your
labs directory however you
In the GNU/Linux system, there are 3 standard
files that are
always open and available for use, called
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.)
standard input by default means input from the keyboard
standard output by default means output to the screen
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.
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
Give the following command a try. (Think of the "greater than" symbol
as an arrow in this context, re-directing the output from
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
>> do? (If you want a hint, it
may be helpful to use both of these commands repeatedly and look at the
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
cat is actually optional. If we invoke
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
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
optional. (You should also be able to see that
cat can be given
multiple input files instead.)
Just as we can re-direct
stdout to a file, we can also
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.
January 2007 [Marge M. Coahran]
January 2008 [Marge M. Coahran]
Monday, 30 August 2010 [Samuel A. Rebelsky]
Monday, 24 January 2011 [Samuel A. Rebelsky]
Tuesday, 25 January 2011 [Samuel A. Rebelsky]
Wednesday, 26 January 2011 [Samuel A. Rebelsky]
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 Jan 31 10:50:01 2011.
The source to the document was last modified on Wed Jan 26 14:50:45 2011.
This document may be found at
A PDF version of this document may be found at