Changes

Jump to: navigation, search

OPS705 Lab 2 (2227)

2,049 bytes added, 02:17, 8 January 2023
m
Chris.johnson moved page OPS705 Lab 2 to OPS705 Lab 2 (2227) without leaving a redirect
# Understand the purpose of '''directories''' and '''directory pathnames'''.
# Use common Linux commands to perform '''directory management tasks'''.
#Use Understand the difference between '''text editorsabsolute''' , '''relative''' to create and modify text files.# Use common Linux commands to '''manage and manipulate text filesrelative-to-home'''.pathnames# Become productive at issuing Linux commands with the most appropriate pathname
If you encounter technical issues, please contact your professor via e-mail or in your section's Microsoft Teams group.
In the Linux OS, the '''root directory''' / is the starting directory, and other ''child directories'', ''grandchild directories'', etc. can be created as required. This hierarchical structure resembles an "''upside-down tree''". There is actually a command called <code>tree</code> that displays a '''directory tree diagram'''!
 
* '''NOTE''': To install the ''tree'' command: <code>sudo yum install tree</code>
=== Directory Pathnames ===
| '''/usr/bin''' || Common utilities (commands) for users.
|-
| "'''/usr/sbin''' || Common utilities for system administration
|-
| '''/etc''' || System administration files (eg. passwd)
When you log into your Linux user account, you are automatically directed to your '''home''' directory. This directory is where the user can store files and create subdirectories to organize their files.
 
=== Pathname Types ===
There are different types of file pathnames that we can use to access a directory or text file.
 
For Example:
* '''Absolute Pathname''': <code>/home/userid/uli101/cars.txt</code>
* '''Relative Pathname''': <code>uli101/cars.txt</code> (this assumes you are in your home directory)
* '''Relative-to-Home Pathname''': <code>~/uli101/cars.txt</code>
 
These types of file pathnames can make it more efficient (i.e. less keystrokes for users to type) when issuing Unix and Linux commands.
 
