Changes

Jump to: navigation, search

Tutorial2: Unix / Linux File Management

1,246 bytes added, 19:37, 10 May 2021
INVESTIGATION 1: CREATING & MANAGING DIRECTORIES
=INVESTIGATION 1: CREATING & MANAGING DIRECTORIES=
<span style="color:red;">'''ATTENTION''': Depending on your ULI101 instruction, you may be required to complete this tutorial for marks in the course.<br>Please refer to your instructor's course notes in terms of marked assigned to the course.<br><br>The due date for successfully completing this tutorial (i.e. '''tutorial 2''') is by '''Friday by midnight''' next week (i.e. '''Week 3''').<br>If your instructor has NOT assigned marks for completing this tutorial, you can perform it for practice purposes.</span><br><br> 
In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove''' directories in your Matrix account.
# We will now create the subdirectories that are contained in the '''uli101xx''' directory.<br>Issue the following Linux command to move to the '''uli101xx''' directory using a '''relative''' pathname:<br><span style="color:blue;font-weight:bold;font-family:courier;">cd uli101xx</span><br><br>
# Issue a Linux command to confirm that your current location is in the ''uli101xx'' directory.<br>You should know how to issue this command from a previous tutorial.<br><br>
# Issue the following Linux command to create the directories called '''notes''', '''tutorials''' and '''examplessamples''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir notes tutorials examplessamples</span><br><br># Issue a Linux command to confirm that those directories have been created<br>(you should already know how to do this).<br><br>There are better ways to create the same directory structure without using the '''cd''' command.<br><br>Issuing the '''cd''' command '''<u>prior</u>''' to issuing other commands indicates to others that you are a novice or "'''newbie'''".<br>You can actually '''lose marks''' on quizzes or examples tests if you issue multiple Linux commands to performed tasks that can be performed<br>using a single Linux command with '''pathnames'''.<br><br>
# Issue the following Linux command to return to your home directory: <span style="color:blue;font-weight:bold;font-family:courier;">cd</span><br><br>
# Issue a Linux command to confirm that you are now located in your home directory.<br><br>Although we will teach you how to '''remove''' directories and their contents in another sectioninvestigation,<br>let's remove the created directory structure' and '''recreate''' the same directory structure<br>using just <u>one</u> command using pathnames.<br><br>
# Issue the following Linux command to remove all of the directories that you have created:<br><span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101xx acp100xx xyz100xx</span><br><br>
# Issue a Linux command to confirm that the '''uli101xx''', '''acp100xx''' and '''xyz100xx''' that been removed.<br><br>
# Issue the following single Linux command to create the entire directory structure:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p uli101xx/notes uli101xx/tutorials uli101xx/examples samples acp100xx xyz100xx</span><br><br>'''NOTE:''' The '''-p''' option allows "parent" directories to be automatically created first to then create their subdirectories.<br><br># Issue the following Linux command to confirm that all of the directories have been created:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld uli101xx/ uli101xx/notes uli101xx/tutorials uli101xx/examples samples acp100xx xyz100xx</span>
<br><br>
'''Running a Shell Script to Check your Work'''
# Make certain that your current directory is '''your home directory'''.<br><br>
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scriptsmyscripts/week2-check-1</span><br><br># If you encounter errors, then view the feedback to make corrections, and then re-run the checking script. If you receive a congratulation message that there are no errors, then proceed to the next sectionpart.
<br>
'''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></table>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br>'''NOTE:''' You should see the directory structure that you created in the previous sectioninvestigation. You can also issue the '''tree''' command using a directory pathname to display the directory structure for a specific scope.<br><br>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''').<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR </span><br><br>What directories do you see?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR uli101xxx</span><br><br>Note the differences between both of these commands (i.e. using and <u>not</u> using a pathname).<br><br>You can copy and move entire directories (and their contents) to other directories.<br><br><table align="right"><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 to display the directory structure of your home directory to confirm you moved the acp100xx directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree </span><br><br>Let's run a checking script to see if you moved and copied the directories correctly.<br><br>
# Make certain that your current directory is '''your home directory'''.<br><br>
# Issue the following Linux command run checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scriptsmyscripts/week2-check-2</span><br><br># If you encounter errors . make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next sectioninvestigation.<br><br>
===Part 3: Removing Directories ===
In this sectioninvestigation, we will learn how to '''safely remove''' directories and their contents.
# Issue the following Linux command (entering "y" when prompted): <span style="color:blue;font-weight:bold;font-family:courier;">rm -ri xyz100xx</span><br><br>'''NOTE:''' You should have removed all directories that you have created.<br><br>Let's run a checking script to confirm that you have correctly removed all of those directories.<br><br>
# Issue the '''tree''' command to confirm that all of those recently created directories have been removed.<br><br>
# Issue the following Linux command to run a checking script to confirm removal of those directories:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scriptsmyscripts/week2-check-3</span><br><br># If you encounter errors , make corrections and re-run the checking script until you receive a congratulations message, and proceed to the next INVESTIGATION.<br><br>
=INVESTIGATION 2: MANAGING TEXT FILES=
# Issue a Linux command to check that you correctly created those directories.<br><br>
# Issue the following Linux command to edit a text file called '''mytext.txt''' in the '''linux/practice''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">nano textedit/practice/mytext.txt</span><br><br>'''NOTE:''' When using the '''Nano''' text editor, you are placed in '''INPUT''' mode, so you can enter text immediately.<br><br>[[Image:nano-text-editor-2.png|right|400px|thumb|Enter the following text that appears in this diagram.]]
# Enter the lines shown in the other diagram of the nano text editor on the right side.<br><br>'''NOTE:''' Refer to the table below for a list of<br>common '''Nano''' navigation &amp; editing commands:<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;">Key(s)</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><trvalign="top"><td style="font-family:courier">'''&lt;ctrl&gt;&lt;SPACEBAR&gt;''',<br>'''&lt;esc&gt;&lt;6&gt;'''</td><td>Move forward /backward one word</td></tr><trvalign="top"><td style="font-family:courier">'''&lt;escctrl&gt;&lt;a&gt;''',<br> '''&lt;ctrl&gt;&lt;SPACEBARe&gt;'''</td><td>Move back one wordto beginning/end of line</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;k&gt;'''</td><td>Cut line</td></tr><tr><td style="font-family:courier">'''&lt;esc&gt;^6'''</td><td>Copy Line ('''Note</td></tr><tr><td style="font-family:courier">''' to get '''^''' character, type '''&lt;shiftctrl&gt;&lt;u&gt;6''', <u>not</utd> <ctrltd>)Paste Cut/Copied Text</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;ug&gt;'''</td><td>Paste Cut/Copied TextDisplay help screen (ctrl-x to exit help screen)</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;gx&gt;'''</td><td>Display help screen (ctrl-x to Save editing changes and exit help screen)</td></tr></table><br>
# Referring to the table above, practice navigating and editing your entered lines for practice.<br><br>
# To '''save''' your editing session, press: <span style="color:blue;font-weight:bold;font-family:courier;">&lt;ctrl&gt;x</span><br><br>
# You will be prompted to modify your file: type the letter <span style="font-family:courier;color:blue;font-weight:bold">y</span> for '''yes'''.<br><br>
# The name of the file will be displayed, Press <span style="color:blue;font-weight:bold;font-family:courier;">ENTER</span>.to save editing changes for that file name.<br><br>'''NOTE:''' This prompt for file name allows you to change the name of the file if you wish.<br>By pressing '''ENTER''', it will accept the default filename.<br><br>
# Practice Perform some more editing operations based on the file ( using the nano command in '''step #4''' ) and perform more editing operations,chart above.<br><br>(just type more random lines at the bottom of this file) and then # '''saveSave''' your editing session and '''exit''' your text editor.<br><br>
===Part 2: Creating Text Files Using The vi Text Editor===
# Issue the following to run the vi online tutorial:<br><span style="color:blue;font-weight:bold;font-family:courier;">/home/murray.saul/vi-tutorial<br><br>
# In the tutorial menu, select the first menu item labelled "'''USING THE VI TEXT EDITOR'''"<br><br>
#Read and follow the instructions in the tutorial. Eventually, it will display a '''simulated''' vi environment<br>and will provide you with "'''hands-on'''" practice using the vi text editor. As far as this author is aware, <br>there is NO "hands-on" tutorial for the nano text editor in this particular format.<br><br>
# When you have completed that section, you will be returned to the main menu.<br>If you want to get extra practice, you can select the menu item labelled "'''REVIEW EXERCISE'''".<br><br>
# When you want to exit the tutorial, select the menu option to exit the tutorial.<br><br>
: It is recommended to try both text editors, and choose the text editor that you feel that is easier to use.<br><br>
===Part 3: Manage / &amp; Manipulate Text File Content===
We conclude this tutorial by learning to '''manage''', '''view''' or '''manipulate the <u>display</u>''' file content without having to use a of text editorfiles.<br>This is HIGHLY ADVISED in case you only want to view contents and '''NOT ''' edit text file contents by mistakewhich can cause accidental erasure of data.
'''Perform the Following Steps:'''
# Make certain that you are located in your home directory.<br><br># Refer to the following table of '''Text File Management commands Commands:'''<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;" width="20%">Linux Command</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td style="font-family:courier">'''touch'''</td><td>Create empty file(s) / Updates Existing File's Date/Time Stamp</td></tr><tr><td style="font-family:courier">'''cat'''</td><td>Display text file's contents without editing (small files)</td></tr><tr><td style="font-family:courier">'''more , less'''</td><td>Display / Navigate within large text files without editing</td></tr><tr><td style="font-family:courier">'''cp'''</td><td>Copy text file(s)</td></tr><tr><td style="font-family:courier">'''mv'''</td><td>Move / Rename text files</td></tr><tr><td style="font-family:courier">'''rm'''</td><td>Remove text file(s)</td></tr><tr valign="top"><td style="font-family:courier">'''sort'''</td><td>Sorts (rearranges) order of file contents when displayed. Content is sorted alphabetically by default. The '''-n'''''Italic text'' option sorts numerically, '''-r''' performs a reverse sort</td></tr><tr valign="top"><td style="font-family:courier">'''head''' , '''tail'''</td><td>Displays the first / last 10 lines of a text file by default. An option using a value will display the number of lines (e.g. <span style="font-family:courier">head -5 filename</span> will display first 5 lines, <span style="font-family:courier">tail -5 filename</span> will display last 5 lines).</td></tr><tr valign="top"><td style="font-family:courier">'''grep'''</td><td>Displays file contents that match a pattern</td></tr><tr valign="top"><td style="font-family:courier">'''uniq'''</td><td>Displays identical consecutive lines only once</td></tr><tr valign="top"><td style="font-family:courier">'''diff''' file1 file2</td><td>Displays differences between 2 files</td></tr><tr><td style="font-family:courier">'''file'''</td><td>Gives info about the contents of the file (e.g. file with no extention)</td></tr><tr><td style="font-family:courier">'''find'''</td><td>To find files matching specified characteristics: <!-- <table><tr><td width="30%">'''<span style="font-family:courier">find . -name "file*"</span>'''</td><td>List pathname of any filenames beginning with "file",<br>from the current directory and any subdirectories</td></tr><tr valign="top"><td>'''<span style="font-family:courier">find . -size +50k</span>'''</td><td>List pathname of any files larger than 50 kb, from the current directory and any subdirectories</td></tr><tr valign="top"><td>'''<span style="font-family:courier">find . -mmin -5</span>''' </td><td>List files modified less than 5 minutes ago</td></tr><tr> valign="top"><td>'''<span style="font-family:courier">find -P .</span>'''</td><td>Lists file pathnames in the current directory</td></tr></table> --> </td></tr></table><br># Confirm that you are located Issue the following Linux command to create three <u>empty</u> text files in your '''home''' current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">touch a.txt b.txt c.txt</span><br><br># Use Issue the '''touch''' following Linux command to create the empty files called ''': <span style="color:blue;font-weight:bold;font-family:courier;">ls -l a.txt''', '''b.txt''', and '''c.txt'''</span><br><br>Check the size in the detailed listing to confirm that these newly-created files are empty.<br><br>[[Image:vi-screen-40.png|right|300px|thumb|Nano text editor containing numbers '''1 to 40'''<br>on separate lines.]]# Use the '''nano''' text editor to edit the empty file called '''a.txt'''. <br><br>Type the number "'''1'''" and press '''ENTER'''. On the second line, type the number "'''2'''" and press '''ENTER'''.<br>Continue entering increasing number values until you reach the number '''40''' on line 40<br>(refer to the diagram on the right).<br><br># '''Save ''' and '''exit''' your editing session.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat a.txt</span><br><br>Can you see all of the contents?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">more a.txt</span><br><br>Can '''NOTE:''' The '''more''' command uses the same navigation keys as with the '''man''' command<br>(refer to week 1 notes for reference). Try using keys that you view or at least used to navigate to see all of the contents?man pages.<br><br>What is the advantage of using the more command?<br><br>
# Type the letter "<span style="color:blue;font-weight:bold;font-family:courier;">q</span>" to exit the ''more'' command.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">less a.txt</span><br><br>Is there any difference between the '''more ''' and '''less ''' commands?<br>(again press '''q''' to quit)<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort a.txt</span><br><br>Why does the output not look what you expected? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort -n a.txt</span><br><br>Try the same command using both the '''-n''' and '''-r''' options to see what happens.<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head a.txt</span><br><br>What is the output from this command display?<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head -7 a.txt</span><br><br>What is the output from this command display?<br><br>
# issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tail a.txt</span><br><br>What is the output from this command display?<br>How would you issue this command to display only the '''last line ''' contained in that file?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">grep 2 a.txt</span><br><br>What type of output appear? Why did these lines appear (what do they all have in common)?<br><br>[[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'''".]]# Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting <br>of the <u>same</u> text: "'''end of line'''" (refer to diagram on right).<br><br># '''Save''' your editing session and save changes to '''exit''' your filetext editor.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">uniq a.txt</span><br><br>What do you notice happened to those newly created lines?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp a.txt a.txt.bk</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp a.txt b.txt</span><br><br>
# Issue one of the commands you learned to display the contents of the file called '''b.txt''' without editing.<br><br>What happened to this file?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mv a.txt aa.txt</span><br><br>
# Issue a Linux command to view the directory contents.<br><br>What happened to the file called '''a.txt'''? Why?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">file b.txt</span><br><br>What sort of information did it provide?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">diff aa.txt b.txt</span><br><br>What was the Was there any output? Why do you think caused the result of this outputIf not, why?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">diff aa.txt c.txt</span><br><br> What reason would do you think is the purpose of this type of output occur?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">find -P . </span><br><br>What is the output of this command?<br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">rm aa.txt b.txt a.txt.bk c.txt</span><br><br>
# Issue the '''ls''' command to verify that these files have been removed.<br><br>
# After you complete the Review Questions sections '''LINUX PRACTICE QUESTIONS''' to get additional practice, then work on your<br>'''online assignment #1''', '''section 2''' "''Basic Unix Commands''" (parts '''4''' to '''6''') labelled:<br> '"''Managing Files''' " , '"''Accessing Files''' " and '"''Review Exercise'''".<br><br>
= LINUX PRACTICE QUESTIONS =
13,420
edits

Navigation menu