0% found this document useful (0 votes)
21 views9 pages

CH 7 Exercises

The document contains instructor materials for the Linux Essentials 2nd Edition, focusing on exercises related to managing files in the Linux File Hierarchical System. It includes detailed steps for navigating directories, creating subdirectories, and copying files using the Fedora Linux distribution. Each exercise aims to teach essential commands and concepts for effective file management in Linux.

Uploaded by

oscarjmatos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

CH 7 Exercises

The document contains instructor materials for the Linux Essentials 2nd Edition, focusing on exercises related to managing files in the Linux File Hierarchical System. It includes detailed steps for navigating directories, creating subdirectories, and copying files using the Fedora Linux distribution. Each exercise aims to teach essential commands and concepts for effective file management in Linux.

Uploaded by

oscarjmatos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

9781119092063 Linux Essentials Instructor Materials – Exercises

Linux Essentials, 2nd Edition, Exercises


Please see the Linux Essentials 2nd Edition Instructor
Materials: Introduction Instructor Letter and Syllabus for guidelines
associated with the following exercises.

Chapter 7: Managing Files

Exercise 7.a: Navigating through the Linux File Hierarchal System (FHS)
(Objectives 2.3 & 4.3)

Linux Distribution: Fedora (non-root user & password needed)


Desktop Environment: GNOME 3

1. If you have not already done so, boot up your computer (or virtual machine)
to start the Fedora Linux distribution.
2. When the machine has booted up, access the tty2 virtual terminal by
pressing Ctrl+Alt+F2.
3. Log on to a regular user’s account by typing in the username at the login
prompt and entering the user account’s password at the Password prompt.
4. At the command prompt, type pwd and press Enter. The pwd command
shows your present working directory (in this case, it is your home directory).
Any commands you enter in your current or present working directory affect
the commands outcome. Record your home directory: _____________
5. To make it easier to see your files in the tty2 terminal, type unalias ls and
press Enter. This will “turn off” any color coding of directories.
6. To see the files in your present working directory, type ls and press the Enter
key. The ls command lists files in your "present working directory" only if
you do not specify a directory.
7. To see how your present working directory affects the outcome of
commands, change your present working directory to a new location by
typing cd /etc and press Enter. The cd command allows you to change
your present working directory. In this case, your present working directory is
now the /etc directory (the location of several configuration files).
8. Type pwd and press Enter. Do you see /etc displayed? You should.
9. Type ls and press Enter. You should see different files than you did in step
#6. You see different files because your present working directory is different
than it was in step #6.
10. Type cd and press Enter to go back to your home directory. The cd
command with nothing entered after it is a shortcut to change your present
working directory to your home directory.
11. Type pwd and make sure you are in your home directory. Your home
directory should be the same directory you were in during step #4.
12. You can see the type of files in your home directory by using the –F option
to the ls command. Type ls -F and press Enter. You should see several
files followed by a forward slash (/). This indicates the file is a directory or, in
this case, a subdirectory.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Continued on next page

Exercise 7.a: Navigating through the Linux File Hierarchal System (FHS)
(Objectives 2.3 & 4.3) (continued)

