Changes

Jump to: navigation, search

OPS705 Lab 2 (2227)

932 bytes removed, 14:12, 20 September 2022
no edit summary
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.
# Make certain that your Since the current directory is location was 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, then it will display a simulated vi environment and will provide you with "handsmakes sense to use relative or relative-on" practice using the vi text editor.# When you have completed that section, you will be returned to the main menu-home pathnames.# If you want to get extra practice, you can select the menu item labelled "REVIEW EXERCISE".# When you want to exit On the tutorialother hand, select what if we changed the menu option location to exit the tutorial.a different directory?
== Part 4: Pathing Practice ==[[Image:vi-text-editor-bdir12.png|right|200px350px|thumb|FigThis diagram should reflect your own directory structure before beginning Part 4. 10: Contents of ''othertextRefer to this tree diagram throughout Part 4 to help orient yourself.txt'']]After you have completed When performing the tutorial:# Using vimnext series of steps, create a new text file called ''othertext.txt'' in your home directory.# Write refer to the tree diagram on the text shown in ''Figright. 10'' Learning to your new ''othertext.txt'' file, savereference a tree diagram on a quiz, midterm or final exam can help to prevent errors and quit.loss of marks!
==Since we will be running Linux commands depending on the directory structure that you have created in Part 2: Manage and Manipulate Text File Content==We conclude this tutorial by learning to '''manage'''3, let's run a checking script to verify you created it correctly:''view''' or '''manipulate # Make certain that your current directory is your home directory.# Issue the display''' <code>tree</code> command to confirm that all of text filesthose 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-part4</code>
This is HIGHLY ADVISED in case If you only want to view contents encounter errors, make corrections and '''NOT''' edit text file contents which can cause accidental erasure of datare-run the checking script until you receive a congratulations message.
Refer 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 table 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 RECOMMENDED to view the tree diagram and check for the correct number of Text File Management Commands.. symbols. Students commonly make mistakes and lose marks on these type of questions!'''# 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'' file from 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?
{| class="wikitable" style="margin-left: 0px; margin-right:auto;"! Linux Command !! Purpose !! Example|-| touch || Create empty file(s) / Updates Existing FileLet's Date/Time Stamp || <code>touch otherfile.txt</code>|run a checking script to make certain you performed the recently-| cat || Display text file's contents without editing (small files) || <code>cat otherfileissued commands correctly.txt</code>|-| more , less || Display / Navigate within large text files without editing || <code>less otherfile# Make certain that your current directory is your home directory.txt</code>|-| cp || Copy text file(s) || # Issue the <code>cp otherfile.txt uli101xx/otherfile.txttree</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 command to confirm that all of file contents when displayed. Content is sorted alphabetically by default. The -nItalic text option sorts numerically, -r performs a reverse sort || <code>sort otherfilethose recently created directories have been removed.txt</code>|-| head || Displays # Download the '''first''' 10 lines of a text file by default. An option using a number value will display that number of lines (e.g. Part 3 checking script: <code>head wget urlhere -5 filename<P /code> will display first 5 lines). || <code>head -2 otherfile.txt<home/code>|your-| 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 seneca-5 filename<id/code> will display last 5 lines). || <code>tail -2 otherfile.txt<bin/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# Run the checking script for Part 3: </code>|check_lab2-| 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.txtpart5</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 make corrections 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>less a.txt</code>#* NOTE: The <code>less</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 <code>less</code> command?# Type the letter '''q''' to exit the more command.# 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 re-7 a.txt</code>#* What is run 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 checking script until you issue this command to display only the last line contained in that file?# 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 receive 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>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 =

Navigation menu