Changes

Jump to: navigation, search

OPS705 Lab 2 (2227)

26,778 bytes added, 23:23, 10 September 2022
Created page with "= LAB PREPARATION = === Purpose / Objectives of Lab 2 === Understanding how to use a command line interface (CLI) is an incredibly useful skillset as application developers...."
= LAB PREPARATION =

=== Purpose / Objectives of Lab 2 ===
Understanding how to use a command line interface (CLI) is an incredibly useful skillset as application developers. It is present in all major operating systems, and is often how you will interact with tools and databases to accomplish tasks a GUI doesn't provide.

In this lab, you will practice working with the Linux command line environment inside your Azure CentOS VM to:
# Understand the purpose of '''directories''' and '''directory pathnames'''.
# Use common Linux commands to perform '''directory management tasks'''.
#Use '''text editors''' to create and modify text files.
# Use common Linux commands to '''manage and manipulate text files'''.

If you encounter technical issues, please contact your professor via e-mail or in your section's Microsoft Teams group.

=== Minimum Requirements ===
Before beginning, you must have:
# Successfully completed [[OPS705_Lab_1 | Lab 1]]
# Attended the Week 2 class lecture
# Read through the Week 2 slides, and have them handy as a reference for concepts
# Your ''username''-lnx CentOS VM in Azure
# Your Seneca Azure login credentials
# Your linked mobile device for 2FA

= KEY CONCEPTS =
===Linux Directories===

[[Image:directory-structure-1.png|thumb|right|350px|In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as '''C:''', or '''D:''').<br><br> All files and directories appear under a single ancestor directory called "the '''root''' directory".]]
To better '''organize''' files (text, images, documents, spreadsheets, programs, etc) within your Linux system, they should be stored in '''directories'''. To further organize many files, directories may contain '''sub-directories'''.

The Linux file system is '''hierarchical''', like other operating systems such as '''Windows''', '''macOS''', etc.
In Linux (as opposed to Windows), there are no drive letters (such as '''C:''', or '''D:'''). All files and directories appear under a single ancestor directory called the "'''root directory'''".

Learning how to issue Linux commands for '''navigating''' and '''manipulating''' directory and files within the the Linux filesystem are '''essential skills''' for Linux users and Linux system administrators (i.e. ''sysadmins'').

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'''!

=== Directory Pathnames ===
[[Image:path-name.png|thumb|right|180px|A pathname points to a file system location by '''following the directory tree hierarchy'''.]]
<blockquote>A '''pathname''' is used to specify a '''route to a file''' within the file system.

A pathname '''points''' to a file system location by '''following the directory tree hierarchy''' expressed in a string of characters in which path components, separated by a delimiting character, represent each directory. The '''delimiting character''' is most commonly the slash ("'''/'''"). (Reference: [[https://en.wikipedia.org/wiki/Path_(computing)]])</blockquote>

The following table displays and defines '''commonly used directories''' (listed by directory pathname) for for ALL Linux filesystems:

{| class="wikitable" style="margin-left: 0px; margin-right:auto;"
! Directory Pathname !! Purpose
|-
| '''/''' || Root directory (ancestor to all directories) || rowspan="10" | [[Image:directory-structure-2.png|thumb|center|500px|'''Common directory pathnames''' for for ALL Linux Filesystems.]]
|-
| '''/home''' || Used to store all users’ home directories
|-
| '''/home/username/ || A '''specific''' user's home directory
|-
| '''/bin''' || Common system binaries (commands)
|-
| '''/usr/bin''' || Common utilities (commands) for users.
|-
| "'/usr/sbin''' || Common utilities for system administration
|-
| '''/etc''' || System administration files (eg. passwd)
|-
| '''/var''' || Dynamic files (log and mail files)
|-
| '''/tmp''' , '''/var/tmp''' || Temporary files for programs
|-
| '''/dev''' || Device driver files (terminals, printers, etc.)
|}

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.