13. You can see a different file type by typing ls -F /usr/bin/uname and
pressing Enter. An asterisk after a file means it is executable (can run as a
program).
14. You can see another file type by typing ls -F /etc/rc5.d/ and pressing Enter
(several files will show). A @ after a file means it is a symbolic link.
15. There are dot files (aka hidden files) in your home directory. Look at them by
typing in ls –a and pressing Enter. A dot file, also called a "hidden file,"
starts with a dot (period) and is not displayed unless the -a option is used
with the ls command.
16. Now just type in ls and press Enter. You should not see the dot (hidden) files
in this listing. This happens because you need to use the –a option on the
ls command in order to see them.
17. Now try a long listing, by typing in ls –l and pressing Enter. The –l option is
a lowercase L and not a number one. Don't worry about all the information
shown; you will be learning about this information in later chapters.
18. Take a look at the items at the root (/) filesystem by typing ls / and pressing
Enter. Can you tell whether these are directories or not? (It’s okay if you can’t
tell. You’ll be able to tell in the next step.)
19. Add an indicator to the end listed item by typing ls -F / and pressing Enter.
Remember that the forward slash (/) indicator tells you that the item is a
directory, whereas a blank indicates the item is a file.
20. Now you will try something different and attempt to change your present
working directory to various places across the filesystem using relative and
absolute directory references. First navigate to the /bin directory by typing
cd /etc and press Enter. Entering a directory or file reference starting with a
forward slash (/) is called using an "Absolute Directory Reference."
21. Now see where your present working directory is currently located by typing
in pwd and pressing Enter. This indicates your present working directory is
now at the /bin directory.
22. Use another absolute directory reference to take you back to the user
account’s home directory by typing cd directory and pressing Enter, where
directory is your account’s home directory, which you recorded in step #4.
Remember that an absolute directory reference ABSOLUTELY starts with a
forward slash (/).
23. Check that you are in your account’s home directory by typing in pwd and
pressing Enter.
24. Try using a relative directory reference to enter a subdirectory under your
account’s home directory. Type cd Downloads and press Enter. (Note: if
your home directory does not have a subdirectory named Downloads, then
pick another subdirectory of your choice).
25. Check that you are in the Downloads directory (or the other directory you
chose) by typing in pwd and pressing Enter.
26. Use another method to take you back to your home directory by typing: cd ~
and pressing Enter. The symbol after the cd command is a tilde (~) symbol.
The tilde (~) symbol is typically located above the Tab key on your keyboard.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.a: Navigating through the Linux File Hierarchal System (FHS)
(Objectives 2.3 & 4.3) (continued)

27. Check that you are in your home directory by typing in pwd and pressing
Enter.
28. Now you will navigate up one directory level using a relative directory
reference. Type cd .. and press Enter. Yes, that is two dots or periods (..)
after the cd command. Don’t leave them out or this will not work! The two
dots or periods (..) are a relative directory reference meaning “one directory
level up.” This is sometimes called the “parent directory.”
29. See where you are by typing pwd and pressing Enter.
30. Now try something a little different: use just one dot (or period) with the cd
command. Type cd . and press Enter. Yes, that is one dot or period (.) after
the cd command. Don’t leave it out or this command will not work!
31. See where you are by typing pwd and pressing Enter. You stayed in the
exact same place! This is because the single dot or period (.) is a relative
directory reference meaning “current directory.”
32. Now navigate to your home directory using another relative directory
reference. Type cd $HOME and press Enter. Be sure to use the correct
case (Yes, $HOME is all upper-case letters) or this command will not work
correctly. $HOME is an environment variable that points to your current
home directory.
33. Check that you are in your home directory by typing in pwd and pressing
Enter.
34. Now try to navigate up two directory levels using a relative directory
reference. Type cd ../.. and press Enter.
35. Type pwd and press Enter to see where your present working directory is
currently located.
36. Use the cd command to navigate to your account’s home directory using
one of the four methods explored in this exercise.

Exercise 7.b: Creating and Using New Subdirectories (Objective 2.4)

Linux Distribution: Fedora (non-root user & password needed)


Desktop Environment: GNOME 3

1. If you have not already done so, boot up your computer (or virtual machine)
to start the Fedora Linux distribution.
2. When the machine has booted up, access the tty2 virtual terminal by
pressing Ctrl+Alt+F2.
3. Log on to a regular user’s account by typing in the username at the login
prompt and entering the user account’s password at the Password prompt.
4 At the command prompt, type pwd and press Enter. The pwd command
shows your present working directory (in this case, it is your home directory).
Record your home directory: ___________________
5. Type ls -F and press Enter. You should see several file names with a / at the
end of their names, indicating that they are subdirectories.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.b: Creating and Using New Subdirectories (Objective 2.4)


(continued)

6. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type in mkdir HomeDirectory/NewDir and press Enter. This will
create a new subdirectory in HomeDirectory called NewDir. You are using an
absolute directory reference with the mkdir command to create the new
subdirectory.
7. Type ls -F and press Enter. You should see your newly created
subdirectory.
8. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type cd HomeDirectory/NewDir and press Enter. This will
change your present working directory to the new subdirectory. Notice that
you used an absolute directory reference with the cd command.
9. Type ls -F and press Enter. You should not see any files in the new
subdirectory you just created. Why? You haven’t put any files in there yet!
10. Type touch MyFile and press Enter. The touch command either creates
an empty file or updates the access and modification time stamps of a file. In
this case, you just created a blank empty file called MyFile.
11. Type ls -F and press Enter. You should now see the file you just created,
called MyFile in the new subdirectory.
12. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type in ls -F HomeDirectory/NewDir and press Enter. You
should see the same file listed as in the preceding step. In the preceding
step, you looked at the files in your present working directory. In this step,
you used an absolute directory reference to see files listed there.
13. Type cd .. and press Enter. (Don’t miss those two periods after the cd
command, or this step won’t work.) This will move your present working
directory from the new subdirectory up one level to your HomeDirectory.
14. Check that you are back at your HomeDirectory by typing pwd and pressing
Enter. You should see the same directory that you recorded in step #4. If you
don’t, use your cd command to put your present working directory back to
HomeDirectory.
15. Warning: This next command will generate an error. At the command prompt,
type mkdir Stuff/MoreStuff and press Enter. You should get an error
message similar to: “mkdir: cannot create directory ‘Stuff/MoreStuff’: no such
file or directory.” This mkdir command attempts to create a subdirectory
called Stuff and another subdirectory within Stuff called MoreStuff.
However, a subdirectory cannot be created if a parent directory does not
exist. The mkdir command knows the Stuff subdirectory does not
exist yet, so it refuses to create anything.
16. Type mkdir -p Stuff/MoreStuff and press Enter. The –p option forces the
mkdir command to create both the Stuff subdirectory and the
MoreStuff subdirectory. Notice that you used a relative directory
reference to create the HomeDirectory/Stuff/MoreStuff
subdirectories.
17. Type ls -RF Stuff and press Enter. The MoreStuff subdirectory should
display.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.b: Creating and using new subdirectories (Objective 2.4)


(continued)

18. Warning: The next command will generate an error. Type rmdir Stuff and
press Enter. You should get an error message similar to “rmdir: failed to
remove ‘Work’: Directory not empty.” This rmdir command attempts to
delete a subdirectory. However, if the directory contains subdirectories
and/or files, it will not work, as demonstrated here.
19. Type rmdir Stuff/MoreStuff and press Enter. You got no error messages!
Why? Because the subdirectory MoreStuff is empty. Notice you used a
relative directory reference to remove the subdirectory MoreStuff.
20. Type rmdir Stuff and press Enter. You got no error messages, because the
subdirectory Stuff is now empty (you deleted the subdirectory contents in
the preceding step.)
21. Next you will attempt to use the rmdir command to delete the NewDir
subdirectory you created earlier. Do you think this will work? Using the home
directory you recorded in step #4 for HomeDirectory in this command, type
rmdir HomeDirectory/NewDir and press Enter. It did not work. Do you
know why? (Look back at step #10 if you need some help answering this
question).
22. Remove both the MyFile file from NewDir and the directory by typing
the command rm -iR HomeDirectory/NewDir and pressing Enter. When
the command asks something similar to “descend into directory?” type y and
press Enter. Type y and press Enter for each item it asks to remove. This will
remove the NewDir subdirectory and all its contents.
23. Type ls -F and press Enter. Is the NewDir subdirectory gone?

Exercise 7.c: Copying Files (Objective 2.4)

Linux Distribution: Fedora (non-root user & password needed)


Desktop Environment: GNOME 3

1. If you have not already done so, boot up your computer (or virtual machine)
to start the Fedora Linux distribution.
2. When the machine has booted up, access the tty2 virtual terminal by
pressing Ctrl+Alt+F2.
3. Log on to a regular user’s account by typing in the username at the login
prompt and entering the user account’s password at the Password prompt.
4 At the command prompt, type pwd and press Enter. The pwd command
shows your present working directory (in this case, it is your home directory).
Record your home directory: ___________________
5. Type touch chores and press Enter. This will create an empty file in your
present working directory (pwd) called chores.
6. Type ls chores and press Enter. You should see the chores file listed.
7. Type cp chores chores.bck and press Enter. This will create a copy of the
file chores and give it a new name, chores.bck.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.c: Copying Files (Objective 2.4) (continued)

