Download - Linux Introduction: Hands-On
Exercise Manual for Course 143
Linux® Introduction: Hands-On
143/MA/J.3/310/J.2
by Frank Schmidt
Technical Editor: Mikael Hasteus
© LEARNING TREE INTERNATIONAL, INC.All rights reserved.
All trademarked product and company names are the property of their respective trademark holders.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, or translated into any language, without the prior written permission of the publisher.
Copying software used in this course is prohibited without the express permission of Learning Tree International, Inc. Making unauthorized
copies of such software violates federal copyright law, which includes both civil and criminal penalties.
143-MA-i © All rights reserved. Not to be reproduced without prior written consent.
Exercise Manual Contents
Legend for Course Icons ..................................................................................................... iii
Hands-On Exercise 2.1: Configuring the Desktop ............................................................... 1
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation ............. 13
Hands-On Exercise 4.1: Using Pathnames and Working With Directories ........................ 21
Hands-On Exercise 4.2: Copying and Moving Files .......................................................... 29
Hands-On Exercise 4.3: Linking and Removing ................................................................ 39 Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash ......... 47
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access ......................... 57
Hands-On Exercise 6.2: Sharing Access to Files .............................................................. 65 Hands-On Exercise 7.1: Using find to Locate Files ........................................................ 75
Hands-On Exercise 7.2: Pattern Matching With grep ....................................................... 81
Hands-On Exercise 8.1: Creating Simple Pipelines .......................................................... 85 Hands-On Exercise 8.2: Working With sed and gawk ...................................................... 93
Hands-On Exercise 9.1: Editing a Text File With vim ....................................................... 99
Hands-On Exercise 10.1: Customizing the Shell ............................................................. 107
Hands-On Exercise 10.2: Customizing the Login Scripts ................................................ 113
Hands-On Exercise 11.1: Working With Shell Scripts ..................................................... 123
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs ................................ 133
Hands-On Exercise 14.1: Configuring Linux .................................................................... 141
Solutions to Exercises ..................................................................................................... 147
Solutions to Hands-On Exercise 2.1 ................................................................................ 149
Solutions to Hands-On Exercise 3.1 ................................................................................ 151
Solutions to Hands-On Exercise 4.1 ................................................................................ 155
Solutions to Hands-On Exercise 4.2 ................................................................................ 159
Solutions to Hands-On Exercise 4.3 ................................................................................ 163
Solutions to Hands-On Exercise 5.1 ................................................................................ 167
Solutions to Hands-On Exercise 6.1 ................................................................................ 171
Solutions to Hands-On Exercise 6.2 ................................................................................ 175
Solutions to Hands-On Exercise 7.1 ................................................................................ 179
Solutions to Hands-On Exercise 7.2 ................................................................................ 181
Solutions to Hands-On Exercise 8.1 ................................................................................ 183
Solutions to Hands-On Exercise 8.2 ................................................................................ 187
143-MA-ii © All rights reserved. Not to be reproduced without prior written consent.
Exercise Manual Contents (continued)
Solutions to Hands-On Exercise 9.1 ................................................................................ 189
Solutions to Hands-On Exercise 10.1 .............................................................................. 193
Solutions to Hands-On Exercise 10.2 .............................................................................. 197
Solutions to Hands-On Exercise 11.1 .............................................................................. 201
Solutions to Hands-On Exercise 12.1 .............................................................................. 209
Solutions to Hands-On Exercise 14.1 .............................................................................. 213
143-MA-iii © All rights reserved. Not to be reproduced without prior written consent.
Legend for Course Icons
Standard icons are used in the hands-on exercises to illustrate various phases of each exercise.
Major step
Warning
1. Action
Hint
Checkpoint
Stop
Question
Congratulations
Information
Bonus
143-MA-iv © All rights reserved. Not to be reproduced without prior written consent.
143-MA-1 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
Background The GNOME windowing system offers a default configuration consisting of a desktop with multiple workspaces, panels, and drop-down menus. These tools offer many routes to execute programs graphically. They may be personalized for ease of use and increased productivity. Objectives In this exercise, you will • Customize the GNOME desktop • Add a launcher • Configure the properties of the GNOME Terminal window • Save the configuration
Adding a launcher
1. Use the mouse to follow the menu path Applications | Internet |
Firefox Web Browser. Right-click this item and choose Add this launcher to the desktop.
A new launcher appears on the top panel. 2. Use this new launcher to start the Firefox application. Enter the URL http://ltreesrv.ltree.com to test connectivity
to the classroom server.
143-MA-2 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued)
The hypertext links access additional documentation available for browsing from the server.
Investigate the additional documentation outside of class time if desired.
3. Test the web service on your own host by entering the URL: http://ltreeN.ltree.com
N is your team’s number for this week. Your login name is userN and your workstation is ltreeN.
Your own host is also providing a Web site containing a famous e-mail from Linus Torvalds.
Customizing your preferences
4. Use the mouse to follow the menu path System | Preferences |
Screensaver. 5. Choose your desired screensaver. Clear the box labeled Lock screen when screensaver active.
No password will be required when the screen saver is active.
Click the Close button to save this choice.
143-MA-3 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
(continued)
GNOME terminal
6. Use the mouse to follow the menu path Applications | System
Tools | Terminal. Right-click this item and choose Add this launcher to the panel.
A new launcher appears on the top panel. 7. Use the new icon and start the GNOME Terminal program.
The Terminal window will be running a bash shell within it. 8. From this shell prompt, issue the hostname command.
a. Are you logged into your local machine, or into a remote machine via the network?
9. From the Terminal window’s menus, select Edit | Profiles select the
Edit button.
The Editing Profile box appears with tabs for the various portions of the profile to be changed.
Within the General tab, clear the box labeled Use the system fixed width font. This will make the Font drop-down list available.
143-MA-4 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued) 10. Click the current default font size. Choose Monospace from the
Family list, Regular from the Style list, and 14 from the Size list. 11. Verify that the font style chosen in the Preview box is readable for
you and your teammate.
If not, repeat the step above and choose a larger size.
When you are satisfied, click OK on the lower right.
You are returned to the Editing Profile box. 12. Remain within the General tab and clear the box labeled Allow bold
text. From the top, select the Scrolling tab. Change the Scrollback value to 1000 lines. Finally, click Close twice on the lower right.
The Terminal window’s Default profile has been customized. You will notice the increase in font size has also resized the Terminal window itself.
13. Move the Terminal window as needed by dragging the title bar.
The size of the Terminal window can be adjusted by dragging up, down, left, or right from one of the corner handles.
143-MA-5 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
(continued)
Saving the configuration
14. From System | Preferences | Startup Applications, select the
Options tab, then select Automatically remember running applications when logging out. Then click Close.
15. From the top panel, choose System | Log Out userN…
A dialog box appears asking for confirmation on the logout action.
After the logout is complete, the system will redisplay the login window.
16. Log in with your username and password.
Your Terminal window is open and ready.
Congratulations! You have completed the exercise.
143-MA-6 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued)
Further desktop customization
Adding another panel
17. Position the mouse over either the top or bottom horizontal panels. Right-click to invoke the menu, then select New Panel from this menu.
A new, empty panel will appear on the right side of the screen.
18. Position the mouse over the newly created panel, right-click to invoke the menu, and select Properties.
From the Panel Properties box in the General tab, make the following customizations:
Change the Orientation to Left via the drop-down box Increase the Size to 30 pixels Select from the checkboxes:
Expand Show hide buttons Arrows on hide buttons
Finally, click the Close button near the lower right.
The panel should then be placed on the left side of your desktop, with Arrow buttons on either end to hide it.
If any of these configurations failed, you may repeat this step to change the panel’s properties.
143-MA-7 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
(continued)
19. Right-click the Firefox Web Browser launcher on the top panel. Clear the Lock to Panel box. Right-click again and choose Move. Move the launcher to the newly added left panel.
Copying and pasting between GNOME Terminals
20. Open two GNOME Terminals. 21. In one Terminal, log in to ltreesrv via Secure Shell using:
ssh ltreesrv Enter your login password, ltree, when prompted.
22. In that same Terminal, issue the hostname command. 23. In that same Terminal, use the mouse to highlight the text
hostname.
Select Edit | Copy. 24. In the other Terminal, select Edit | Paste, then press <Enter>, if
needed, to execute.
You have taken a command run on one system and executed it again on another system.
You can usually paste any selected text using the middle mouse button without using the menus.
25. Use exit to terminate the ssh session on ltreesrv.
143-MA-8 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued)
Using tabs in GNOME Terminals
26. In either GNOME Terminal, select File | Open Terminal. 27. Select File | Open Tab.
b. What is the difference between a new tab and a new Terminal?
c. Were they both opened with the same properties associated with the Default profile? Yes No
28. Close the Terminal that has two tabs.
Creating a new profile
29. From any Terminal, select File | New Profile. In the New Profile
dialog box, enter Color into the “Profile name” field. Click the Create button.
You are now placed in the Editing Profile window for the Color profile.
30. Choose the Colors tab. In the section titled “Foreground and Background, Bold and Underline” clear the box labeled Use colors from system theme.
143-MA-9 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
(continued)
You can now change the foreground and background for this profile. Click the button labeled Text color. You may pick different palettes by clicking the color wheel. Then select an appropriate foreground color by clicking in the triangle.
When you have decided on the foreground color, click OK at the bottom.
31. Set the Terminal window’s background color. Click the button labeled
Background color, then follow the steps as above.
32. When both foreground and background colors are satisfactory, click the Close button on the lower right in the Editing Profile window.
d. What profile is currently in use?
Using multiple profiles
You can change the active profile using the Terminal menu. You can select the profile of choice for new Terminals or tabs through the File menu.
33. Open a second tab within the same Terminal window using the
Color profile.
The two tabs are using different profiles.
143-MA-10 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued)
Another method to add launchers
34. Right-click the left panel itself, then select Add to Panel, the first
item from the drop-down menu.
A selection menu, “Add to Panel,” appears. 35. From the selection list, click the second choice, Application
Launcher, then click the Forward button near the bottom right corner.
Scroll down through the categories until you locate Accessories. Click the small triangle to the left of this text.
This will cause the triangle to rotate 90 degrees, indicating the next level of choices is displayed.
Scroll down through this list to locate gedit Text Editor, select this item, then click the Add button near the bottom right corner.
GNOME comes with gedit, a powerful graphically based text editor that we will explore later in the class.
The new launcher should appear on the left panel.
Click the Close button near the bottom right corner.
143-MA-11 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop
(continued)
Using multiple workspaces
The GNOME desktop supports multiple workspaces. Each of these workspaces can have its own set of open applications.
36. Notice the two rectangles in the lower right corner of the bottom panel. Click each of these to move between workspaces.
e. Are the customized panels available on each workspace? Yes No f. Are the same applications open on each workspace? Yes No
37. Return to the original workspace. 38. Right-click the desktop background itself. Select Change Desktop
Background from this menu. Scroll through the list of desktop wallpaper to locate one you like. Select it and then close the Desktop Background Preferences box.
g. Is the desktop background the same on all workspaces? Yes No
39. Take advantage of multiple workspaces by opening different applications in each workspace.
143-MA-12 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 2.1: Configuring the Desktop (continued)
Further customization
40. If you still have time remaining, you may want to further customize your GNOME session.
143-MA-13 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation
Background The file, ls, and less commands are used to discover files and investigate their contents. We will follow a series of steps to read some file contents. We will also investigate the online documentation for these useful commands. Objectives In this exercise, you will • List filenames using ls • Determine which are the names of text files • Display the contents of text files • Use man and info to learn about other helpful options for these
commands • Use bash history to increase efficiency on the command line
Listing filenames
Before we can less the contents of any file, we must first discover the file’s name, then decide if the file contains text.
1. Begin with a simple ls command to discover the contents of the working directory.
Notice the names f1, d1, and s1. We want to investigate these further.
143-MA-14 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation (continued)
Determining text files
2. Use the file command on each name, d1, f1, and s1, to discover
their types.
a. What type of entity is d1?
b. What type of entity is f1?
c. What type of entity is s1?
Displaying contents of text files
3. Use less to explore the contents of f1.
Within less, find all the occurrences of the text pattern FAILURE.
You may search for patterns in less using /pattern and then repeat the search using n. Exit less when you’ve reached the end of the file.
Exit less using the quit command, q.
143-MA-15 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation
(continued)
Using man and info
4. Documentation on the same command may vary. We will try to
locate the same information from multiple sources and compare the results.
We will examine the options -C (uppercase C) and -x (lowercase x) that the ls command supports.
Record the meaning of the –C and –x options
Use ls --help
You may have to use the scrollbars to review all of the output. 5. Use man ls to browse its manual page. Use the less search
features to search for the option information desired.
You may also search for patterns in man using /pattern. 6. Use info ls to begin browsing the document. Use the
<Down arrow> to reach the Menu section. Descend to the subtopic, by pressing <Enter> on the line:
* General output formatting
When you have read about the two options, enter q to quit.
143-MA-16 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation (continued) 7. Try the ls command twice, first with the –C option, the second time
with –x
File names appear in ASCII order, first in columns, then in lines.
d. What command displays a file’s content type?
e. What command(s) would display syntax usage information about the command date?
f. How would you find the string column when using less or man?
Congratulations! You have completed the exercise.
143-MA-17 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation
(continued)
Investigating documentation
Common sense options
8. Use the --help option of file and read about the –b option, also
known as --brief. 9. Use the file command with the -b option and three arguments: f1,
d1, and s1
g. Is this a useful option when using multiple arguments? Yes No
Shell scripts are executable text files
10. Review the output of file when s1 is the argument. It may seem
contradictory.
h. Is it considered executable? Yes No i. Is it considered text? Yes No
143-MA-18 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation (continued) 11. Use less to peruse the contents of s1
j. What are the contents of shell scripts?
k. What symbol denotes a comment?
Comparing info and man
Using an incorrect argument to info or man gives very different results.
12. Use man and info try to learn about the fictitious command called
bogus.
l. Did man open a manual page? Yes No m. Did info open an Info document? Yes No
Use quit, q, to exit the Info document about the info command.
13. Try to find bogus in the whatis database.
143-MA-19 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 3.1: Using the Command Line and Reading Documentation
(continued)
When all else fails, read documentation about documentation. 14. Search for man and info in the whatis database. 15. Read the Info document on info 16. Read the manual page on man 17. Read the Info document on the man command. 18. Read the manual page on the info command.
GNOME help
19. Open Help | Contents from a Terminal window to learn more about
GNOME Terminals. 20. Open System | Help and read about the GNOME Desktop.
143-MA-20 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-21 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories
Background Objects in the directory tree may be referenced with full or relative pathnames. Creating new directories will allow us to group related files into these new directories. Objectives In this exercise, you will • Navigate the directory hierarchy, tracking the working directory • Reference files and directories using full and relative pathnames • Create new directories
Navigating the hierarchy
1. Use cd to make sure your current directory is set to your home
directory. 2. Display the full pathname of your current directory using pwd 3. Change the current directory to /143 using cd /143 Verify the new current directory. 4. Use ls -l to discover the contents of this directory.
You should notice the lintronica file.
143-MA-22 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories (continued)
a. What is the full pathname to this lintronica file?
5. Change working directory to /etc Display the name of the current directory to verify that the previous
cd succeeded. 6. List the contents of the current directory.
You should notice the hosts file.
b. What is the full pathname to this hosts file?
7. Use less to view the contents of the hosts file. Search for your
computer’s hostname.
/etc/hosts contains hostname-to-IP address mapping for our network.
8. Return to your home directory using cd Display the name of the current directory to verify that you have
returned home.
143-MA-23 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories
(continued)
Using full and relative pathnames
9. Using less and a full pathname, display the contents of the same
lintronica file listed in Step 4.
The full pathname starts with /, followed by subdirectory 143, followed by the file name.
10. Without changing the working directory, use a full pathname to view
the contents of lintronica within the /home/user30 directory. 11. Without changing the current directory, use a relative pathname to
view the contents of the same lintronica described in the previous step.
c. With your home directory as your current directory, what is a relative pathname to /home/user30/lintronica?
Assuming a home directory of /home/userN, the relative pathname of../user30 is the home directory for user30.
143-MA-24 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories (continued)
Creating new directories
12. Return to your home directory. 13. Create a new subdirectory named logos using the command line
mkdir logos
14. While still in your home, use ls -l without arguments.
You should notice the new directory.
15. Obtain another listing with ls -l, using logos as the argument.
When a directory is given as an argument to ls, only the contents are listed, not the directory itself.
16. View the timestamp and other attributes of the newly created
directory using ls -ld logos
17. Examine the contents and attributes, including hidden names, using ls –al logos
The logos directory is not completely empty; it still contains the . and .. entries.
143-MA-25 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories
(continued)
d. Assume that /home/user30 is your current directory. What are the relative pathnames to the following? /home
/
/etc
/home/user29
Congratulations! You have completed the exercise.
143-MA-26 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories (continued)
Directory management
Full pathnames
18. Use a full pathname to list the contents of the /bin directory.
e. Were the files for the executable commands ls and hostname listed? Yes No
19. Make /tmp the new current working directory. 20. Repeat the directory listing from Step 18.
f. Was the same full pathname used? Yes No
21. From /tmp, use relative pathnames to list the contents of /bin 22. Use the file command to discover the content type of the
command file named ls within the /bin directory. 23. Display a directory listing of your home using a full pathname.
143-MA-27 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.1: Using Pathnames and Working With Directories
(continued)
Creating directories
24. From /tmp, using a single mkdir command, create two levels of
subdirectories under your home named history/facts
An option to create parent directories is required.
25. Create an additional directory named history/myths
The parent to myths already exists. 26. Change your working directory to the newly created directory
history, then use a relative pathname to list the contents of your home directory.
Your home directory is one level above. 27. Change your working directory to facts, then use a relative
pathname to list the contents of your home directory.
Your home directory is now two levels above. 28. Return to your home directory.
143-MA-28 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-29 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2:
Copying and Moving Files
Background Files and directories are fundamental building blocks of Linux. Carelessly copying files can actually lead to loss of data. Familiarity with cp, mv, and their associated options is critical to successfully managing your data. Much of the power of command line tools is managing files. As we’ll learn later in the course, these techniques can be automated by embedding the same commands in shell scripts. Objectives In this exercise, you will • Copy and move files • List attributes with ls • Use options to warn about overwriting and create backups • Compare file contents
Copying and moving files
1. Return to your home directory for this exercise if necessary. 2. Use ls -l to get a long listing of the contents of the /143 directory.
You should notice a file named history.txt. 3. Use ls to list the contents of your working directory and verify that
there is not a file named hist.txt in the current directory. 4. Using cp, full pathnames, and relative pathnames, copy the
/143/history.txt file to the file named hist.txt in the current directory.
143-MA-30 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files (continued)
The full pathname is needed for the source file; it must start with a forward slash, /.
5. Copy /etc/hosts to hosts in the current directory.
Verifying copies with long listings
6. Use ls -l to display a long listing the contents of your working
directory and verify the timestamps on the newly created hist.txt and hosts files.
Warning! Both hosts and hist.txt must exist in the current directory to continue the exercise.
Options for cp and mv
7. We will now attempt to copy a source file over an existing target file.
The backup option will make another copy of the target file. Use cp and its –b option and copy hosts over hist.txt
8. Display a long listing of the entire contents of the current directory.
a. Was a backup file named hist.txt~ created? Yes No
143-MA-31 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files
(continued)
9. Restore the original contents by moving the backup file back to its original name.
Use mv to rename hist.txt~ to hist.txt
Upon completion, the current directory should contain the original contents of hist.txt.
10. As in Step 7, we will purposely attempt to copy over an existing file.
The interactive option for cp will prompt for confirmation before overwriting. Attempt to copy, using the –i option, from hosts to hist.txt When prompted to overwrite the existing hist.txt file, respond by entering yes
11. Use diff to compare the current contents of hist.txt to hosts
b. Are these two files now identical? Yes No
143-MA-32 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files (continued)
Copying to directories
12. Using a single command, copy /143/history.txt and
/etc/hosts to the current directory.
Use . as the final argument.
13. List the contents of the current directory to verify the existence of the new history.txt and hosts files and the logos directory created in Hands-On Exercise 4.1.
14. Without leaving the home directory, copy the tux.jpg file from the
/143 directory into the logos directory.
The new copy should have the same name.
15. Copy the lti_logo.gif file from the /143 directory into the logos directory. Name the new copy logo.gif
A target pathname containing directory/file is required.
16. Display a long listing of the contents of the logos directory.
You should see the tux.jpg and logo.gif files only.
143-MA-33 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files
(continued)
c. What are the command lines to do the following?
Copy lintronica to lin.txt in the current directory:
Copy lintronica to lin.txt in the /tmp directory:
Move lintronica to lin.txt in the /tmp directory:
Congratulations! You have completed the exercise.
Additional copy and move techniques
Perform the following two steps only if you did not perform them in Hands-On Exercise 4.1, Bonus steps 24 and 25.
17. From your home, using a single mkdir command, create two levels
of subdirectories under your home named history/facts
An option to create parent directories is required.
18. Without leaving your home, create an additional directory named history/myths
143-MA-34 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files (continued)
The parent to myths already exists.
Working with directories
19. Copy the file named /143/famous_email into the newly created
history/facts directory. 20. Without leaving the current directory, copy /143/fables.txt to
history/myths/famous_email beneath your home directory. Confirm a successful copy by obtaining listings of history/myths and history/facts
They both contain a file named famous_email. 21. Use diff to compare the contents of the newly copied
history/facts/famous_email file to the history/myths/famous_email file.
They will differ. 22. Use diff to compare the contents of the two directories
history/facts and history/myths
143-MA-35 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files
(continued)
Exploring the –i and –b options further
23. From the home directory, attempt to copy history.txt over
hosts, using both the -i and -b options.
When prompted to overwrite, reply yes 24. Use diff to compare the current contents of these two files.
d. Are their contents the same or different?
e. Was the backup created? Yes No
25. Using mv, restore the original contents of hosts from its backup. 26. Repeat the diff from Step 24 to verify the restoration.
Their contents will now differ.
143-MA-36 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files (continued)
Answering the -i prompt
27. In the current directory, make your own backup copy of
history.txt, naming it orig.history.txt
Also make your own backup copy of linus, naming it orig.linus
Display a long listing of the attributes of these four files. Verify that the new files have the same size in bytes as the originals, but with new modification times.
28. Using only the –i option, copy history.txt over linus. At the
prompt for overwriting, respond with No Way
f. Was linus overwritten by the contents of history.txt? Yes No
29. Attempt the copy again, remembering to use –i. At the prompt for overwriting, respond with Please do
g. Did that response cause an overwrite? Yes No
30. Attempt the copy again, one last time, remembering to use –i. At the prompt for overwriting, respond with You bet
h. Did that response cause an overwrite? Yes No i. What conclusions can you draw about the possible responses?
143-MA-37 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.2: Copying and Moving Files
(continued)
The linus file now contains the same data as history.txt. 31. Use mv with the interactive option to rename orig.linus to
linus. Give a response to allow the renaming to occur. 32. Use less on linus, verifying that its contents have changed.
Learning more about cp and mv
33. Use man or info to learn more about cp
j. What is the use of the –-recursive option with cp?
34. Use man or info to learn more about mv
k. What is the use of the –-update option with mv?
143-MA-38 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-39 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing
Background The Linux directory hierarchy is composed of many levels and thousands of files. We want to explore some of the contents of some of the better-known directories and use linking to simplify access to files and directories in other branches of the hierarchy. Objectives In this exercise, you will • Create regular and symbolic links • Remove filenames and file contents
Linking into other directories
1. From your home directory, use ls –l to display a long listing of the
contents of /etc
a. Did you find files named passwd and hosts? Yes No
/etc/passwd is used to identify local users. /etc/hosts contains hostname-to-IP address mapping for our network. We viewed its contents in Hands-On Exercise 4.1.
2. Using ln, create a regular link to /etc/hosts named my_hosts
No options are required to make regular links.
143-MA-40 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing (continued) 3. Display a long listing, including the inode number, of my_hosts and
/etc/hosts
4. Use diff to compare the contents of my_hosts and /etc/hosts
There should be no difference.
5. Use rm to delete my_hosts Answer yes to any prompt about removing a write-protected regular
file. 6. Obtain a long listing of the /etc directory.
b. Did this removal affect /etc/hosts? Yes No
143-MA-41 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing
(continued)
Creating a symbolic link
7. Using ln –s, create a symbolic link in the current directory named
my_pw to /etc/passwd
The first argument is the existing name; the second is the name to be created.
8. Use ls –l, display a long listing of my_pw 9. Using the bash history mechanism, retrieve the previous command
line and insert an uppercase L into the option list, then execute.
The ls –lL my_pw long listing should follow the link, and display the attributes of /etc/passwd.
You can do a separate ls -l /etc/passwd if desired.
c. Do my_pw and /etc/passwd contain the same attributes? Yes No
10. Use less to explore the contents of my_pw
d. Does the less program follow the symbolic link to its target? Yes No
You should see a line with your own login name.
11. Use diff to compare the contents of my_pw and /etc/passwd
143-MA-42 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing (continued)
Contrasting links with rm and shred
12. Create a regular link named my_email to famous_email in the
current directory. 13. Obtain a long listing of the current directory, with inode numbers, to
confirm that the link was successful. 14. Use shred to overwrite the contents of my_email, the new name
created in Step 12.
15. Display the attributes of my_email and famous_email
Compare the contents of these two files.
e. Did shred affect famous_email? Yes No
16. Use rm to delete my_pw and my_email 17. Obtain a long listing of the /etc directory to verify that
/etc/passwd was not affected by the removal of my_pw
f. Do regular links share all the same attributes? Yes No g. Do symbolic links share all the same attributes as the name linked to? Yes No h. If a symbolic link is given as an argument, do all commands operate on the symbolic link itself? Yes No
143-MA-43 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing
(continued)
Congratulations! You have completed the exercise.
Making more use of links
Creating regular links
18. Copy the /143/history.txt file into the current directory, naming
the new copy h1 19. Create a regular link to h1 called h2
20. Review the attributes of h1 and h2
i. What is the link count?
21. Display the inode numbers of h1 and h2
j. Is the inode number the same for both? Yes No
22. Copy h2 to h3 Display a listing of attributes, including inode number, for h1, h2, and
h3
143-MA-44 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing (continued)
Symbolic links to directories
23. From the home directory, create two subdirectories named copies
and links 24. Create a symbolic link called active that is linked to the links
subdirectory.
Symbolic links can also be made to directories. 25. Move h1 and h2 from the current directory into active
k. Into which directory were the files actually moved?
26. Display a long listing of active
l. Were h1 and h2 listed within the contents of active? Yes No
27. Repeat the long listing from above, but add the L option to the command line.
This new long listing should follow the link and display the contents that are really beneath links.
143-MA-45 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 4.3: Linking and Removing
(continued)
m. Were h1 and h2 now listed within the contents of active? Yes No n. What effect would cd active have?
The output of pwd would show the symbolic link name within the full pathname.
28. From the home directory, remove the link named active Verify that the contents of the subdirectory links were not affected.
29. Recreate the symbolic link called active; this time it is to be linked
to the copies subdirectory. 30. Move h3 from the current directory into active
o. Into which directory was the file actually moved?
31. Display a long listing of active, including any option to follow the
symbolic link. 32. From the home directory, recursively remove active, links, and
copies
143-MA-46 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-47 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1:
Managing and Archiving Files—Using Nautilus and bash
Background The power of file-handling commands is enhanced by features of the shell. bash has a powerful history capability and filename completion to reduce repetitive typing on the command line. It also supports pathname expansion, using special symbols to reference groups of files as arguments to file-handling commands. Objectives In this exercise, you will • Use pathname expansion special characters to match files with
related names • Copy, move, and delete multiple files or directories • Manage files and directories graphically with Nautilus
Using bash from the command line to match multiple files
The following steps are to be executed from the command line. 1. Use cd to change your working directory to your home directory. 2. Use mkdir to create a subdirectory named log_test 3. Use cp -r /143/wildcard . to obtain a fresh copy of the
directory and its files.
Remain in your home directory for the following steps.
143-MA-48 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash (continued) 4. List the contents of the wildcard directory.
You will find the filenames that contain log and the filenames that end in day.
5. Obtain a long listing using ls –l and the special character * to list only those files in wildcard that start with log
Use a relative pathname: wildcard/file…
a. How many filenames started with the string log?
6. Obtain a long listing using ls –l and a different special character;
list only those files that start with log followed by exactly one character.
7. Use a single cp command to copy all the names beneath wildcard that start with log followed by a single character to the newly created log_test subdirectory.
The directory name must be the final argument on the command line.
8. Without changing your working directory, verify the copy worked by
displaying the long listing of log_test
143-MA-49 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash
(continued)
Moving multiple files
9. Use brace expansion to create two subdirectories under your home
directory named workdays and playdays
Brace expansion allows a series of strings to be used for the expansion.
10. Use the ls command to obtain a list of the files in the wildcard directory that end with the string day
The result should be five pathnames, one for each weekday. 11. Use the mv command and brace expansion to move only the files
named monday, tuesday, and wednesday into the newly created directory workdays directory.
12. Move the final two day files, friday and thursday, into the
playdays directory using a single mv command. 13. Obtain a listing of the wildcard directory.
The names ending in day are gone. 14. Use ls –ld *days to display a long listing of the names beneath
your home that end in the string days.
The –d is needed to see the attributes of the directories themselves.
143-MA-50 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash (continued)
Removing multiple files
15. Using rm –i and the special character *, delete selected filenames
under log_test as described below:
Answer no when prompted for log* and log1.
Answer yes for other prompts.
Using ~ and filename completion
16. Change your working directory to wildcard
Use ls -l ~ to display a long listing of your home directory.
The log_test directory is there.
17. Use filename completion to list the ~/log_test directory contents.
Begin the command line as ls –l ~/log_ and then press the <Tab> key to allow bash to complete the pathname.
18. Change working directory back to your home.
143-MA-51 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash
(continued)
b. What are the effects of the following commands? ls log[5-8]
rm log?
less /etc/{passwd,hosts}
cp –r /143/week* .
Congratulations! You have completed the exercise.
143-MA-52 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash (continued)
Using Nautilus
19. Open the Nautilus browser and view the contents of the home
directory by clicking the desktop icon. 20. Open the logos directory created in a previous exercise. 21. Open an additional Nautilus browser using Places | Computer.
Navigate by opening the Filesystem, then the 143 icons.
Two browser windows should now be open.
22. Copy the files /143/openoffice-writer.png and /143/dialog-warning.png into the logos directory by keeping the <Ctrl> key pressed while dragging.
Be sure to copy, not move.
The Copy and Paste selections from the Edit menu may also be used.
23. Verify that the files were placed inside this directory. 24. Close all Nautilus windows and open a terminal window if needed.
143-MA-53 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash
(continued)
Additional filename completion
25. From your home, create a directory named lin_test 26. We will attempt to have bash complete this directory name for a
listing.
Begin an ls -l command; for the filename argument list, enter the single letter l before pressing <Tab>.
This is the lowercase letter L, not the number 1.
c. Was the single letter l enough for a unique completion? Yes No
27. Continue to add single characters from the directory’s name, pressing <Tab> after each, until a unique completion is made.
Press <Enter> when the directory name has been completed.
28. Using filename completion, copy the famous_email file from the
home directory into lin_test 29. Using filename completion, obtain a long listing of the new contents
of lin_test 30. Using filename completion, recursively remove the directory
lin_test
143-MA-54 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash (continued)
More with special characters
31. Within a Terminal window, change working directory to wildcard
beneath the home directory. 32. Restore any missing contents by copying all files beneath
/143/wildcard to the current directory.
Remain in the wildcard directory for the following steps. Each step can be performed with a single command.
33. Create three subdirectories: log_errors, log_keepers, and day 34. Move all the filenames that contain at least one character followed by
day into the newly created day directory.
Be careful with the special characters; *day also matches the directory day itself. This is not exactly what we want.
35. Move all the filenames starting with log and followed by a single
digit into the log_keepers directory. 36. Move only log and log* into the log_errors directory.
loglist and the three subdirectories should be the only contents of wildcard. Return to your home directory.
143-MA-55 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash
(continued)
Deleting files with Nautilus
37. Open Nautilus and navigate to the directory logos. Right-click
tux.jpg and select Move to Trash. 38. Double-click the Trash icon on the desktop. 39. From the command line, display a long listing of the contents of the
.local/share/Trash/files subdirectory under your home.
This directory contains the contents of Trash. The files in .local/share/Trash/info are also needed to Restore.
40. Right-click on the tux.jpg icon and choose Restore.
If necessary, click Reload in Nautilus to update its view.
Reading image files and text files in Nautilus
41. Navigate into your logos subdirectory, where you will find the icons
for the image files copied there earlier.
Double-click each icon.
d. What program was started?
Look under Help | About.
143-MA-56 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 5.1: Managing and Archiving Files—Using Nautilus and bash (continued) 42. Close the program viewing images. 43. Use Nautilus to navigate to the /143 directory. Double-click the icon
for fables.txt.
e. What program was started?
44. Close the program using fables.txt. 45. In Nautilus, open Help | Contents to read more about the file
manager.
143-MA-57 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1:
Adjusting Permissions and Testing File Access
Background Files are more than just containers of data; they hold our organization’s information. A complete understanding of file access needs and restrictions is required. Objectives In this exercise, you will • Understand the meaning of the three basic permissions—read, write,
and execute—and how they are applied to files and directories • Change permissions on existing files and directories • Adjust the file creation mask for safe default permissions
Understanding existing permissions
1. Set the file creation mask using umask 007 2. We want to examine some permissions on existing system files.
Use ls –l to display the permissions of the file /etc/group
a. Who can read the contents of this file?
3. Use cp to copy /etc/group to the current directory; name the copy
permtest 4. Examine the permissions of the new copy, permtest.
143-MA-58 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access (continued)
b. Are they the same as the original file? Yes No
The new permissions are derived from the original file’s permissions and the umask value.
Adjusting permissions
5. Use chmod 000 to remove all permissions on permtest
Review the file’s new permissions. 6. Use less to display the contents of permtest
c. Could you read the contents of the file? Yes No 7. Attempt to make a copy from /etc/group to permtest again.
d. Did the copy work? Yes No
To copy over an existing file requires write permission on that file.
8. Though the permission set of 000 is very secure, it is not very useful.
Change the permissions on permtest so that: The owner of the file has read and write access The group has only read access There should be no access for others
9. Verify the success by listing the new permissions.
e. Could you now read the contents of the file using less? Yes No
143-MA-59 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access
(continued)
Setting the creation mask
10. Display the current creation mask using the umask command.
f. What permissions will not be assigned?
11. touch the file maskfile1, then list its permissions to verify your
answer.
The umask is the octal permissions not granted.
The new permissions are derived from the maximum relevant permissions, 666, and the umask value.
12. Set the umask value to 000, then confirm the new value. 13. Use mkdir to build an empty directory named maskdir1 14. Display a long listing of only the newly created file and directory.
143-MA-60 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access (continued)
The -d option to ls gives information about the directory given as an argument.
g. What are the numeric permissions on the newly created maskdir1?
h. What are the numeric permissions on the newly created maskfile1?
i. What umask value would be considered most restrictive?
15. Set the umask value back to the initial value of 002.
Congratulations! You have completed the exercise.
143-MA-61 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access
(continued)
A deeper look at permissions
16. Set the umask value to 777, and then confirm the new value. 17. Create an empty file named maskfile2 and an empty directory
named maskdir2 18. Display the permissions on these newly created names.
j. What are the permissions on maskfile2 and maskdir2?
19. Restore the original umask value. 20. Using a single chmod command, assign all permissions for the user
on all files and directories with names that start with the string mask. Group and other should not have their permissions changed.
Using a single rm command, remove all names that start with the
string mask
143-MA-62 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access (continued)
Permissions and symbolic links
21. Verify that the permtest file still exists in the current directory. 22. Using octal notation, remove all permissions from this file.
Attempt to display the contents of permtest
This should fail. 23. Create a symbolic link to the file permtest named permtest_link
k. What are the differences in permissions between permtest and permtest_link?
l. Which permissions—permtest’s or permtest_link’s—really control access to the data?
24. Verify your assumption by attempting less on permtest_link 25. Use ls and the appropriate options on permtest_link to follow
the link and display the permissions on the target file, permtest.
143-MA-63 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access
(continued)
Changing permissions recursively on directories
26. Create two levels of subdirectories named dir1/dir2 beneath the
current directory.
Recall that the –-parent, or –p, option to mkdir creates multiple levels.
27. Adjust the permissions as follows:
For dir1 For the owner, give read, write, and execute For the group, leave the permissions unchanged For others, give only execute
For dir1/dir2 For the owner, give read, write, and execute For the group, leave the permissions unchanged For others, give no permissions
28. Copy the /143/linus file to the dir1 subdirectory.
Adjust the permissions on dir1/linus for full permissions for all user categories.
29. Copy the /143/linus file to the dir1/dir2 subdirectory.
Adjust the permissions on dir1/dir2/linus for no permissions for all user categories.
143-MA-64 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.1: Adjusting Permissions and Testing File Access (continued) 30. Obtain a complete listing of the permissions of dir1 and all of its
contents.
Include the -R option to ls to descend the subdirectories. 31. Use a single chmod to set the permissions of dir1 and all of its
contents to 751
The -R, or –-recursive, option to chmod applies the permissions recursively.
32. Obtain a fresh listing of permissions for dir1 and all of its
subordinates.
m. Were the permissions recursively applied? Yes No
143-MA-65 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files
Background There is a special permission, set group ID, that can be applied to a directory so that all of its contents can be shared through group access permissions. Passwords are a first line of defense in guarding against unauthorized access to the system. Password management policies usually dictate changing the password periodically. Objectives In this exercise, you will • Create a directory structure on the server that allows group access to
its contents • Use su to substitute your login identity with another • Change your login password
Warning! Verify that the umask value has been reset to 002 after the previous exercise.
Log Manager Assignment: Part 2—Setting up the shared directory
1. Use ssh to log in to ltreesrv as userN, with the password of
ltree
Respond yes if prompted to continue. 2. Use hostname to verify a successful network connection.
143-MA-66 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files (continued) 3. Use less to examine the contents of the /etc/group file.
Your primary group, userN, should have an empty membership list since you are the only member.
4. Use a string search to locate the student group that has your login
name in the membership list.
Use /student
The student group will be used for sharing. 5. Create a directory under your home named extract_dir 6. Use ls –ld to display a long listing of only the directory named
extract_dir
a. Which of your groups owns this new directory?
143-MA-67 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files
(continued)
Two steps to group inheritance
7. With chgrp, change the group ownership of extract_dir to
student
8. Apply the write and set group ID permission to extract_dir using
chmod g+ws
9. Display another long listing of the attributes of extract_dir to
verify the proper group ownership and permissions.
b. How is the set group ID permission denoted in ls –l output?
10. Copy the week_dir.tar.gz file from your home directory into
extract_dir and verify that the new copy is owned by the student group.
The group ownership was inherited from the parent directory.
143-MA-68 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files (continued)
Changing identity
11. Verify your current identity with the id command. 12. Use su, substitute user, and the option for a login shell, to take on
the identity of logadmin.
The password is ltree
Be sure to include the –l option to receive all the context of the logadmin account. Use dash-el, not dash-one.
13. Display your current identity.
logadmin belongs to the logadmin, and student groups.
Log Manager Assignment: Part 3—Extracting the archive in the shared directory
14. Change your working directory to the /home/userN/extract_dir directory created earlier.
15. Use tar –xvzf week_dir.tar.gz to extract the archive. Then
verify that the student group ownership has been inherited throughout the weekNN directory structure.
143-MA-69 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files
(continued)
16. Use exit to return to your original account.
Verify that your identity is no longer logadmin.
You should be back to your login identity.
Use exit again to terminate the ssh session.
You should be back to your local workstation.
Changing login password
17. Use the passwd command to change your login password.
You will have to enter your original password first, and then the new password twice.
Make your password linuxtree143
This will not change your password on ltreesrv.
Warning! Record this new password. It will be required for future login and su attempts.
143-MA-70 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files (continued) 18. Test the new password by using su to your own account. At the su
prompt for a password, respond with the new value.
19. Return to your original shell with exit
c. Which user’s password must be supplied for the following?
su –l logadmin
ssh ltreesrv
su
d. What file contains the list of groups and membership lists?
e. Which of your groups, primary or secondary, is the default group owner for your files and directories?
Congratulations! You have completed the exercise.
143-MA-71 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files
(continued)
Changing group ownership
Group file sharing
20. On your own workstation, create a directory beneath your home
named stu_dir 21. Change the group ownership of stu_dir to student
Assign the set group ID permission to stu_dir and verify the permission set.
Verify the correct permissions and ownership of stu_dir
22. Copy lintronica from the home directory into stu_dir 23. Move history.txt from the home directory into stu_dir 24. List the attributes of the contents of stu_dir
f. Why is history.txt not owned by group student?
143-MA-72 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files (continued)
Removing set group ID permission
25. Remove only the set group ID permission and group write
permission for stu_dir
Verify that the permission was removed by listing the attributes of stu_dir
26. Copy s1 file from the home directory to stu_dir
g. What group owns this new file?
27. Look up the -R, or –recursive, option to chgrp in the online
documentation.
Use that option to change the group ownership of stu_dir and all its contents back to your primary group.
143-MA-73 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files
(continued)
The --command= option
28. Examine the su manual page and read about the –-command=
option.
Use this option to execute a single command as another user: The user name is logadmin The login password is ltree The single command to execute is touch /tmp/sutest
You may need quotes to keep the command as a single argument.
h. What is the ownership of /tmp/sutest?
The set user ID permission
29. List the permissions of the /bin/su file.
i. What is the purpose of the s permission?
When executed, this file uses the credentials of the file’s owner.
30. Examine the permissions of the file named ~/s1
143-MA-74 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 6.2: Sharing Access to Files (continued) 31. Add the set user ID permission to this file.
j. How is the set user ID permission represented in the output of ls -l?
32. Additional set user ID files are a security risk.
Auditing tools look for this permission. Remove the s1 file.
143-MA-75 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1:
Using find to Locate Files
Background Due to the immensity of a typical Linux directory hierarchy, we need tools that allow us to quickly locate required files and directories. Objectives In this exercise, you will • Use locate to search for files by name • Use find to search the hierarchy based on various criteria
Rapid searching with locate
1. Return to your home directory. Use locate to find all occurrences of
files named monday
a. Were your filenames discovered under the ~/week_dir/weekNN directories? Yes No
The locate command searches a database, not the filesystem. The database is updated by a scheduled job. It has not been updated since these new files were created.
2. Use sudo updatedb
sudo has been preconfigured to allow you to run updatedb.
Give your own login password at the prompt.
143-MA-76 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1: Using find to Locate Files (continued)
The password should be linuxtree143 if it was successfully changed in Exercise 6.2; otherwise, it is ltree.
3. Attempt locate again for monday
monday file names found. 4. The locate argument can be any portion of a name. Use locate
again to search for any name containing day
Contrasting locate and find
The find command searches the filesystem itself, not a database.
5. Use find from the current directory along with the –name monday
criteria. 6. Building from the previous find, search from the root directory, /,
for everything named monday
There are many “Permission denied” messages for directories you lack permission to search. Use the scroll bar to review the output; the monday names are within this listing.
b. Was find able to locate all of the desired names? Yes No
143-MA-77 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1: Using find to Locate Files
(continued)
Combining find expressions
The find command supports multiple criteria to describe the desired file or directory.
7. Use find to search /143 and the current directory for these two
criteria: It must be a directory It must be named week39
8. Building from the previous find, append the –ls action.
c. What does each of the following commands do? locate log
find –name log
find –name log –type f
find / -name '*log*' –type f –mtime -3
143-MA-78 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1: Using find to Locate Files (continued)
Congratulations! You have completed the exercise.
Additional searching
GNOME file searching
9. Use the GNOME Search for Files tool, from the Places menu, to
locate everything containing log in its name under the /var directory.
find and hard links
Hard links to the same file share the same inode number. 10. Create a new regular link mylog1/linlink to the file named
lintronica
Do not use the –s, symbolic link option. 11. Use ls –i lintronica and record its inode number:
12. Use find from the current directory with the –inum criterion to
display all names tied to that inode number.
143-MA-79 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1: Using find to Locate Files
(continued)
Shell wildcards with find
13. Use find to search the current directory for these two criteria:
It must be a directory It must contain the string log within its name Add shell wildcards, but no quote marks, to the -name criteria to locate any directory name that contains the string log.
Use *log*
An error will occur because the shell interpreted the special characters before find started.
14. Add quotes around only the search pattern and repeat the previous
search.
Combining criteria with -or
find criteria can also be combined by –or, but the results may be unexpected due to the way find processes criteria.
15. Construct a find that searches from the current directory, and joins
its two criteria with -or It must be a directory It contains the string log anywhere within its name
Make the directory test the leftmost on the command line, and make
the name test rightmost on the command line.
143-MA-80 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.1: Using find to Locate Files (continued)
d. What was the result of this find?
16. As in Step 8, add the -ls action to execute ls -lids on the
names found.
Add it immediately after the -name test.
Controlling precedence
17. Construct a new find command that will combine multiple criteria.
We will raise the precedence level of -or using parenthesis.
Parentheses are special characters to the shell and each must be quoted. For example: '(' criterion_1 -or criterion_2 ')'
From the /home directory, search for anything that meets these criteria:
Name is lintronica and Owner is user1 or user2
143-MA-81 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.2:
Pattern Matching With grep
Background After desired files have been located within the hierarchy, we want to be able to search for data within those files. Objectives In this exercise, you will • Find selected strings using grep • Search using regular expression special characters
Use of grep
We want to revisit some steps we performed in Exercise 6.2, but improve our solution by using grep.
1. Use grep to locate your login name in the /etc/group file.
Notice that your name is the name of one group, and listed as a member in another.
a. Which lines contain your name?
2. Reuse the previous command; modify the grep pattern to use
regular expression special characters. Locate the line that starts with your group name.
143-MA-82 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.2: Pattern Matching With grep (continued) 3. Display your hostname, then use grep to find that string within the
file/etc/hosts 4. Display the lines from the week_dir/week40/* files that contain
the string FAILURE. 5. Reuse the previous command, inserting the –l ( letter el, not digit
one ) option to list only the names of the files containing the search string.
6. Obtain a fresh copy of /143/history.txt
Locate any occurrence of Linux in the history.txt file, regardless of case.
The –i option will help.
b. grep displays only the strings that match the search pattern Yes No grep can search only a single file per execution Yes No grep is case-sensitive by default Yes No
Congratulations! You have completed the exercise.
143-MA-83 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.2: Pattern Matching With grep
(continued)
More on searching
Extending grep with regular expressions
7. Find all the lines that end with a digit. 8. Find all lines in the file that reference events that are dated between
1982 and 1986. 9. Find the all lines containing 1982, 1984, 1986, or 1988.
grep and find
The grep command can be used in a find command’s -exec action.
10. Search under /home for any file named lintronica; then search
within the files that match that criterion for the string mirror
find ... -exec grep mirror {} ';' 11. Use the Nautilus search tool to perform the same steps described in
the previous step.
143-MA-84 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 7.2: Pattern Matching With grep (continued)
grep options
12. Display the lines in the /143/linus file that do not contain Linus
The -v option is needed. 13. Display the lines in the /143/linus file that contain Linus. Use the
option that highlights the matching text in color.
The –-color option will help. 14. Look at the online documentation for grep to learn about the
options: --after-context= --before-context=
15. Repeat the previous search. The output should include the single
lines above and below each of the matching lines.
143-MA-85 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1:
Creating Simple Pipelines
Background The Linux text stream model is a building block for handling files or command output. Simple text processing is done using filtering commands. Multiple simple filters can be combined using pipelines to create custom tools. Objectives In this exercise, you will • Capture command output using redirection • Use simple filters to format text • Create complex tools by combining filters into pipelines
Catenating files and capturing output
1. Change your working directory to week_dir/week40 2. Use the cat command with two arguments to display the contents of
monday followed by the contents of tuesday
a. Contrast cat and less. Was there any separation between the contents of the two files or scrolling when written to standard output by cat? Yes No
3. Concatenate the contents of all the files in this directory. Repeat the
previous cat command, but use *day as the only argument. 4. Repeat the previous cat command, but add a redirection symbol to
save standard output into a new file called whole_week 5. Use wc –l * to verify that the number of lines in whole_week is
equal to the sum of the number of lines from the *day files.
143-MA-86 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines (continued) 6. Use cat and redirection symbols to append keyboard input to the
end of whole_week
The cat command will read standard input when no argument list is given. The default standard input device is the keyboard.
Use cat >> whole_week without any argument list.
Warning! Do not overwrite the existing contents of whole_week.
Enter the following three lines of text as standard input to cat:
STOP END week40
After the three lines of text, on a line by itself, press <Ctrl><D>:
^D
b. What does ^D mean when given at the start of a line?
7. Use tail to display only the last three lines of whole_week
See the three lines entered above.
143-MA-87 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines
(continued)
Combining tools into pipelines
8. Return to your home directory. Display a long listing of the current
directory sorted by size in bytes.
The size in bytes is the fifth field of a long listing. Be sure to use a numeric sort. Slide 8-25 of the Course Notes has a sort example.
9. Pipe the results of the previous command to tail -1 to display only the line with the largest number of bytes.
10. Repeat the previous command pipeline, but change it to display only
the first two lines. These are the total line and the line with the smallest number of bytes.
11. Sort the contents of /etc/passwd numerically, with the third field
as the sort key.
The file is colon-delimited. Use the -t: within the sort command. See slide 8-17 in the Course Notes.
12. Pipe the results of the previous command to tail -1 to display on
the line with the largest UID. 13. Replace tail in the pipeline above with a filter that will scroll the
sorted lines.
143-MA-88 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines (continued)
c. What are the three standard streams?
d. What is the meaning of the following redirection symbols? <
>>
>
|
Congratulations! You have completed the exercise.
143-MA-89 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines
(continued)
Watching files grow
For this step, you will need two Terminal windows open. 14. Attempt a tail –f follow_file
e. Why did this fail?
It is not uncommon that empty log files are created before any logging is written to them.
15. Create the empty follow_file using touch
Repeat the tail –f from the previous step.
16. In the other Teminal window, use cat >> follow_file
Enter the following text into cat’s standard input: GOOD messages BAD messages MIXED messages Terminate standard input by entering the end of file character.
f. Did tail display the lines as they were added? Yes No
17. Use ^C to terminate only the tail command.
143-MA-90 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines (continued) 18. Reuse the previous tail command with these changes:
Pipe its output to grep to select only the lines containing the string GOOD
19. In the other Terminal window, enter the following text into cat’s
standard input appending the output to follow_file: GOOD times BAD times OTHER times GOOD news BAD news NO news
Terminate cat’s standard input using ^D
Terminate the tail and grep pipeline using ^C
Multiple redirections
20. Use find to locate all the filenames on the system that contain the
string log Redirect the standard error stream to /dev/null Redirect the standard output stream to log_listing
Standard error is redirected using 2> 21. Repeat the find command, but pipe the standard output into less
143-MA-91 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.1: Creating Simple Pipelines
(continued)
Taking care with redirection symbols
22. Sort the follow_file used earlier. 23. Repeat the sort from above, but use output redirection and send
the standard output of sort back into follow_file
g. What happened to follow_file?
Longer pipelines
24. Task: Find the largest object under /etc.
The -s option to ls will list the sizes in blocks of a directory’s contents.
Obtain a listing of the contents of /etc with ls -s Pipe that result into a filter to arrange the largest sizes on top.
A numerical reverse order sort is required. Add another filter to the pipe to display only the line with the largest size.
143-MA-92 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-93 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2:
Working With sed and gawk
Background sed and gawk are filters that reformat text. They are often combined with other filters that may be used to select and organize the data stream. Objectives In this exercise, you will • Use sed substitution to reformat text • Use gawk’s field selection to display only portions of lines
Simple sed substitution
1. Use tail -1 to display only the last line of /etc/passwd 2. Repeat the previous tail and pipe the output into sed. Use the
substitution operation to replace the string : with the string +
a. How many strings were changed on the line?
3. Repeat the previous command and add the global flag g to the end
of the substitution operation.
b. How many strings were changed on the line?
143-MA-94 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2: Working With sed and gawk (continued)
Printing fields with gawk
4. Display a long listing of the contents of the current directory with
attributes.
The output is formatted into nine fields of whitespace-delimited text.
Repeat the previous command, piping the output into gawk and printing only the first and ninth fields.
5. Repeat the previous command, and modify the print statement to
display the first, fifth, and ninth fields, in order. 6. Repeat the previous pipeline, rearranging the order of fields on the
print so that the fifth field is displayed first, followed by the ninth field, followed by the first.
7. Use grep to search /etc/hosts for the line that ends with your
simple hostname, without the domains.
A regular expression special character is needed to anchor the pattern to the end of the line. The --color option for grep will help to identify a match at the end of a line.
Pipe that result to gawk to display only the first field.
First field is the IP address.
143-MA-95 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2: Working With sed and gawk
(continued)
c. What is the output from the following commands? hostname | sed 's/e/E/'
hostname | sed 's/e/E/g'
date | gawk '{ print $2, $3, $6 }'
Congratulations! You have completed the exercise.
143-MA-96 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2: Working With sed and gawk (continued)
Additional pipelines
Adding additional filters
8. Repeat the previous pipeline, adding sort to the end to arrange the
output such that the largest size is printed last. 9. Modify the previous pipeline, so that only the line describing the
largest file is printed. 10. Display a listing of all users that are logged in using who Pipe the output into grep to select lines that contain the string pts
Pipe the output into gawk to display only the first, third, fourth, and fifth fields.
11. Reuse the pipeline constructed for Step 7: grep 'ltreeN$' /etc/hosts | gawk '{ print $1 }'
The output is the machine's IP address: 10.1.1.N
Add another gawk to the end of the pipeline that will display only the last number of the IP address.
The numbers are separated by dots.
143-MA-97 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2: Working With sed and gawk
(continued)
12. Use gawk to display fields two through six of lintronica.
Append to the pipeline a grep that will select lines containing a 5 Append to that pipeline another gawk that will display only the second field as delimited by @.
Build the solution up in stages using the bash history feature.
Testing input interactively
13. Enter the sed command that will globally replace the string apple
with pear on all lines of input.
The sed command will read standard input from the keyboard; enter the following text as stdin to sed:
An apple a day Keeps the doctor away Give the teacher an apple Are Snapples made from apples ^D
143-MA-98 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 8.2: Working With sed and gawk (continued)
Additional gawk capabilities
14. gawk can sum fields within a print statement.
Enter the command line gawk '{ print $1 + $2 }'
Enter two numbers on each line for gawk to process.
gawk’s standard input will default to the keyboard.
143-MA-99 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1:
Editing a Text File With vim
Background vim and similar editors are powerful command-line tools that can change text files. Their power is in the portability and reusability of the skills, and also in the amount of work that can be accomplished with the fewest steps. Objectives In this exercise, you will • Navigate through a file • Add and remove text • Save changes
Moving through the file and adding text
1. Copy the file w42_logger from the /143 directory. Invoke vim, giving w42_logger as its argument. 2. Use :set number <Enter> to enable line numbers within the edit
session. 3. Move to the ninth line of the file, 9G 4. This line has an error, an extra 2 in the filename. Move the cursor
right and edit this line so that the directory name is week42.
Use x to delete one character. 5. Move to line 15 of the file, 15G
143-MA-100 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim (continued) 6. This line has an error, a directory argument is missing. Edit this line
to append the directory name week42 after the cd command.
Terminate text entry with <Esc>. 7. Move to line 19 of the file, 19G. This line has an error, a missing
output redirection symbol. Edit this line to insert the > character before the word week42.byday.log.
8. Use / to search for the line containing the string compress. 9. This line should be a comment. Insert the # character at the
beginning of the line. 10. Write the file out, but do not exit the editor session.
Use :w, then press <Enter>.
Editor contents are saved with the original name.
Searching
11. Use / to search for the line containing the string week42.
Repeat the search using the command n until all occurrences have been found.
a. Will a string search wrap around the file, and restart at the beginning? Yes No
143-MA-101 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim
(continued)
12. Position the cursor on line 2. Use / to search for the letter w
Cursor is located on the w of w42 in line 2.
Changing text and global substitution
13. Use c$ to change from the current cursor position to the end of line.
Enter the string w43 as the replacement text.
Press <Esc> to terminate text entry. 14. Perform a global substitution using :1,$s/week42/week43/g
b. On which lines was the substitution attempted?
Saving with another name
These contents are now suitable for saving as the w43_logger file.
15. Use the command :w w43_logger <Enter> to save the edit
buffer’s contents with another name. 16. Use the command :q! <Enter> to exit this edit session.
143-MA-102 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim (continued)
Reviewing the changes
17. The w42_logger script has been fixed and reused to create the
w43_logger script.
Use diff to compare these two files.
Only lines containing wNN and weekNN should differ.
c. What key must be pressed to execute vim commands that start with the colon character, :?
d. What key must be pressed to terminate text entry when using the vim commands i, a, or c?
Congratulations! You have completed the exercise.
143-MA-103 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim
(continued)
Additional editing with vim
18. If needed, obtain a fresh copy of history.txt from the /143 directory. Invoke vim, giving history.txt as its argument.
19. Find all occurrences of the string Unix using / and n until the
search wraps around. 20. Move to the last line of the file, G 21. Use the question mark, ?, to search upward in the file and locate the
string Linus.
Inserting and appending text
22. Position the cursor at the beginning of line 1. 23. Insert a new line containing TITLE as the new first line of the file. Terminate the input string with the <Esc> key.
Newly inserted text may contain newline. 24. Position the cursor at the end of the last line of the file. 25. Append a newline containing FOOTNOTES after the last line. 26. Use :wq to save the changes and exit the editor.
143-MA-104 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim (continued)
Editing ranges of lines
27. If needed, obtain a fresh copy from famous_email in the /143
directory. Open the file famous_email with vim. 28. Enable line numbering using :set number
29. Using a string search, move the cursor to the first occurrence of the string minix. Record the line number: ______________
30. Repeat the search until you find the third occurrence of the string
minix and record the line number: ______________
These mark the range of lines to be changed. 31. Using the line numbers to define a range of lines, perform a
substitution changing the string minix to the string MULTICS
e. How many lines were changed?
f. Would using 1,$ as the range have the same effect? Yes No
143-MA-105 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 9.1: Editing a Text File With vim
(continued)
Saving to another file
32. Save the changed version to a new file named newlog 33. Exit the vim session. 34. Use diff from the command line to compare the newly created
newlog file to the original famous_email.
143-MA-106 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-107 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1:
Customizing the Shell
Background The shell is the primary application used to gain access to other applications. Variables and options are used to customize the shell’s behavior. Objectives In this exercise, you will • Modify shell options • Assign and use variables • Adjust $PATH to locate executables
Shell options
1. Display the current settings of shell options using set –o
a. What is the status of the noclobber option?
2. Use set –o noclobber to enable the option. 3. Verify the setting using set –o 4. Test the noclobber option by attempting to clobber a file; use
ls > history.txt
b. Was history.txt changed? Yes No
143-MA-108 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1: Customizing the Shell (continued)
Examining $PATH
5. Display the contents of PATH using set and piping to grep
c. What does the shell search for in these directories?
6. Confirm the $PATH contents by echoing the variable.
The useradd command is located in the /usr/sbin directory. Verify the PATH variable includes this directory. 7. Test the PATH variable by installing a new user account, newgal,
through sudo: sudo useradd newgal
Give your own login password when prompted by sudo.
143-MA-109 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1: Customizing the Shell
(continued)
Setting the prompt string
8. Display the contents of the HOSTNAME and PWD variables. 9. Without quoting, assign $HOSTNAME$PWD to PS1
Values are appended in the shell by not having any whitespace separator between them.
10. Assign PS1 any desired text and spaces, but replace $HOSTNAME
and $PWD with their corresponding sequences \h and \w
Use the single quotes around the value assigned. 11. Change the working directory to /tmp, then back again to $HOME to
verify the prompt string is updated correctly.
d. What shell variable contains the following? Prompt string:
Directory search order for commands:
Working directory name:
143-MA-110 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1: Customizing the Shell (continued)
Congratulations! You have completed the exercise.
Using the shell
Adding more to the PATH
12. The ifconfig command is located in /sbin directory. Verify your PATH variable so the ifconfig command can be found. 13. Use ifconfig to display your network interface settings.
143-MA-111 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1: Customizing the Shell
(continued)
Working without $PATH
14. Assign an empty string into PATH 15. Display $PATH
There should be no output except the newline from echo itself. 16. Attempt the ls command.
e. Was it successful? Yes No f. Why would the previous echo work but ls fail?
17. Assign a minimal set of directories, /usr/bin and /bin, into PATH 18. Attempt the ls again.
Get a fully restored PATH by opening a new Terminal window, thus having some of our startup scripts executed again.
19. From the shell prompt, press <Ctrl><D>, or ^D
The Terminal window closes.
If the shell does not exit, disable the ignoreeof option using set +o ignoreeof
143-MA-112 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.1: Customizing the Shell (continued)
The history option
20. Open a new Terminal window
Use set –o and pipe into grep to discover the status of the history option.
21. Turn off the history option.
22. Review the options to verify that this option has changed.
23. Enter the commands that answer the questions: Who am I? Where am I?
Attempt to use the arrow keys to display the previous commands.
g. What is the last command in the history list?
24. Re-enable the command-line history option. 25. Display the shell option settings using set +o. Compare this output
to that of set –o.
h. Which form of the set command produced output that is suitable for saving into a file and using to restore options?
26. Use another set +o and output redirection to save the new option settings into a file called saveopts.
143-MA-113 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts
Background Login scripts automate the customization of the shell’s behavior by assigning aliases, variables, and shell options. Objectives In this exercise, you will • Create and use aliases • Edit .bashrc to assign aliases and set shell options • Edit .bash_profile to assign variables and export them to the
environment
Assigning aliases
1. Use the alias command to display the list of currently defined
aliases. 2. Create an alias named rm with the value rm –i
Remember to use quotes around any spaces on the assignment.
3. Verify that the alias has been created by using the alias command. 4. Use the mouse to open a new Terminal window. 5. In the newly opened Terminal window, display the list of known
aliases.
The alias is not defined in this shell.
143-MA-114 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts (continued)
Editing .bashrc
6. Use vim or gedit to open .bashrc for editing.
Be sure your working directory is your home directory.
At the end of the file, add the line: alias rm='rm -i'
Save the file and exit the editor.
7. Use the command bash to create a new shell in the current Terminal
window.
List the currently defined aliases.
a. Was the rm alias defined in .bashrc available? Yes No
8. Use the mouse to open a new Terminal window.
In the newly opened Terminal window, display the list of known aliases.
b. Was the rm alias available in the new window? Yes No
143-MA-115 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts
(continued)
The PATH and PS1 environment variables
9. Enter the command env | grep '^P' to display the names of all
environment variable assignments that begin with the character P.
Both PS1 and PATH are environment variables.
c. Is the working directory contained in $PATH? Yes No
Check for a dot ( . ), the working directory’s relative pathname. 10. List the contents of the current directory, noting the shell script s1.
Enter the command line s1
Shell will not look in the working directory for executables. 11. Change the PATH variable through an assignment that appends a
dot, ., to the end of its contents.
Warning! Be careful not to lose the previous contents of PATH.
Separate each directory in $PATH with a colon, :. 12. Verify the new contents of $PATH. 13. Enter the s1 command again.
The shell finds the s1 script.
143-MA-116 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts (continued)
Changing .bashrc and .bash_profile
14. Using either vim or gedit, open the .bash_profile file for
editing.
Modify the assignment to PATH to include . (dot) at the end, as was done interactively a few steps ago.
Save the changes and exit the editor.
15. Using either vim or gedit, open the .bashrc file for editing.
Modify the assignment to PS1 to '\u@\h \w $ '
Save the changes and exit the editor. 16. Log out of the system, confirming the action when prompted.
The exit command will not terminate the desktop.
Use the System menu. 17. Log in again and open a Terminal window after GNOME has started.
143-MA-117 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts
(continued)
18. Examine the contents of PATH and check your aliases to verify that the startup scripts ran correctly.
d. Which startup script runs only in login shells?
e. Which startup script runs in each new interactive shell?
f. What is the benefit of defining aliases in your .bashrc file?
Congratulations! You have completed the exercise.
143-MA-118 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts (continued)
Changing bash configuration files
Adding a shell option
19. Edit .bashrc, adding a line to turn on the noclobber option. Create a new bash shell and use set –o to display the current
options.
g. Was the noclobber option in effect? Yes No
Testing logout sequence
20. Create a .bash_logout file in the home directory and add the
following two lines: date >> logout echo Logging Out >> logout
Save the changes to .bash_logout and exit the editor. 21. Use the exit command to terminate the shell. 22. Open a new Terminal window if needed and examine the contents of
logout
h. Was an entry added by using exit? Yes No
.bash_logout is only executed when a login shell terminates.
143-MA-119 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts
(continued)
23. Use ssh $HOSTNAME to log in to your own host. Enter yes if prompted to accept the key. Give your own login password. After a successful login via ssh, terminate this login shell. 24. Examine the contents of the logout file.
i. Did the ssh session start a login shell? Yes No
Testing login sequence
25. Edit .bash_profile and add the following three lines of text after
line 2:
date >> login echo Logging In >> login echo 26. Edit .bashrc and add the following three lines of text at the end:
date >> login echo New bash shell >> login echo 27. Log out, then log in again through GNOME. 28. Examine the contents of login.
j. Which login script ran first?
143-MA-120 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts (continued)
Special bash options
29. Open a second Terminal window. In this second Terminal, use tail -f login
You can monitor new entries being added in the steps below. 30. From the first Terminal window, use the bash command to create a
new shell.
k. Which scripts wrote entries to login?
31. Use the command bash –-login to create another shell.
l. Which scripts wrote entries to login?
32. Use the command bash –-noprofile to create another shell.
m. Which scripts wrote entries to login?
143-MA-121 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 10.2: Customizing the Login Scripts
(continued)
33. Use the command bash –-norc to create another shell.
n. Which scripts wrote entries to login?
34. Use the command bash –i to create another shell.
o. Which scripts wrote entries to login?
35. Close the Terminal window running the six shell commands. 36. Interrupt the tail command with ^C, and use this Terminal to
examine the bash documentation to learn the meanings of the options used in this bonus.
143-MA-122 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-123 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1:
Working With Shell Scripts
Background The power of the command line is maximized by using shell scripts. These files contain the command-line steps that automate some procedures. Objectives In this exercise, you will • Execute a shell script as a command or as an argument to bash • Modify a shell script to include error checking and argument
processing
Reading and running a script
1. Make your home directory the working directory.
Read through the contents of the bin/logpro4 shell script. 2. Run the script as a command: logpro4 week39 week40 week41 week42
a. What directory will contain the .gz files created?
3. List the contents of the directory containing the .gz files.
143-MA-124 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts (continued)
Run the script a second time using: bash logpro4 week39 week40 week41 week42
There is no difference in the results.
Modifying a first script
The current revision of logpro4 does not remove the $HOME/weekNN after processing completes.
4. Execute an ls of your home directory; notice the temporary
directories created by the logproN scripts.
Should display the weekNN directory names. Use either vim or gedit to edit ~/bin/logpro4. Modify the script to add a new command to remove the temporary
work directory. This command must be the last command within the for loop.
rm -r "$week"
This new command line should be immediately before the done line.
5. Run the script again, remembering to pass in the following argument
list: week39 week40 week41 week42
143-MA-125 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts
(continued)
6. List the contents of your home directory; verify that the weekNN directories have been removed.
Extending logpro4
7. Currently logpro4 writes all lines into the $week.byday.log file.
We would like to create an additional output file called $week.byday.FAILURE. This new file will contain only the FAILURE lines from the $week.byday.log file. Compress this new file and move it into the $HOME/logfiles directory with the others.
Use either vim or gedit to edit ~/bin/logpro4 and make the following enhancements: After the sed command, add the line:
grep FAILURE "$week.byday.log" > "$week.byday.FAILURE"
On the gzip line, add the new file name to the argument list.
On the mv lines, add the new file name as the second argument on the list.
Remember gzip creates a .gz file name. Remember to adjust both mv lines.
If desired, additional gzip and mv lines could be added instead of changing the existing argument list.
143-MA-126 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts (continued) 8. Test the script with the argument list:
logpro4 week39 week40 week41 week42
List the contents of your $HOME/logfiles directory to verify that the FAILURE files were created.
Warning! If the script failed, fix the errors and run again.
b. What is the use of the following in shell scripts? if
for
#
Congratulations! You have completed the exercise.
143-MA-127 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts
(continued)
Additional scripting
9. The output of logpro4 is a series of gzipped files. We need a way
to view these files without losing the compressed version.
Examine the contents of the bin/readlog1 script.
It will uncompress the $HOME/logfiles/week40.byday.log.gz file created earlier and pipe it to less for viewing.
We want to enhance bin/readlog1 to process multiple weekNN files passed as arguments.
This is similar to the migration from logpro3 to logpro4 discussed in the Course Notes.
10. Use either vim or gedit to edit bin/readlog1 and make the
following changes: Add an if/then/fi block at the beginning of the script to verify the
number of arguments passed is not equal to zero. If it is equal to zero, display an error message and exit the script. Add a for loop to process the multiple weekNN files contained in "$@". The for loop should encapsulate the innermost if/then/else/fi block.
143-MA-128 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts (continued)
Remove or comment out the assignment week=week40.
The file logpro4 contains an if/then/fi block and a for loop to use as models.
11. Test the script by executing it multiple times with various argument
lists: readlog1
Program should exit with an error message that readlog1 requires an argument. readlog1 week39 readlog1 week40 week41 week42
less is executed within the for loop once for each file. You must use q to quit each execution.
Should display each gzipped log files using less.
Warning! If the script failed, fix the errors and run again.
143-MA-129 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts
(continued)
Creating a new script from an old script
12. A script is now required to display the gzipped FAILURE files.
The readlog1 script does not process the FAILURE files, but it is an excellent script to build from.
Copy bin/readlog1 to bin/errlog1
Use vim or gedit to modify bin/errlog1 as follows:
Find all occurrences of $week.byday.log and replace them with $week.byday.FAILURE
In vim use :1,$s/...old.../...new.../g<Enter>
13. Test the new script. errlog1 week40 errlog1 week41 week42
Only the FAILURE messages are displayed.
The week39 files had no FAILURE; it was a very good week!
143-MA-130 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts (continued)
Creating a new script
14. Create a new script called bin/myrm that accepts as arguments the
names of files and directories to be removed.
The myrm script will actually move the files and directories into a hidden subdirectory, $HOME/.deleted.
Create the myrm script with the editor of your choice; it should include the following:
A test to verify the $HOME/.deleted directory already exists; if not, create it.
You may use negation to test if the directory is not present.
if ! [ -d "$HOME/.deleted" ] then mkdir "$HOME/.deleted" fi
The ! is the negation operator.
The commands to copy the names in the argument list to the directory $HOME/.deleted, and then to remove the actual argument names.
All of the arguments are contained in $@. Save the script and exit the editor. Add execute permission to the new script.
143-MA-131 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 11.1: Working With Shell Scripts
(continued)
15. Test the new script and look for the names in the .deleted directory.
myrm history.txt myrm logfiles lintronica ls .deleted
16. If the script works as desired, create the alias: alias rm=$HOME/bin/myrm
143-MA-132 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-133 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1:
Launching and Controlling Multiple Jobs
Background Linux is a multitasking operating system. Its users can multitask also. The ability to start and manage multiple processes allows users to take full advantage of the power of Linux. Objectives In this exercise, you will • Start background jobs • Obtain a listing of jobs and processes
Starting a background job
Warning! Close any open gedit windows. 1. Start gedit in the background.
Remember to use the &. 2. Use the ps command to get the PID of the gedit process.
Record the PID here:
3. Open another Terminal window for the following steps.
143-MA-134 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs (continued) 4. Use the ps command without options.
a. Was the gedit listed in the output? Yes No
The gedit was started from another shell.
Listing and finding processes
5. Use ps –ef piped into grep to search for the string gedit. 6. Use ps aux piped into grep to search for the string gedit.
Both lines containing gedit and grep gedit are displayed. 7. Use pgrep to obtain the PID of gedit.
b. Was it the same PID as recorded above? Yes No
8. Use the kill command to terminate the gedit by its PID.
This will terminate gedit. 9. Start the log_chk script in the foreground. 10. Attempt to interrupt the script using ^C
This script ignores ^C.
143-MA-135 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs
(continued)
11. Return to the previous Terminal window. 12. Search for the PID of log_chk.
Use one of the methods above. 13. Use kill with no options on the PID of log_chk.
This script ignores the default signal, TERM. 14. Use kill with signal 9 to terminate log_chk.
This will terminate it.
c. Which processes are listed by the following commands? ps
ps aux
ps aux | grep bash
pgrep bash
143-MA-136 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs (continued)
Congratulations! You have completed the exercise.
Managing processes
Using top options and interactive commands
15. Read the man page or info page for top for the use of the –d option. 16. Start top, requesting a five-second delay between updates.
Notice that the screen is updated every five seconds.
top has many interactive commands.
17. Use the d command and change the interval to every one second.
The screen is updated every one second. 18. Use the u command, specifying root as the only user for display in
top’s output. 19. Use u again to reset to all users. 20. Try the h command to bring up the top help screen.
Read about the l, t, and m commands.
A toggle is a command that turns a setting off if it is on and on if it is off.
143-MA-137 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs
(continued)
21. Return to the normal top display and try out the l, t, and m commands.
22. Bring up the top help screen and read about the f command. 23. From the normal top display, try out the f command.
Modify the fields chosen for output, limiting the output to just these fields: PID, USER, %CPU, %MEM, and COMMAND.
Press the letter before each field name to enable or disable the toggle.
Return to the normal top display.
d. Has the output been reduced to only the desired columns? Yes No
24. Return to the top help screen and read about the W command.
e. What is the purpose of the W command?
25. From the normal top display, try out the W command.
f. What filename did top use to save the configuration?
143-MA-138 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs (continued) 26. Exit top and return to the shell. 27. Examine the contents of the top configuration file.
g. Would it be easier to create this file manually, or to create it from within top itself?
28. Restart top
h. Was the output configured as before? Yes No
Running background jobs
29. Use <Ctrl><Alt><F2> and login to the console
Execute sleep --help to read about its syntax and function.
The manual page and info document for sleep have slightly more information.
The sleep command pauses for process for the number of seconds given as an argument.
30. Start the sleep command in the background for a duration of 500
seconds. 31. Log out of the console, then log in to the console again.
143-MA-139 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 12.1: Launching and Controlling Multiple Jobs
(continued)
32. Search for the sleep command.
The sleep command continued to run.
Kill all running sleep commands.
Running background jobs with nohup
33. Enter the command shopt -s huponexit
The bash option shopt –s huponexit enables HUP signals to be sent when the shell exits.
34. Repeat steps 30 through 32 as above.
i. Is the sleep command still running? Yes No 35. Enter the commands:
shopt -s huponexit nohup sleep 500 &
The nohup command's argument ignores the HUP signal, which is given when the user logs off.
36. Log out and log in to the console again as above.
j. Is the sleep command still running? Yes No 37. Return to the GUI: <Ctrl><Alt><F1>.
143-MA-140 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-141 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux
Background The new installation should be investigated and configured. Objective In this exercise, you will • Configure and use the newly installed Ubuntu Linux
Logging in to Ubuntu
1. Log in to the teamN account created during installation. Enter the
password ltree when prompted. This password will be needed for sudo.
A desktop should be available.
Adjusting the screen resolution if needed
2. Select the Settings “gear” icon from the top panel, then on the right
side choose Displays from the drop-down menu.
Choose a working resolution from the menu, 1440 x 900 (16:10) or 1280 x 800 (16:10) or 1024 x 768 (4:3).
Click Apply.
You may need to scroll toward the upper right corner.
Settings
143-MA-142 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux (continued)
Finding and opening a terminal
3. Select the Dash home launcher, Ubuntu button in upper left.
Enter Terminal into the Search box.
This launcher may be added to the dock or desktop. 4. Determine your user-level information:
What is my identity? whoami
What is my directory? pwd
What files do I have? ls -al
What are my groups? id
Only members of the sudo group can use sudo. 5. Determine your host-level information:
What is my system information? uname -a
What is my system name? hostname
How much disk space is free? df -h
Ubuntu button
143-MA-143 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux
(continued)
6. Determine your network configuration:
Open the System Settings tool, “Wrench and Gear.”
Choose the Network configuration tool to view networking information.
ltreesrv is both the DHCP and DNS server.
What is my network address? ifconfig
Can I reach the server? ping -c1 ltreesrv
Time and Date
Lower left side
Network
143-MA-144 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux (continued)
Adjusting the clock
7. Start the Time & Date tool from System Settings and adjust the time
zone, clock, and calendar as needed.
Use the date command in the Terminal to verify the new setting.
Restoring the course materials and adjusting the ownership
8. Restore the course files as done earlier using: sudo tar –xvjPf /L*/1*
An ls -l /143 . command reveals that the restored files are owned by user ubuntu (UID = 999) and group ubuntu (GID = 999).
Change user and group ownership of the /143 directory and the
newly copied contents your current directory: sudo chown -R teamN:teamN /143 [a-z]*
The chown command syntax accepts user:group to change both; -R is the recursive option.
Your home directory and the /143 directory now have the course files for further study.
Congratulations! You have completed the exercise.
143-MA-145 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux
(continued)
Using the newly installed Linux host
Log Manager Assignment: Part 7—Retrieving your archive from the server
9. ltreesrv has your archive, /home/userN/week_dir.tar.gz,
that was used during chapters 5 and 6. We want to securely copy that archive into your current directory on this new workstation.
ltreesrv does not have access to your newly created teamN account. You will need to provide the remote login name as part of the scp command.
scp userN@ltreesrv:/home/userN/week_dir.tar.gz .
Your password for userN on ltreesrv should be ltree 10. Extract your archive using tar –xvzf week_dir.tar.gz
The Log Manager project is complete.
11. Confirm that your home directory now has your log manager files and your course files.
143-MA-146 © All rights reserved. Not to be reproduced without prior written consent.
Hands-On Exercise 14.1: Configuring Linux (continued)
Adding additional users
12. Install an additional user account. Set the login name to your
teammate’s name. Set the password to ltree sudo useradd -m partner1 sudo passwd partner1
The -m option causes the home directory to be created. Not all versions of useradd require this option.
13. Switch to the new user account to test it.
This new user account is not in the sudo group and cannot use sudo.
Return to your original teamN account.
Self-guided exploration
14. Experiment with some of the default applications available in this
Ubuntu distribution.
143-MA-147 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Exercises
143-MA-148 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-149 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 2.1
a. Local
b. New terminal is in its own window, new tab is within the same window c. Yes d. Default
e. Yes
f. No g. Yes
143-MA-150 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-151 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 3.1
1. ls
2. file d1 file f1 file s1
a. directory b. ASCIl text c. Bourne-Again shell script text executable
3. less f1 /FAILURE
4. ls –-help
5. man ls 6. info ls
143-MA-152 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 3.1 (continued) 7. ls -C ls –x
d. file e. man date info date date –-help f. /column<Enter>
8. file –-help 9. file –b d1 f1 s1
g. No 10. file s1
h. Yes i. Yes
11. less s1
j. Commands, comments, blank lines k. #
143-MA-153 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 3.1
(continued)
12. man bogus info bogus
l. No
m. Yes 13. whatis bogus
14. whatis man whatis info 15. info info 16. man man 17. info man 18. man info
19. Done with GNOME
20. Done with GNOME
143-MA-154 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-155 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.1
1. cd 2. pwd 3. cd /143 pwd 4. ls -l
a. /143/lintronica 5. cd /etc pwd 6. ls
b. /etc/hosts 7. less hosts /ltreeN 8. cd pwd 9. less /143/lintronica 10. less /home/user30/lintronica
11. less ../user30/lintronica
c. ../user30/lintronica
143-MA-156 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.1 (continued) 12. cd 13. mkdir logos 14. ls -l 15. ls -l logos 16. ls -ld logos 17. ls -al logos
d. .. ../.. ../../etc ../user29
18. ls /bin
e. Yes 19. cd /tmp 20. ls /bin
f. Yes 21. ls ../bin 22. file ../bin/ls
143-MA-157 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.1
(continued)
23. ls /home/userN 24. mkdir -p /home/userN/history/facts 25. mkdir /home/userN/history/myths
26. cd /home/userN/history
ls ..
27. cd facts ls ../..
28. cd
143-MA-158 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-159 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.2
1. cd 2. ls -l /143 3. ls 4. cp /143/history.txt hist.txt 5. cp /etc/hosts hosts 6. ls -l 7. cp -b hosts hist.txt 8. ls -l
a. Yes
9. mv hist.txt~ hist.txt 10. cp -i hosts hist.txt 11. diff hist.txt hosts
b. Yes
12. cp /143/history.txt /etc/hosts . 13. ls
14. cp /143/tux.jpg logos
143-MA-160 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.2 (continued) 15. cp /143/lti_logo.gif logos/logo.gif 16. ls -l logos
c. cp lintronica lin.txt cp lintronica /tmp/lin.txt
mv lintronica /tmp/lin.txt 17. mkdir -p history/facts 18. mkdir history/myths 19. cp /143/famous_email history/facts 20. cp /143/fables.txt history/myths/famous_email ls history/myths history/facts
21. diff history/facts/famous_email history/myths/famous_email 22. diff history/facts history/myths 23. cp -ib history.txt hosts 24. diff history.txt hosts
d. Same e. Yes
143-MA-161 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.2
(continued)
25. mv hosts~ hosts 26. diff history.txt hosts
27. cp history.txt orig.history.txt cp linus orig.linus ls -l history.txt orig.history.txt linus orig.linus
28. cp -i history.txt linus
f. No
29. cp -i history.txt linus
g. No
30. cp -i history.txt linus
h. Yes i. Any response that starts with letter y is yes
31. mv -i orig.linus linus 32. less linus 33. man cp
j. Copy directories recursively
143-MA-162 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.2 (continued) 34. man mv
k. Move only if source file is newer or if destination file is missing
143-MA-163 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.3
1. ls -l /etc
a. Yes 2. ln /etc/hosts my_hosts 3. ls -li /etc/hosts my_hosts 4. diff my_hosts /etc/hosts
5. rm my_hosts 6. ls -l /etc
b. No
7. ln -s /etc/passwd my_pw 8. ls -l my_pw 9. ls -lL my_pw
c. No
10. less my_pw
d. Yes 11. diff /etc/passwd my_pw
143-MA-164 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.3 (continued) 12. ln famous_email my_email
13. ls -li 14. shred my_email 15. ls -l famous_email my_email diff famous_email my_email
e. Yes 16. rm my_pw my_email 17. ls -l /etc
f. Yes g. No h. No, rm does, less does not 18. cp /143/history.txt h1 19. ln h1 h2 20. ls -l h1 h2
i. 2
21. ls -i h1 h2
143-MA-165 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.3
(continued)
j. Yes
22. cp h2 h3 ls -li h1 h2 h3 23. mkdir copies links
24. ln -s links active
25. mv h1 h2 active
k. links
26. ls -l active
l. No
27. ls -lL active
m. Yes
n. Change working directory to links 28. rm active ls -l links 29. ln -s copies active 30. mv h3 active
143-MA-166 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 4.3 (continued)
o. copies
31. ls -lL active
32. rm -r active links copies
143-MA-167 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 5.1
1. cd 2. mkdir log_test 3. cp -r /143/wildcard . 4. ls wildcard 5. ls -l wildcard/log*
a. 12
6. ls -l wildcard/log? 7. cp wildcard/log? log_test 8. ls -l log_test 9. mkdir {work,play}days 10. ls wildcard/*day 11. mv wildcard/{mon,tues,wednes}day workdays 12. mv wildcard/{thurs,fri}day playdays 13. ls wildcard 14. ls –ld *days 15. rm -i log_test/*
143-MA-168 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 5.1 (continued) 16. cd wildcard ls -l ~ 17. ls -l ~/log_<Tab> 18. cd
b. Lists the names log5, log6, log7, and log8 Removes all names log followed by a single character Displays contents of /etc/passwd then /etc/hosts Recursively copies names starting with week from /143 to the current directory
19. Done with GNOME
20. Done with GNOME 21. Done with GNOME 22. Done with GNOME 23. Done with GNOME 24. Done with GNOME 25. mkdir lin_test 26. ls –l <Tab>
143-MA-169 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 5.1
(continued)
c. No 27. ls -l lin_ <Tab> 28. cp fa <Tab> lin_ <Tab> 29. ls -l lin_ <Tab> 30. rm -r lin_ <Tab> 31. cd wildcard 32. cp /143/wildcard/* . 33. mkdir log_{errors,keepers} day 34. mv *?day day 35. mv log[1-9] log_keepers 36. mv log 'log*' log_errors cd 37. Done with GNOME 38. Done with GNOME 39. ls -l ~/.Trash 40. mv ~/.Trash/tux.jpg ~/logos 41. Done with GNOME
143-MA-170 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 5.1 (continued)
d. Eye of Gnome
42. Done with GNOME 43. Done with GNOME
e. gedit 44. Done with GNOME
45. Done with GNOME
143-MA-171 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.1
1. umask 007 2. ls –l /etc/group
a. Anyone
3. cp /etc/group permtest 4. ls -l permtest
b. No
5. chmod 000 permtest
ls -l permtest
6. less permtest
c. No
7. cp /etc/group permtest
d. No 8. chmod u=rw,g=r permtest 9. ls -l permtest
143-MA-172 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.1 (continued)
e. Yes
10. umask
f. Read, write, and execute for others
11. touch maskfile1 ls -l maskfile1 12. umask 000 umask 13. mkdir maskdir1 14. ls -ld maskfile1 maskdir1 or ls -ld mask*
g. 777 h. 660
i. 777 15. umask 002 16. umask 777 umask 17. touch maskfile2
mkdir maskdir2
143-MA-173 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.1
(continued)
18. ls -ld maskfile2 maskdir2
j. 000 for both
19. umask 002 20. chmod u=rwx mask*
rm -r mask* 21. ls permtest 22. chmod 000 permtest
less permtest
23. ln -s permtest permtest_link
k. permtest is 000; permtest_link is 777 l. permtest
24. less permtest_link 25. ls -lL permtest_link 26. mkdir -p dir1/dir2
27. chmod u=rwx,o=x dir1 chmod u=rwx,o= dir1/dir2
143-MA-174 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.1 (continued) 28. cp /143/linus dir1 chmod 777 dir1/linus 29. cp/143/linus dir1/dir2
chmod 000 dir1/dir2/linus 30. ls -lR dir1 31. chmod -R 751 dir1 32. ls -lR dir1
m. Yes
143-MA-175 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.2
1. ssh ltreesrv 2. hostname 3. less /etc/group 4. /student 5. mkdir extract_dir 6. ls -ld extract_dir
a. Primary group 7. chgrp student extract_dir 8. chmod g+ws extract_dir 9. ls -ld extract_dir
b. s for group execute
10. cp week_dir.tar.gz extract_dir ls –l extract_dir 11. id
12. su –l logadmin 13. id
143-MA-176 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.2 (continued) 14. cd /home/userN/extract_dir 15. tar –xvzf week_dir.tar.gz ls –l week_dir 16. exit id exit 17. passwd 18. su userN 19. exit
c. logadmin your own superuser
d. /etc/group
e. Primary group
143-MA-177 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.2
(continued) 20. mkdir stu_dir 21. chgrp student stu_dir
chmod g+s stu_dir ls -ld stu_dir
22. cp lintronica stu_dir 23. mv history.txt stu_dir
24. ls -l stu_dir
f. mv did not create a new file
25. chmod g-ws stu_dir ls -ld stu_dir
26. cp s1 stu_dir ls -l stu_dir
g. Primary group 27. chgrp -R userN stu_dir
143-MA-178 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 6.2 (continued) 28. man su su --command 'touch /tmp/sutest' logadmin
h. logadmin 29. ls -l /bin/su
i. Set user ID permission
30. ls -l s1 31. chmod u+s s1
j. An s in the execute position for user 32. rm s1
143-MA-179 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 7.1
1. cd locate monday
a. No 2. sudo updatedb 3. locate monday 4. locate day 5. find -name monday 6. find / -name monday
b. Yes 7. find /143 . -type d -name week39 8. find /143 . -type d -name week39 –ls
c. Search a database for any name containing log Search the current directory and below for anything named log Search the current directory and below for any file named log Search the root directory and below for any file containing log in its name modified three or fewer days ago
143-MA-180 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 7.1 (continued) 9. Done in GNOME 10. ln lintronica mylog1/linlink 11. ls –i lintronica 12. find . –inum N 13. find -type d -name *log* 14. find -type d -name '*log*'
15. find -type d -or -name '*log*'
d. All directories and names containing log
16. find -type d -or -name '*log*' -ls 17. find /home -name lintronica '(' -user user1 -or
-user user2 ')'
143-MA-181 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 7.2
1. grep userN /etc/group
a. student, userN 2. grep '^userN' /etc/group 3. hostname grep ltreeN.ltree.com /etc/hosts 4. grep 'FAILURE' week_dir/week40/* 5. grep –l 'FAILURE' week_dir/week40/* 6. cp /143/history.txt . grep -i linux history.txt
b. No No Yes
7. grep '[0-9]$' history.txt 8. grep '^198[2-6]' history.txt 9. grep '^198[2468]' history.txt 10. find /home -name lintronica -exec grep mirror {} ';'
143-MA-182 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 7.2 (continued) 11. Done using Nautilus 12. grep -v Linus /143/linus
13. grep --color Linus /143/linus 14. man grep
15. grep --color --after-context=1
--before-context=1 Linus /143/linus
143-MA-183 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.1
1. cd week_dir/week40 2. cat monday tuesday
a. No
3. cat *day
4. cat *day > whole_week
5. wc –l *
6. cat >> whole_week STOP END week40 ^D
b. End of file 7. tail -3 whole_week
143-MA-184 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.1 (continued) 8. cd ls -l | sort -n -k5
9. ls -l | sort -n -k5 | tail -1
10. ls -l | sort -n -k5 | head -2 11. sort -n -t: -k3 /etc/passwd 12. sort -n -t: -k3 /etc/passwd | tail -1 13. sort -n -t: -k3 /etc/passwd | less
c. Standard input, standard output, standard error
d. Redirect standard input from the file name on the right
Redirect standard output, appending or creating the file name on the right Redirect standard output, overwriting or creating the file name on the right Redirect standard output from the command on the left to standard input of the command on the right
14. tail –f follow_file
e. follow_file does not exist
143-MA-185 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.1
(continued)
15. touch follow_file tail –f follow_file
16. cat >> follow_file GOOD messages BAD messages MIXED messages ^D
f. Yes 17. ^C 18. tail –f follow_file | grep 'GOOD' 19. cat >> follow_file GOOD times BAD times OTHER times GOOD news BAD news NO news ^D
20. find / -name '*log*' 2>/dev/null > log_listing
21. find / -name '*log*' 2>/dev/null | less 22. sort follow_file
143-MA-186 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.1 (continued) 23. sort follow_file > follow_file
g. Truncated to zero length before sort
24. ls -s /etc | sort -nr | head -1
143-MA-187 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.2
1. tail -1 /etc/passwd
2. tail -1 /etc/passwd | sed 's/:/+/'
a. First 3. tail -1 /etc/passwd | sed 's/:/+/g'
b. All 4. ls -l | gawk '{ print $1, $9}' 5. ls -l | gawk '{ print $1, $5, $9}' 6. ls -l | gawk '{ print $5, $9, $1}' 7. grep 'ltreeN$' /etc/hosts | gawk '{ print $1 }'
c. ltrEeNN.ltree.com ltrEENN.ltrEE.com MMM DD YYYY
8. ls -l | gawk '{ print $5, $9, $1}' | sort -n 9. ls -l | gawk '{ print $5, $9, $1}' | sort -n |
tail -1
143-MA-188 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 8.2 (continued) 10. who | grep pts | gawk '{ print $1, $3, $4, $5}' 11. grep 'ltreeN$' /etc/hosts | gawk '{ print $1 }' |
gawk -F. '{ print $4 }' 12. gawk '{ print $2, $3, $4, $5, $6}' lintronica | grep '5' | gawk -F@ '{ print $2 }' 13. sed 's/apple/pear/g' An apple a day Keeps the doctor away Give the teacher an apple Are Snapples made from apples ^D
14. gawk '{ print $1 + $2 }' 10 20
5.5 2.3 5.6 0 ^D
143-MA-189 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 9.1
1. cp /143/w42_logger . vim w42_logger
2. :set number
3. 9G
4. <Right arrow> to digit 2
x
5. 15G
6. <Right arrow> to cd
a week42 <Esc>
7. 19G
<Right arrow> to space before week42.byday.log i > <Esc>
8. /compress <Enter>
9. i # <Esc> 10. :w <Enter>
11. /week42 n n n n n n n
a. Yes
143-MA-190 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 9.1 (continued) 12. 2G /w <Enter> 13. c$week43 <Esc> 14. :1,$s/week42/week43/g
b. All 15. :w w43_logger <Enter> 16. :q! <Enter> 17. diff w42_logger w43_logger
c. <Enter> d. <Esc>
18. cp /143/history.txt . vim history.txt 19. /Unix <Enter> n 20. G 21. ?Linus <Enter> 22. 1G
143-MA-191 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 9.1
(continued)
23. iTITLE <Esc> 24. G $
25. a FOOTNOTES <Esc> 26. :wq <Enter> 27. vim famous_email 28. :set number <Enter>
29. /minix <Enter>
30. n n 31. :1,9s/minix/MULTICS/
e. 3
f. No
32. :w newlog <Enter>
33. :q! <Enter> 34. diff newlog famous_email
143-MA-192 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-193 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.1
1. set –o
a. Off 2. set –o noclobber 3. set –o 4. ls > history.txt
b. No 5. set | grep PATH
c. Executable files 6. echo $PATH
7. sudo useradd newgal
8. echo $HOSTNAME $PWD 9. PS1=$HOSTNAME$PWD
10. PS1='\h \w \$ ' 11. cd /tmp cd $HOME
143-MA-194 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.1 (continued)
d. PS1 PATH PWD
12. echo $PATH 13. ifconfig 14. PATH= 15. echo $PATH
16. ls
e. No f. echo is built into the shell
17. PATH=/usr/bin:/bin 18. ls
143-MA-195 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.1
(continued)
19. ^D 20. set –o | grep history 21. set +o history 22. set –o 23. id hostname pwd
g. set +o history
24. set -o history 25. set –o set +o
h. set +o 26. set +o > saveopts
143-MA-196 © All rights reserved. Not to be reproduced without prior written consent.
143-MA-197 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.2
1. alias 2. alias rm='rm –i'
3. alias 4. Done with mouse 5. alias 6. vim .bashrc 7. bash alias
a. Yes 8. Done with mouse
alias
b. Yes 9. env | grep '^P'
c. No 10. ls s1
11. PATH=$PATH:.
143-MA-198 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.2 (continued) 12. echo $PATH
13. s1
14. vim .bash_profile
15. vim .bashrc 16. Done with GNOME 17. Done with GNOME 18. echo $PATH alias
d. /etc/profile and ~/.bash_profile e. ~/.bashrc f. Aliases are redefined with each new interactive shell
19. vim .bashrc bash set -o
g. Yes
20. vim .bash_logout 21. exit
143-MA-199 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.2
(continued)
22. cat logout
h. No 23. ssh $HOSTNAME yes exit
24. cat logout
i. Yes 25. vim .bash_profile
26. vim .bashrc 27. Done with GNOME 28. cat login
j. .bash_profile 29. tail -f login
30. bash
k. .bashrc
31. bash –-login
143-MA-200 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 10.2 (continued)
l. .bashrc, .bash_profile 32. bash –-noprofile
m. .bashrc 33. bash –-norc
n. Neither 34. bash –i
o. .bashrc 35. Done with GNOME 36. man bash
143-MA-201 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1
1. cd less bin/logpro4 2. logpro4 week39 week40 week41 week42 bash logpro4 week39 week40 week41 week42
a. $HOME/logfiles 3. ls logfiles
4. # Exercise.11.1.Step.4.EndPoint if (( $# == 0 )) then echo "$0 requires a weekNN directory name" exit 1 fi cd # return to home directory for week in "$@" do if cp -r "/143/$week" . # copy in 1 week of files then cd "$week" # change directory else echo "could not copy /143/$week" exit 1 fi
143-MA-202 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1 (continued) # create byday file with week days in order
# use sed to clean up domain names
cat {mon,tues,wednes,thurs,fri}day |
sed "s/.ltree.com/ $week /" > "$week.byday.log" # gzip the results and move to finish directory gzip "$week.byday.log" # create finished directory and move results there if [ -d "$HOME/logfiles" ] then mv "$week.byday.log.gz" "$HOME/logfiles" else mkdir "$HOME/logfiles" mv "$week.byday.log.gz" "$HOME/logfiles" fi echo "$HOME/logfiles/$week.byday.log.gz file created" cd rm -r "$week" done 5. logpro4 week39 week40 week41 week42
6. ls
143-MA-203 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1
(continued)
7. # Exercise.11.1.Step.7.EndPoint if (( $# == 0 )) then echo "$0 requires a weekNN directory name" exit 1 fi cd # return to home directory for week in "$@" do if cp -r "/143/$week" . # copy in 1 week of files then cd "$week" # change directory else echo "could not copy /143/$week" exit 1 fi
143-MA-204 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1 (continued) # create byday file with week days in order # use sed to clean up domain names cat {mon,tues,wednes,thurs,fri}day | sed "s/.ltree.com/ $week /" > "$week.byday.log" # grep for FAILURE grep FAILURE "$week.byday.log" > "$week.byday.FAILURE" # gzip the results and move to finish directory gzip "$week.byday.log" "$week.byday.FAILURE" # create finished directory and move results there if [ -d "$HOME/logfiles" ] then mv "$week.byday.log.gz" "$week.byday.FAILURE.gz" "$HOME/logfiles" else mkdir "$HOME/logfiles" mv "$week.byday.log.gz" "$week.byday.FAILURE.gz" "$HOME/logfiles" fi echo "$HOME/logfiles/$week.byday.log.gz file created" cd rm -r "$week" done
8. logpro4 week39 week40 week41 week42 ls logfiles
b. Test condition for true or false Repeat commands Comment, ignore anything following on that line
Line wraps around on
page
Line wraps around on
page
143-MA-205 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1
(continued)
9. less bin/readlog1
10. # Exercise.11.1.Step.10.EndPoint if (( $# == 0 )) then echo "$0 requires a file name: weekNN" exit 1 fi if [ -d "$HOME/logfiles" ] then cd "$HOME/logfiles" for week in "$@" do if [ -f "$week.byday.log.gz" ] then zcat "$week.byday.log.gz" | less else echo "file $HOME/logfiles/$week.byday.log.gz not found" fi done else echo "directory $HOME/logfiles not found" exit 1 fi
Line wraps around on
page
143-MA-206 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1 (continued) 11. readlog1
readlog1 week39 readlog1 week40 week41 week42
143-MA-207 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1
(continued) 12. cp bin/readlog1 bin/errlog1 vi bin/errlog1 # Exercise.11.1.Step.12.EndPoint if (( $# == 0 )) then echo "$0 requires a file name: weekNN" exit 1 fi for week in "$@" do if [ -d "$HOME/logfiles" ] then cd "$HOME/logfiles" if [ -f "$week.byday.log.gz" ] then zcat "$week.byday.log.gz" | less else echo "file $HOME/logfiles/$week not found" exit 1 fi else echo "directory $HOME/logfiles not found" exit 1 fi done
143-MA-208 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 11.1 (continued) 13. errlog1 week40 errlog1 week41 week42 14. vim bin/myrm # Exercise.11.1.Step.14.EndPoint if ! [ -d "$HOME/.deleted" ] then mkdir "$HOME/.deleted" fi # copy the file into the .deleted directory for name in "$@" do if cp -rb "$name" "$HOME/.deleted" then rm -r "$name" echo "$name deleted" else echo "$name could not be removed" fi done
chmod u+x bin/myrm 15. myrm history.txt myrm logfiles lintronica ls .deleted
16. alias rm=$HOME/bin/myrm rm linus
143-MA-209 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 12.1
1. gedit & 2. ps 3. Done with GNOME 4. ps
a. No 5. ps -ef | grep gedit 6. ps aux | grep gedit 7. pgrep gedit
b. Yes 8. kill pid 9. log_chk 10. ^C
11. Done with GNOME 12. pgrep log_chk 13. kill pid
14. kill -9 pid
143-MA-210 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 12.1 (continued)
c. Processes in the current shell All processes All processes with the string bash The PID of any bash process
15. man top 16. top -d 5 17. d 1 18. u root 19. u
20. h
21. l t m
22. h 23. f
143-MA-211 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 12.1
(continued)
d. Yes 24. h
e. Write the configuration file
25. W
f. ~/.toprc
26. q 27. cat ~/.toprc
g. Let top do the work
28. top
h. Yes
29. sleep --help
30. sleep 500 &
143-MA-212 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 12.1 (continued) 31. Logout and Login again 32. pgrep sleep
kill PID
33. shopt -s huponexit sleep 500 & 34. sleep 500 &
Logout and Login again pgrep sleep
i. No
35. shopt -s huponexit nohup sleep 500 & 36. Logout and Login again
pgrep sleep
j. Yes
143-MA-213 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 14.1
1. Done with GNOME 2. Done with GNOME 3. whoami pwd ls -al
id 4. uname -a hostname df –h 5. ifconfig ping -c1 ltresrv 6. Done with GNOME 7. sudo tar –xvjPf /L*/1* sudo chown -R teamN:teamN /143 [a-z]* 8. scp userN@ltreesrv:/home/userN/week_dir.tar.gz . 9. tar –xvzf week_dir.tar.gz
10. ls -l 11. sudo useradd -m partner1 sudo passwd partner1
143-MA-214 © All rights reserved. Not to be reproduced without prior written consent.
Solutions to Hands-On Exercise 14.1 (continued) 12. su - partner1
id exit
13. Done with GNOME 14. Done with GNOME