==== Absolute Pathnames ====
<table align="right"><tr><td>[[Image:absolute-path-2.png|thumb|right|250px|The directory in red displays the FULL path from the '''root''' directory to the '''bin''' directory (i.e. the absolute pathname: '''/bin'''.]]</td><td>[[Image:absolute-path-1.png|thumb|right|250px|Directories in red display the FULL path from the '''root''' directory to the '''examples''' directory (i.e. the absolute pathname: '''/home/your-user-id/uli101/examples'''.]]</td></tr><tr><td>[[Image:relative-pathname-1.png|thumb|right|250px|Directories in red display the path from the current directory location (which is '''xyz100''') to the '''bin''' directory (i.e. the relative pathname: '''../../../bin'''.]]</td><td>[[Image:relative-pathname-2.png|thumb|right|250px|Directories in red display the path from the current directory location (which is '''uli101''') to the '''examples''' directory (i.e. the relative pathname: '''uli101/examples''' or ./uli101/examples]]</td></tr><tr><td>[[Image:relative-to-home-1.png|thumb|right|250px|Directories in red display the path from the home directory of the current user (which is '''userid''') to the '''examples''' directory (i.e. the relative-to-home pathname: '''~/uli101/examples'''.]]</td><td>[[Image:relative-to-home-2.png|thumb|right|250px|Directories in red display the path from another user's home directory location to their '''notes''' directory (i.e. the relative-to-home pathname: '''~otheruserid/uli101/notes''']]</td></tr></table>
An '''absolute pathname''' is a path to a file or directory '''always beginning from the root directory''' (i.e. <code>/</code>).
 
This type of pathname is referred to as '''absolute''' because the pathname always begins from the '''root directory''', ''regardless the location or your current directory''. In other words, this type of pathname requires that you always provide the '''FULL''' pathname starting with the root directory.
 
Advantages of using Absolute Pathnames:
* Useful if you do not know your current directory location
* Understand the location of file within the filesystem.
 
Examples:
* <code>ls /bin</code>
* <code>ls /home/your-user-id/uli101/examples</code>
 
==== Relative Pathnames ====
A '''relative pathname''' is a path to a file or directory that begins from your '''current''' directory. This is called relative because it is used to locate a specific file ''relative to your current directory''.
 
'''NOTE:''' In order to use relative pathnames, it is absolutely necessary that you know the location of your current directory!
 
Relative Pathname Symbols:
* <code>.</code> A period symbol "." represents the '''current''' directory
* <code>..</code> Two consecutive period symbols ".." represents the '''parent directory''' (i.e. one level up)
 
Advantages of using Relative Pathnames:
* Possible shorter pathname (less typing)
 
Examples:
* <code>ls ../../../bin</code>
* <code>ls examples</code>
* <code>ls ./examples</code>
 
==== Relative-to-Home Pathnames ====
A '''relative-to-home''' pathname begins with the tilde character ( i.e. '''~''') to represent the user’s home directory.
 
The tilde character <code>~</code> stores the path of the user’s home directory (i.e. '''~''' = '''/home/current-user-id''')
 
You can immediately place a username after the tilde to represent another user’s home directory (e.g. '''~otheruserid''' = '''/home/otheruseid''')
 
Examples:
* <code>ls ~/uli101/examples</code>
* <code>ls ~otheruserid/uli101/notes</code>
 
'''NOTE''': Deciding which '''type of pathname''' to use depends on many factors including:
# Knowledge of current directory
# Knowledge of directory structure
# Currently directory location
# Type of file management command that is being used
= INVESTIGATION 1: Creating and Managing Directories =
#* If so, what does this say about how to use multiple options for Linux commands?
# Issue the following Linux command to create the acp100xx and xyz100xx directories: <code>mkdir /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</code>
#* '''NOTE:''' You can create multiple directories by issuing the <code>mkdir</code> command with multiple arguments.('''Make sure there's a space between each path!''')
# Issue the following Linux command to confirm that those directories have been created: <code>ls -ld /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</code>
#* Using a FULL pathname starting from the root directory (i.e. /) requires is a LOT of typing!
Perform the following steps:
<table align="right"><tr valign="top"><td>[[Image:script-check-1.png|thumb|right|250px|If all check checks pass, then user performed task correctly and can continue.]]</td><td>[[Image:script-check-2.png|thumb|right|450px|If there is a warning, then feedback is provided to user to correct and re-run checking script.]]</td></table>
# Make certain that your current directory is your home directory.
# Create a new directory called '''bin''': <code>mkdir bin</code>
# Download the Part 1 checking scriptInstall git: <code>wget urlhere sudo yum -P y install git</code># Using git, download all lab check scripts into your bin directory: <code>git clone https://github.com/ops705/labs.git /home/your-seneca-id/bin/</code># Move back to your home directory.# Run the checking script for Part 1: <code>check_lab2-part1labcheck2.1.sh</code>
#* If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.
#* If you receive a congratulations message that there are no errors, then proceed to the next part.
# Assuming you received a '''congratulations''' message, take a screenshot of the full script output. You'll need it for later.
==Part 2: Viewing Directory Contents / Copying & Moving Directories==
# Display the directory structure of your home directory to confirm you copied the uli101xx directory: <code>tree</code>
# Issue the following Linux command: <code>mv acp100xx xyz100xx/uli101xx/tutorials</code>
# Display the directory structure of your home directory to confirm you moved the acp100xx directory: </code>tree</code>
'''Let's run a checking script to see if you moved and copied the directories correctly:'''
# Make certain that your current directory is your home directory.
# Download Run the checking script for Part 2 checking script: <code>wget urlhere -P /home/your-seneca-id/bin/labcheck2.2.sh</code># Run Take a screenshot of the checking full script output. You'll need it for Part 2: <code>check_lab2-part2</code>later.
If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message.
# Make certain that your current directory is your home directory.
# Issue the <code>tree</code> command to confirm that all of those recently created directories have been removed.
# Download the Part 3 checking script: <code>wget urlhere -P /home/your-seneca-id/bin/</code># Run the checking script for Part 3: <code>check_lab2-part3labcheck2.3.sh</code>
If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message.
Proceed to the next investigation.
=INVESTIGATION 2: Managing Text FilesAbsolute / Relative / Relative-to-Home Pathnames=As you will be working in the The best way to learn about different types of file pathnames is to use them while issuing Linux command line environment, it commands and see which pathnames (or combination of pathnames) is useful to learn a the most efficient (i.e. requiring the least one common command-line text editorsnumber of keystrokes).
Although programmers and developers usually use graphical IDE's to code and compile programs In the following investigation (Visual Studio, Sublime, Eclipse, etcParts 1-3), they can you will create source code the exact same directory structure each time, but using a text editor and compile their code directly on the server to generate executable programs (without having to transfer them for compilation or execution)different path type.
Developers very often use First, let's confirm:# Connect to your CentOS VM's CLI# Issue a text editor command to modify configuration files. In this course, confirm you will become familiar with the process of installing, configuring, and running network services. Text editors are an important tool to help this setup, but are also used to "tweak" or make periodic changes located in service configurationsyour '''home''' directory.
The two most readily-available == Part 1: Using Absolute Pathnames ==[[Image:dir12.png|right|350px|thumb|In this investigation, you will create the blue sections of this tree diagram.]]Let's create the following directory structure under your home directory by issuing the <code>mkdir</code> command line text editors in Linux are '''nano''' and using only '''viabsolute'''pathnames.
The nano text editor would seem like an easier-# Issue the following Linux command to create the directory structure displayed tothe right using absolute pathnames: <code> mkdir -use p /home/youruserid/tutorial3/practice/commands /home/youruserid/tutorial3/practice/examples /home/youruserid/tutorial3/notes/lesson1 /home/youruserid/tutorial3/notes/lesson2</code>#* ''NOTE: Just continue typing and let the text editor, but vi (although taking longer continue of separate lines. Remember to learn) has outstanding features and allow replace the text "youruserid" with your actual Seneca username.''# Confirm that you properly created the user to be more productive directory structure: <code>tree /home/youruserid/tutorial3</code>#* You should notice that using absolute pathnames with editing text filesthis Linux command requires a lot of typing.
==Part 12: Creating Text Files Using The vi Text EditorRelative-to-Home Pathnames ==[[Image:vi-text-editor-2.png|right|250px|thumb|The online vi-tutorial provides users "hands-on" experience of using the vi text editor.]]You will now learn basic editing skills using the vi (vim) text editor including creating, editingLet's remove this directory structure, and saving text files. As mentioned, issue the vim text editor (although taking longer same command using a '''relative-to learn) has outstanding features to increase coding productivity-home''' pathname instead.
# To remove this directory structure, issue the following Linux command ('''Vim''' is a newer version enter "y" at each prompt to remove ALL contents): <code>rm -ri /home/youruserid/tutorial3</code># Confirm that the directory structure has been removed using the <code>tree</code> command.# Create the same directory structure using relative-to-home pathnames: <code>mkdir -p ~/tutorial3/practice/commands ~/tutorial3/practice/examples ~/tutorial3/notes/lesson1 ~/tutorial3/notes/lesson2</code>#* NOTE: You usually generate the ~ character by Holding down SHIFT and press the button to the left of vi with expanded featuresthe number 1 above the text on your keyboard. We will be #* Did this command require less typing than the previous command using vim in this course. In most cases, vi and vim can be used interchangeably hereabsolute pathnames?# Issue the tree command to confirm the directory structure was properly created.)
The big thing to remember with vim is that it starts in == Part 3: Using Relative Pathnames ==Let's remove the ''COMMANDtutorial3''' mode. You need to directory and its contents and issue letter commands to change modes (including to enter plain text into the document). Also, you can press colon “: ” in COMMAND mode to enter more complex commandssame command using '''relative''' pathnames.
# Issue the same command as you did in step #5 to remove the ''tutorial3'' directory and its contents safely.# Issue a Linux command to confirm you removed the 'Resource:'tutorial3'' [httpsdirectory and its contents.# Create the same directory structure using relative pathnames:<code>mkdir -p tutorial3/practice/cloud.netlifyusercontent.comcommands tutorial3/practice/examples tutorial3/notes/assetslesson1 tutorial3/344dbf88-fdf9-42bb-adb4-46f01eedd629notes/04650c56-ae63-4c1c-8f3c-0cb5f38ebb6blesson2</vi-help-sheet-011code># Issue a command to verify that the proper directory structure was created.pdf vi Cheat Sheet (PDF)]
An online tutorial has been created You may think that issuing Linux file management commands are better using relative or relative-to give you "hands-on" experience on how to use vi text editor. It home pathnames instead of absolute pathnames, but that is recommended that you run this interactive tutorial in your Linux account to learn how to create and edit text files with the vi text editornot always true.
Since the current directory location was your home directory, then it makes sense to use relative or relative-to-home pathnames. On the other hand, what if we changed the location to a different directory?
 
'''Let's run a checking script to verify you created the Part 1-3 directories correctly:'''
# Make certain that your current directory is your home directory.
# Download Issue the interactive vi tutorial: <code>wget vi-tutorial -P /home/your-seneca-id/bin/tree</code>command to confirm that all of those recently created directories have been removed.# Run the interactive tutorialchecking script for Parts 1-3: <code>vi-tutoriallabcheck2.4.sh</code># In the tutorial menu, select the first menu item labelled "USING THE VI TEXT EDITOR"# Read and follow the instructions in the tutorial. Eventually, it will display a simulated vi environment and will provide you with "hands-on" practice using the vi text editor.# When you have completed that section, you will be returned to the main menu.# If you want to get extra practice, you can select the menu item labelled "REVIEW EXERCISE".# When you want to exit the tutorial, select the menu option to exit the tutorial.
[[Image:viIf you encounter errors, make corrections and re-text-editor-b.png|right|200px|thumb|Fig. 10: Contents of ''othertext.txt'']]After run the checking script until you have completed the tutorial:# Using vim, create receive a new text file called ''othertext.txt'' in your home directory.# Write the text shown in ''Fig. 10'' to your new ''othertext.txt'' file, save, and quitcongratulations message.
==Part 24: Manage and Manipulate Text File ContentPathing Practice ==We conclude [[Image:dir12.png|right|350px|thumb|This diagram should reflect your own directory structure before beginning Part 4. Refer to this tutorial by learning tree diagram throughout Part 4 to '''manage'''help orient yourself.]]When performing the next series of steps, '''view''' refer to the tree diagram on the right. Learning to reference a tree diagram on a quiz, midterm or '''manipulate the display''' final exam can help to prevent errors and loss of text files.marks!
This Perform the following:# Change to the examples directory in your recently-created directory structure: <code>cd tutorial3/practice/examples</code># Confirm you are located in the ''examples'' directory.# Remembering that we are located in the ''examples'' directory, issue the following Linux command using a relative pathname to display files in the ''/bin'' directory: <code>ls ../../../../../bin</code># Now issue the following Linux command using an absolute pathname: <code>ls /bin</code>#* '''Which type of pathname would be the best to use in this situation?'''# Let's copy the file called ''ls'' which is contained in the ''/bin'' directory to your home directory by using the <code>cp</code> command.# First, copy the ''ls'' command from the ''/bin'' directory to your home directory using absolute pathnames: <code>cp /bin/ls /home/youruserid</code># Now let's issue the previous command using just relative pathname (remember, our current directory location is ''examples''): <code>cp ../../../../../bin/ls ../..</code>#* '''TIP: For relative pathnames that move up multiple parent directories such as these, it is HIGHLY ADVISED in case you only want RECOMMENDED to view contents the tree diagram and check for the correct number of .. symbols. Students commonly make mistakes and lose marks on these type of questions!'''NOT# Let's issue the command using one absolute pathname and a relative pathname: <code>cp /bin/ls ../..</code>#*What did this command do?# Let's issue the same command using one absolute pathname and a relative-to-home pathname: <code>cp /bin/ls ~</code>#* What did this command do?# Let's copy the ''ls'' edit text file contents which can cause accidental erasure of datafrom the ''/bin'' directory to your current directory (i.e. examples): <code>cp /bin/ls .</code># Issue the following Linux command: <code>cp /bin/ls ./ls.bk</code>#* What does this command do?
Refer '''Let's run a checking script to make certain you performed the following table recently-issued commands correctly:'''# Make certain that your current directory is your home directory.# Issue the <code>tree</code> command to confirm that all of Text File Management Commandsthose recently created directories have been removed.# Run the checking script for Part 4:<code>labcheck2.5.sh</code>
{| class="wikitable" style="margin-left: 0px; margin-right:auto;"! Linux Command !! Purpose !! Example|-| touch || Create empty file(s) / Updates Existing File's Date/Time Stamp || <code>touch otherfile.txt</code>|-| cat || Display text file's contents without editing (small files) || <code>cat otherfile.txt</code>|-| more , less || Display / Navigate within large text files without editing || <code>less otherfile.txt</code>|-| cp || Copy text file(s) || <code>cp otherfile.txt uli101xx/otherfile.txt</code>|-| mv || Move / Rename text files || <code>mv otherfile.txt uli101xx/otherfile.txt</code>|-| rm || Remove text file(s) || <code>rm otherfile.txt</code>|-| sort || Sorts (rearranges) order of file contents when displayed. Content is sorted alphabetically by default. The -nItalic text option sorts numerically, -r performs a reverse sort || <code>sort otherfile.txt</code>|-| head || Displays the '''first''' 10 lines of a text file by default. An option using a number value will display that number of lines (e.g. <code>head -5 filename</code> will display first 5 lines). || <code>head -2 otherfile.txt</code>|-| tail || Displays the '''last''' 10 lines of a text file by default. An option using a number value will display that number of lines (e.g. <code>tail -5 filename</code> will display last 5 lines). || <code>tail -2 otherfile.txt</code>|-| grep || Displays file contents that match a pattern || <code>grep "third" otherfile.txt</code>|-| uniq || Displays identical consecutive lines only once || <code>uniq otherfile.txt</code>|-| diff file1 file2 || Displays differences between 2 files || <code>diff otherfile.txt otherfile2.txt</code>|-| file || Gives info about the contents of the file (e.g. file with no extension). || <code>file othertext.txt</code>|} Perform the following steps:# Make certain that If you are located in your home directory.# Create three empty text files in your current directory: <code>touch a.txt b.txt c.txt</code># Issue the following Linux command: <code>ls -l a.txt b.txt c.txt</code>#* Check the size in the detailed listing to confirm that these newly-created files are empty. [[Image:vi-screen-40.png|right|300px|thumb|Nano text editor containing numbers '''1 to 40'''<br>on separate lines.]]# Use the vim text editor to edit the empty file called ''a.txt''.# Type the number "1" and press ENTER. On the second lineencounter errors, type the number "2" and press ENTER.# Continue entering increasing number values until you reach the number 40 on line 40. (refer to the diagram on the right).# Save and exit your editing session.# Issue the following Linux command: <code>cat a.txt</code>#* Can you see all of the contents?# Issue the following Linux command: <code>more a.txt</code>#* NOTE: The <code>more</code> command uses the same navigation keys as with the <code>man</code> command. Try using keys that you used to navigate the manual pages.#* What is the advantage of using the more command?# Type the letter '''q''' to exit the more command.# Issue the following Linux command: <code>less a.txt</code>#* Is there any difference between the more make corrections and less commands?# Type the letter '''q''' to exit the less command.# Issue the following Linux command: <code>sort a.txt</code>#* Why does the output not look what you expected? Why?# Issue the following Linux command: <code>sort re-n a.txt</code># Try run the same command using both the <code>-n</code> and <code>-r</code> options to see what happens.# Issue the following Linux command: <code>head a.txt</code>#* What is the output from this command display?# Issue the following Linux command: <code>head -7 a.txt</code>#* What is the output from this command display?# Issue the following Linux command: <code>tail a.txt</code>#* What is the output from this command display?#* How would you issue this command to display only the last line contained in that file?# Issue the following Linux command: <code>grep 2 a.txt</code> [[Image:end-of-line.png|right|200px|thumb|Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting of the <u>same</u> text: "'''end of line'''".]]#* What type of output appear? Why did these lines appear (what do they all have in common)?# Edit the ''a.txt'' file and add to the bottom 5 new lines each consisting of the same text: "end of line" (refer to diagram on right).# Save your editing session and exit your text editor.# Issue the following Linux command: <code>uniq a.txt</code>#* What do checking script until you notice happened to those newly created lines?# Issue the following Linux command: <code>cp receive a.txt a.txt.bk</code># Issue the following Linux command: <code>cp a.txt b.txt</code># Issue the following Linux command: <code>mv a.txt aa.txt</code># Issue a Linux command to view the directory contents.#* What happened to the file called ''a.txt''? Why?# Issue the following Linux command: <code>file b.txt</code>#* What sort of information did it provide?# Issue the following Linux command: <code>diff aa.txt b.txt</code>#* Was there any output? If not, why?# Issue the following Linux command: <code>diff aa.txt c.txt</code>#* What do you think is the purpose of this output?# Issue the following Linux command: <code>find -P .</code>#* What is the output of this command?# Issue the following Linux command: <code>rm aa.txt b.txt a.txt.bk c.txt</code># Issue the <code>ls</code> command to verify that these files have been removedcongratulations message.
= Lab Submission =
When complete, show your instructor Submit to Blackboard full-desktop screenshots (PNG/JPG) of the following in-class:# A successful run of '''check_lab2-part1labcheck2.1.sh'''# A successful run of '''labcheck2.2.sh'''# A successful run of '''check_lab2-part2labcheck2.3.sh'''# A successful run of '''check_lab2-part3labcheck2.4.sh'''# In vim, the completed version A successful run of ''othertext'labcheck2.5.txtsh'''
Make sure to fully stop your VMs when you're done!
 
= Licensing =
Author: Chris Johnson
 
License: [https://www.gnu.org/licenses/lgpl.html LGPL version 3]
[[Category:OPS705]]
[[Category:OPS705 Labs]]
[[Category:Digital Classroom]]
[[Category:Fall 2022]]

Navigation menu