8. Type ls chores* and press Enter. You should see the files chores and
chores.bck.
9. Type mkdir Todo and press Enter. This will create a subdirectory in your
pwd called Todo.
10. Type ls -F and press Enter. You should see the subdirectory Todo.
11. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type cp chores HomeDirectory/Todo/ and press Enter. This
will copy the file chores into the subdirectory you just created, Todo.
12. Type ls Todo and press Enter. You should now see a copy of the file
chores in the subdirectory Todo.
13. Type cp chores Todo/done and press Enter. This will copy the file
chores from your pwd to the subdirectory, Todo, and rename the file to
done.
14. Type ls -F Todo and press Enter. The file done should be there.
15. Type cp -R Todo NewTodo and press Enter. This cp command with the
–R option will copy an entire subdirectory and all of its contents to a new
subdirectory. In this case, you are copying the Todo subdirectory and all of
its files to a new subdirectory called NewTodo.
16. Type ls -F Todo and press Enter. Note the files located here.
17. Type ls -F NewTodo and press Enter. Note the files located here. Do they
match the files in the preceding step? They should, because you copied
them over into this new subdirectory in step #15.
18. Type rm -Ri NewTodo and press Enter. Type y and press Enter to all the
questions asking if it is okay to descend into the directory, delete the files,
etc. Remember, the rm command with the –R option descends down into
the subdirectory to delete all the files, and the –i option asks you if it is okay
to delete the files/directories.
19. Type rm -Ri Todo and press Enter. Type y and press Enter to all the
questions asking if it is okay to descend into the directory, delete the files,
etc.
20. Type rm -i chores* and press Enter. Type y and press Enter to the
question asking if it is okay to delete the files.

Exercise 7.d: Moving Files (Objective 2.4)

Linux Distribution: Fedora (non-root user & password needed)


Desktop Environment: GNOME 3

1. If you have not already done so, boot up your computer (or virtual machine)
to start the Fedora Linux distribution.
2. When the machine has booted up, access the tty2 virtual terminal by
pressing Ctrl+Alt+F2.
3. Log on to a regular user’s account by typing in the username at the login
prompt and entering the user account’s password at the Password prompt.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.d: Moving Files (Objective 2.4) (continued)