= INVESTIGATION 1: Creating and Managing Directories =
In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove directories''' in your user account.

'''Directory File Naming Rules'''

Before you learn how to create directories, it is important to understand what represents an appropriate '''directory filename'''.

Listed below are some common '''file naming rules''':

* Linux characters are '''case-sensitive'''. It is recommended to be consistent (e.g. use all lowercase letters)
* Adopt a '''consistent directory naming scheme''' (this will help you to navigate within your directory structure)
* Make your directory names '''meaningful'''
* '''Avoid non-alphanumeric characters''', as they may have a special meaning to the system that will make your work more difficult when changing to directories, etc.
* '''Avoid using spaces''' for directory names (consider periods, hyphens, and underscores instead)

== Part 1: Directory Creation ==
[[Image:directory-structure-3.png|thumb|right|350px|'''Fig. 4: Tree diagram''' of directory structure to create in your '''home''' directory (displayed in blue text).]]

Creating subdirectories within your home directory makes it more '''efficient''' to save and access files on your Linux server.

A comparison would be rooms in a house. If there were no rooms, just one large room in a 3,000 square foot house, it would be "messy" and difficult to locate items. Each room in a house is used to for a specific purpose to be more productive to perform a task such as a kitchen, bedroom, bathroom, etc.

You will be creating the following directory structure within your home directory (refer to diagram on the right side (''Fig. 4'')).

===Creating Directories===
We will now create our first level of directories.

{{Admon/important|IMPORTANT: For the following commands, you will be using ''your'' Seneca username to replace the text "your-seneca-id" in a pathname.|For example, if your Seneca user ID was '''mkhan''' then you would write the pathname ''/home/your-seneca-id'' as: '''/home/mkhan'''}}
'''Perform the following steps:'''
# SSH into your CentOS Linux account (Refer to Week 2 slides).
# Issue a command to confirm that you are located in your home directory (''/home/your-seneca-id''): <code>pwd</code>
# Issue the following Linux command: <code>mkdir /home/your-seneca-id/uli101xx</code>
#* NOTE: You should always confirm that you have created a directory. This can be done by issuing the <code>ls</code> command. [[Image:directory-structure-4.png|thumb|right|350px|'''Creating''' and '''confirming''' the creation of a directory.]]
# Issue the following Linux command: <code>ls /home/your-seneca-id/uli101xx</code>
#* There are no contents that are contained in this newly-created directory; therefore, no contents appear. A useful option -d can be used to confirm that the actual directory has been created as opposed to viewing the contents of the directory.
# Issue the following Linux command: <code>ls -d /home/your-seneca-id/uli101xx</code>
#* You should now see just the directory listed. You can also combine the <code>-d</code> and <code>-l</code> options to provide more detail regarding the newly-created directory.
# Issue the following Linux command: <code>ls -d -l /home/your-seneca-id/uli101xx</code>
#* How can you confirm from the output of this command that the file uli101xx is a directory?
# Issue the following Linux command: <code>ls -ld /home/your-seneca-id/uli101xx</code>
#* Is the output from this command the same was the output from the previous command?
#* 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.
# 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!
#* Since we are already located in our home directory, we don't have to start from the root directory. This is called a "relative" pathname. See the next instruction for an example.

===Creating Subdirectories===
We will now create the subdirectories that are contained in the ''uli101xx'' directory:

# Move to the ''uli101xx'' directory using a relative pathname: <code>cd uli101xx</code>
# Confirm that your current location is in the ''uli101xx'' directory (''/home/your-seneca-id/uli101xx''): <code>pwd</code>
# Create the directories called notes, tutorials and samples: <code>mkdir notes tutorials samples</code>
# Confirm that those directories have been created: <code>ls</code>

There are better ways to create the same directory structure without using the <code>cd</code> command.

===Creating Subdirectories Without <code>cd</code>===
Issuing the <code>cd</code> command prior to issuing other commands indicates to others that you are a novice or "newbie". You can actually lose marks on quizzes or tests if you issue multiple Linux commands to performed tasks that can be performed using a single Linux command with pathnames.

Here we'll show you how to create directories and subdirectories without having to move inside them.

First, return to your home directory:
# Return to your home directory: <code>cd</code>
# Confirm that you are now located in your home directory (''/home/your-seneca-id''): <code>pwd</code>

Although we will teach you how to remove directories and their contents in another investigation, let's remove the created directory structure and recreate the same directory structure using just one command using pathnames:
# Remove all of the directories that you have created: <code>rm -r uli101xx acp100xx xyz100xx</code>
# Confirm that the ''uli101xx'', ''acp100xx'' and ''xyz100xx'' have been removed: <code>ls</code>
# Issue the following '''single Linux command''' to create the entire directory structure: <code>mkdir -p uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx</code>
#* '''NOTE:''' The -p option allows "parent" directories to be automatically created first to then create their subdirectories.
# Confirm that all of the directories have been created: <code>ls -ld uli101xx/ uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx</code>

===Running a Shell Script to Check Your Work===

Although you are being asked to create the directory structure, you might have made some mistakes.

For example:
* Forgetting to create a directory.
* Making syntax errors (eg. spelling or mixing up character case)
* Not creating subdirectories within specified directories.

To check for mistakes, a '''shell script''' has been created to check your work. '''If the checking shell script detects an error''', then it will provide feedback and offer constructive feedback on how to fix that problem so you can re-run the checking shell scripts until your work is correct.

Perform the following steps:
<table align="right"><tr valign="top"><td>[[Image:script-check-1.png|thumb|right|250px|If all check 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 script: <code>wget urlhere -P /home/your-seneca-id/bin/</code>
# Move back to your home directory.
# Run the checking script for Part 1: <code>check_lab2-part1</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.

==Part 2: Viewing Directory Contents / Copying & Moving Directories==
Now that you have learned how to efficiently create your directory structure, you will now learn how to issue Linux commands to '''view directory contents''' as well as '''copy''' and '''move''' directories.

Perform the following steps:
<table align="right"><tr valign="top"><td>[[Image:directory-structure-5.png|thumb|right|250px|Output of the '''tree''' command to display directory structure.]]</td><td>[[Image:directory-structure-6.png|thumb|right|250px|Output of the '''ls -R''' command to display directory structure.]]</td></tr><tr valign="top"><td>[[Image:directory-structure-7.png|thumb|right|250px|Output of the '''tree''' command to confirm copy of '''uli101xx''' directory (and contents) to the '''xyz100xx''' directory.]]</td><td>[[Image:directory-structure-8.png|thumb|right|300px|Output of the '''tree''' command to confirm movement of '''acp100 directory''' directory (and contents) to the '''xyz100xx''' diredtory.]]</td></tr></table>
# Issue the following Linux command: <code>tree</code>
#* '''NOTE:''' You should see the directory structure that you created in the previous investigation. You can also issue the tree command combined with a directory pathname to display the directory structure for a specific scope.
#* You can also use the -R option for the ls command to display all directories and subdirectories for a specified directory path (referred to as a recursive directory listing).
# Issue the following Linux command: <code>ls -lR</code>
#* What directories do you see?
# Issue the following Linux command: <code>ls -lR uli101xx</code>
#* Note the differences between both of these commands (i.e. using and not using a pathname).
# You can copy and move entire directories (and their contents) to other directories. Issue the following Linux command: <code>cp -R uli101xx xyz100xx</code>
# 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 the Part 2 checking script: <code>wget urlhere -P /home/your-seneca-id/bin/</code>
# Run the checking script for Part 2: <code>check_lab2-part2</code>

If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message.

Proceed to Part 3.

==Part 3: Removing Directories==
You will now learn how to safely remove directories and their contents.

Perform the following steps:
# Confirm that you are located in your home directory.
# The rmdir command is used to remove empty directories. Remove the empty directory called ''tutorials'': <code>rmdir uli101xx/tutorials</code>
# Confirm that you removed the tutorials directory.
# Remove the empty directory called ''uli101xx'': <code>rmdir uli101xx</code>
#* '''NOTE:''' You should get an error message, since the ''uli101xx'' directory is NOT empty.
# To remove non-empty directories, you can use the <code>rm -r</code> option, which can travel down the directory paths and their contents: <code>rm -r uli101xx</code>
# Confirm that the ''uli101xx'' directory (contained in your home directory) and its contents have been removed.
#* '''NOTE:''' To safely remove non-empty directories, you can add the <code>-i</code> option, which will prompt the user if they wish to remove contents as it your travel recursively down a directory to remove directories and their contents.
# Issue the following Linux command (entering "y" when prompted): <code>rm -ri xyz100xx</code>
#* '''NOTE:''' You should have removed all directories that you have created.

'''Let's run a checking script to confirm that you have correctly removed all of those directories:'''
# 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-part3</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 Files=
As you will be working in the Linux command line environment, it is useful to learn a least one common command-line text editors.