4 At the command prompt, type pwd and press Enter. The pwd command
shows your present working directory (in this case, it is your home directory).
Record your home directory: ___________________
5. Type touch math and press Enter. This will create an empty file called
math.
6. Type ls math and press Enter. You should see the file math located in
your pwd.
7. Type mv math algebra and press Enter. This will rename the file math to
algebra.
8. Type ls -F and press Enter. The file named math should not be shown,
because it has been renamed (using the mv command) to algebra. Of
the two files, only the file named algebra should be shown.
9. Type mv algebra gym and press Enter. This will rename the file
algebra to gym.
10. Type ls -F and press Enter. The file named algebra should not be
shown, because it has been renamed (using the mv command) to gym. Of
the two files, only the file named gym should be shown.
11. Type mkdir School and press Enter. This will create the subdirectory
School in your pwd.
12. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type mv gym HomeDirectory/School/ and press Enter. This
will move the file gym to the subdirectory School.
13. Type ls -F and press Enter. This will list all the files and subdirectories in
your pwd. The file gym should be gone, because you moved it into the
subdirectory School.
14. Type ls -F School and press Enter. The file gym should now be showing,
because you are listing out the contents of the subdirectory School where
you moved the file gym to.
15. Type touch lunch and press Enter. This creates a blank empty file called
lunch in your pwd.
16. Type mv lunch School/ and press Enter. This does NOT rename the file
lunch to School, because School is a subdirectory in your pwd.
Instead, this moves the file lunch to the subdirectory School.
17. Type touch reading and press Enter. This creates a blank empty file called
reading in your pwd.
18. Type mv reading School/history and press Enter. This step moves the file
reading( to the sudirectory School and then renames it to
history.
19. Using the home directory you recorded in step #4 for HomeDirectory in this
command, type ls HomeDirectory/School and press Enter. You should see
three files located in the subdirectory School and they should be: gym,
history, and lunch.
20. Type mv School College and press Enter. This renames an entire
subdirectory named School to College.

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.d: Moving Files (Objective 2.4) (continued)

21. Type ls -F College and press Enter. You should see that the three files
gym, history, and lunch are now located in the subdirectory
College.
22. Type ls -F School and press Enter. You should get an error message on
this step. That is because this subdirectory no longer exists. You renamed
School to College in a preceding step—therefore the subdirectory
School no longer exists.
23. Type rm -Ri College and press Enter. Type y and press Enter to all the
questions asking if it is okay to descend into the directory and/or delete the
files. Remember, use the rm command with the –R option to descend down
into the subdirectory College to delete all the files, and the –i option to
ask you if it is okay to delete the files/directories.

Exercise 7.e: Exploring Case Sensitivity and File Globbing (Objective 2.4)

Linux Distribution: Fedora (non-root user & password needed)


Desktop Environment: GNOME 3

1. If you have not already done so, boot up your computer (or virtual machine)
to start the Fedora Linux distribution.
2. When the machine has booted up, access the tty2 virtual terminal by
pressing Ctrl+Alt+F2.
3. Log on to a regular user’s account by typing in the username at the login
prompt and entering the user account’s password at the Password prompt.
4 At the command prompt, type pwd and press Enter. The pwd command
shows your present working directory (in this case, it is your home directory).
Record your home directory: ___________________
5. Type touch fall.dat and press Enter. The “ll” in the file name is two
lowercase L’s (not number ones). This will create an empty file called
fall.dat.
6. Type touch fell.dat and press Enter. This will create an empty file called
fell.dat.
7. Type touch fill.dat full.dat and press Enter. This will create two empty
files called fill.dat and full.dat at the same time!
8. Double check that all four of the files were created. Type ls -F and press
Enter. If you do not see the four files, you will need to add the missing file(s).
Otherwise, this exercise will not work properly.
9. To view only the four files, you will need to use a wildcard symbol. Type
ls f*.dat and press Enter. By using a wildcard symbol, such as the asterisk
(*), you are creating what is called a “filter.” Using a filter along with
commands (such as ls) allows you to “filter out” files or directories you do
not wish to perform the command upon. The asterisk (*) used as a filter in
this command says, “Only show me the files that start with an f and end with
.dat, but have nothing in between.”

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com
9781119092063 Linux Essentials Instructor Materials – Exercises

Exercise 7.e: Exploring Case Sensitivity and File Globbing (Objective 2.4)
(continued)

10. There is another way to view only the four files; you will need to use a
different wildcard symbol. Type ls f?ll.dat and press Enter. The question
mark (?) can also be used as a wildcard character. However, unlike the
asterisk, it is used to wildcard only one character. Used as a filter in this
command, the question mark matches files with any character at the second
character position in the file name.
11. Another way to view only the four files is to use brackets. Type ls f[a-z]ll.dat
and press Enter. You should again see these four files. The brackets
represent a single character position and give you multiple options for
wildcarding. In this case, the brackets were used along with a range of
potential choices for a single character in that position. The range was any
character from a to z.
12. You can also use the brackets to narrow the choices (filter) of files. Type
ls f[ae]ll.dat and press Enter. Here you only found files that had either an a
or an e in the single character position.
13. Just like commands, file and directory names are case sensitive. Try this out
by first using a command that will work. Type ls fall.dat and press Enter.
You should see this file listed.
14. Now try a command that won’t work and shows how file names are case
sensitive. Type ls Fall.dat and press Enter. You should not see this file
listed. This is because you used a capital F in the file name instead of a
lowercase f. File and directory names are case sensitive. In other words,
Linux treats fall.dat and Fall.dat as two different and distinct
files.
15. Now try to delete the files you created in this exercise, using file globbing.
Type rm -i f[aeiu]ll.dat and press Enter. Type y and press Enter to all the
questions asking if it is okay to delete the files. Notice that it is okay to squish
together the various selections for the single character position. (And
yes...you could have just used an asterisk instead, but this helps to reinforce
a new concept.)

Linux Essentials, 2nd Edition, Ancillary Materials by Richard Blum and Christine Bresnahan. Copyright 2015 SYBEX Inc.
All Rights Reserved. http://www.sybex.com

You might also like