Although programmers and developers usually use graphical IDE's to code and compile programs (Visual Studio, Sublime, Eclipse, etc), they can create source code 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).

Developers very often use a text editor to modify configuration files. In this course, 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 in service configurations.

The two most readily-available command line text editors in Linux are '''nano''' and '''vi'''.

The nano text editor would seem like an easier-to-use text editor, but vi (although taking longer to learn) has outstanding features and allow the user to be more productive with editing text files.

==Part 1: Creating Text Files Using The vi Text Editor==
[[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, editing, and saving text files. As mentioned, the vim text editor (although taking longer to learn) has outstanding features to increase coding productivity.

('''Vim''' is a newer version of vi with expanded features. We will be using vim in this course. In most cases, vi and vim can be used interchangeably here.)

The big thing to remember with vim is that it starts in '''COMMAND''' mode. You need to 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 commands.

'''Resource:''' [https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/04650c56-ae63-4c1c-8f3c-0cb5f38ebb6b/vi-help-sheet-011.pdf vi Cheat Sheet (PDF)]

An online tutorial has been created to give you "hands-on" experience on how to use vi text editor. It 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 editor.

# Make certain that your current directory is your home directory.
# Download the interactive vi tutorial: <code>wget vi-tutorial -P /home/your-seneca-id/bin/</code>
# Run the interactive tutorial: <code>vi-tutorial</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:vi-text-editor-b.png|right|200px|thumb|Fig. 10: Contents of ''othertext.txt'']]
After you have completed the tutorial:
# Using vim, create 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 quit.

==Part 2: Manage and Manipulate Text File Content==
We conclude this tutorial by learning to '''manage''', '''view''' or '''manipulate the display''' of text files.

This is HIGHLY ADVISED in case you only want to view contents and '''NOT''' edit text file contents which can cause accidental erasure of data.

Refer to the following table of Text File Management Commands:

{| 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 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 line, 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 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 -n a.txt</code>
# Try 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 you notice happened to those newly created lines?
# Issue the following Linux command: <code>cp 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 removed.

= Lab Submission =
When complete, show your instructor the following in-class:
# A successful run of '''check_lab2-part1'''
# A successful run of '''check_lab2-part2'''
# A successful run of '''check_lab2-part3'''
# In vim, the completed version of ''othertext.txt''

Make sure to fully stop your VMs when you're done!

[[Category:OPS705]]
[[Category:OPS705 Labs]]
[[Category:Digital Classroom]]
[[Category:Fall 2022]]

Navigation menu