Changes

Jump to: navigation, search

Tutorial2: Unix / Linux File Management

58 bytes added, 20:34, 4 September 2023
no edit summary
{{Admon/caution|DO NOT USE THIS VERSION OF THE LAB. This page will no longer be updated.|'''New version here:''' https://seneca-ictoer.github.io/ULI101/A-Tutorials/tutorial2<br />'''Andrew's students please go here:''' http://wiki.littlesvr.ca/wiki/OPS145_Lab_2}}
=UNIX / LINUX FILE MANAGEMENT CONCEPTS=
<br>
===Main Objectives of this Practice Tutorial===
 
:* Understand the purpose of '''directories''' and '''directory pathnames'''.
 
:* Use common '''Unix / Linux commands''' to perform '''directory management''' tasks.
 
:* Use '''Text Editors''' to create and modify text files
 
:* Use Common Unix / Linux commands to '''manage''' and '''manipulate''' text files.
<br><br>
===Tutorial Reference Material===
|- valign="top" style="padding-left:15px;"
|colspan="1" |'''Slides:'''<ul><li>Week 2 Lecture 1 Notes:<br> [https://githubwiki.comcdot.senecacollege.ca/ULI101uli101/slides/raw/main/ULI101-2.1.pdf PDF] | [https://githubwiki.cdot.comsenecacollege.ca/ULI101uli101/slides/raw/main/ULI101-2.1.pptx PPTX]</li><li>Week 2 Lecture 2 Notes:<br> [https://githubwiki.cdot.comsenecacollege.ca/ULI101uli101/slides/raw/main/ULI101-2.2.pdf PDF] | [https://githubwiki.cdot.comsenecacollege.ca/ULI101uli101/slides/raw/main/ULI101-2.2.pptx PPTX]</li></ul>'''Tutorials:'''<ul><li>[http://www.cheat-sheets.org/saved-copy/Nano_Cheat_Sheet.pdf Nano Reference Sheet (PDF)]</li><li>[https://www.smashingmagazine.com/2010/05/vi-editor-linux-terminal-cheat-sheet-pdf Vi Reference Sheet (PDF)]</li></ul>
To better '''organize''' files (eg. text, images, documents, spreadsheets, programs) within your Matrix account, they should be stored in '''directories'''. To further organize <u>many</u> files, directories<br> may contain '''sub-directories'''.
The Unix/Linux file system is '''hierarchical''', like other operating systems such as '''Windows''', '''Mac OSXmacOS''', etc.In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as '''C:''', or '''D:''').<br> 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 (Unix) OS, the "'''root directory'''" / is the starting directory, and other "''child directories''", "'''grandchild directories'''", etc. can be created as required. The hierarchical structure resembles an "''upside-down tree''".  There is actually a command called '''<code>tree''' </code> that displays a "'''directory tree diagram'''"!<br><br>
=== Directory Pathnames ===
[[Image:path-name.png|thumb|right|180px|A pathname points to a file system location by '''following the directory tree hierarchy'''.]]
<i>''A '''pathname''' is used to specify a '''route to a file''' within the file system.<br><br>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 ("'''/'''").</i> Reference: https://en.wikipedia.org/wiki/Path_(computing)
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 following table displays and defines '''delimiting character''' is most commonly used directoriesthe slash ("'''/'''<br>").'' Reference: https://en.wikipedia.org/wiki/Path_(listed by directory pathnamecomputing) for for ALL Unix / Linux Filesystems:<br>
<br><br>The following table displays and defines '''commonly used directories''' (listed by directory pathname) for for ALL Unix / Linux Filesystems:
[[Image:directory-structure-2.png|thumb|right|500px|'''Common directory pathnames'''<br>for for ALL Unix / Linux Filesystems.]]<table cellpadding="5"><tr><th style="border-bottom: 1px solid black;">Directory Pathname</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td>'''/'''</td><td>Root directory (ancestor to all directories)</td></tr><tr><td>'''/home'''</td><td>Used to store users’ home directories
</td></tr><tr><td>'''/home/username'''</td><td>A '''<u>specific</u>''' User's Home Directory</td></tr><tr><td>'''/bin'''</td><td>Common system binaries (commands) </td></tr><tr><td>'''/usr/bin'''</td><td>Common utilities (commands) for users. </td></tr><tr><td>'''/usr/sbin'''</td><td>Common utilities for system administration </td></tr><tr><td>'''/etc'''</td><td>System administration files (eg. passwd)</td></tr><tr><td>'''/var'''</td><td>Dynamic files (log and mail files)</td></tr><tr><td>'''/tmp , /var/tmp'''</td><td>Temporary files for programs</td></tr><tr><td>'''/dev'''</td><td>Device driver files (terminals, printers, etc.)
</td></tr></table><br>
 When you log into your Matrix account, you are automatically directed to your '''home''' directory.<br>This directory is where the user can store files, and create subdirectories to organize their files.
=INVESTIGATION 1: CREATING &amp; MANAGING DIRECTORIES=
 <span style="color:red;">'''ATTENTION''': {{Admon/caution|This online weekly tutorial will be required to must be completed by '''Friday in week Week 3 by midnight''' 11:59pm to obtain a grade of '''2%''' towards this course</span><br><br>.|}}
In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove''' directories in your Matrix account.
 
'''Directory File Naming Rules'''
* '''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: Creating Directories ===
[[Image:directory-structure-3.png|thumb|right|350px|'''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<br>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<br>'''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<br>(refer to diagram on the right side).
'''Perform the Following Steps:'''
'''Perform NOTE:''' For several of the following commands, you will be using '''<u>your</u> Seneca username''' to replace the Following Stepstext:"'''your-seneca-id'''" in a pathname.
'''NOTE:''' For several of the following commands, you will be using '''<u>your</u> Seneca username''' to replace the text:"'''your-seneca-id'''" in a pathname.<br><br>For example, if your Seneca user ID was '''mkhan''' then you would display<br>the pathname ''/home/your-seneca-id'' as: '''/home/mkhan'''<br><br>
# '''Login''' to your matrix account (you should know how to do this from performing [https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_1:_ACCESSING_YOUR_MATRIX_LINUX_ACCOUNT Tutorial 1 INVESTIGATION 1]]).<br><br># Issue a command to '''confirm''' that you are located in your home directory<br>(you should know how to do this from performing [[https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_2:_USING_THE_LINUX_SHELL Tutorial 1 INVESTIGATION 2]]<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mkdir /home/your-seneca-id/uli101xx</span><br>(remember: use <u>your</u> Seneca username instead of "your-seneca-id")<br><br>'''NOTE:''' You should always confirm that you have created a directory.<br>This can be done by issuing the '''ls''' command.<br><br>[[Image:directory-structure-4.png|thumb|right|350px|'''Creating''' and '''Confirming''' the Creation of a Directory.]]
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls /home/your-seneca-id/uli101xx</span><br><br>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 <u>directory</u> has been created as opposed to viewing the contents of the directory.<br><br>
=INVESTIGATION 2: MANAGING TEXT FILES=
 
<table align="right"><tr valign="top"><td>[[Image:nano-text-editor-1.png|thumb|right|250px|The '''Nano Text Editor''' is Easy to Operate for Novice Users.]]</td><td>[[Image:vi-text-editor-1.png|thumb|right|300px|The '''vi Text Editor''', although taking longer to master, has outstanding features and allow the user to be more productive with editing text files.]]</td></tr></table>
<br>
Since IT students will be working in the Unix / Linux command line environment, it is useful to learn a least two common command-line '''text editors'''.
Although '''programming students''' can use '''graphical IDE's''' to code and compile programs, they can '''create source code''' using a text editor and '''compile their source code''' in the '''Matrix''' server to generate executable programs (without having to transfer them for compilation or execution).
'''Networking and Tech Support students''' can use a text editor to '''edit configuration files'''. These students in upper grades will become familiar with the process of installing, configuring, and running network services and text editors are an important tools<br>to help setup but also "tweak" or make '''periodic changes in networking services configuration'''.
The two most readily-available command line text editors in Linux are '''Nano''' and '''vi'''.<br>The Nano text editor would seem like an easier-to-use text editor, but vi (although taking longer to learn)<br>has outstanding features and allow the user to be more productive with editing text files.<br><br>
===Part 1: Creating Text Files Using The Nano Text Editor===
You will now learn basic editing skills using the '''Nano''' text editor including '''creating''', '''editing''', and '''saving''' text files.
 
You will now learn basic editing skills using the '''Nano''' text editor including '''creating''', '''editing''', and '''saving''' text files.
<br><br>
'''Perform the Following Steps:'''
# 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><tr valign="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><tr valign="top"><td style="font-family:courier">'''&lt;ctrl&gt;&lt;a&gt;''',<br> '''&lt;ctrl&gt;&lt;e&gt;'''</td><td>Move to 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</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;u&gt;'''</td><td>Paste Cut/Copied Text</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;g&gt;'''</td><td>Display help screen (ctrl-x to exit help screen)</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;x&gt;'''</td><td>Save editing changes and exit</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>
===Part 2: Creating Text Files Using The vi Text Editor===
[[Image:vi-screen.png|right|300px|thumb|Using the '''vi''' text editor.]]
You will now learn basic editing skills using the '''vi''' ('''vim''') text editor including<br>'''creating''', '''editing''', and '''saving''' text files.
The '''vi''' ('''vim''') text editor (although taking longer to learn) has outstanding features to increase coding productivity.
 
The major different between nano and vi is that '''vi starts in COMMAND LINE mode'''. You need to issue letter commands to enter text. Also you can press colon “: ” in COMMAND mode to enter more complex commands.
===Part 3: Manage &amp; Manipulate Text File Content===
We conclude this tutorial by learning to '''manage''', '''view''' or '''manipulate the <u>display</u>''' of text files.<br>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.
'''Perform the Following Steps:'''
# 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>
# Complete the '''LINUX PRACTICE QUESTIONS''' to get additional practice.<br><br>
= LINUX PRACTICE QUESTIONS =
simulate a quiz:
* https://githubwiki.com/ULI101/labscdot.senecacollege.ca/rawuli101/mainfiles/uli101_week2_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
 
'''Review Questions:'''
For each of the following questions, use a pathname starting from the '''root''' directory (i.e. “/”).<br><br>
[[Image:tree-diagram.png|right|350px|thumb|]]
# Write a single Linux command to create the directory structure starting from your home directory from the diagram displayed on the right.
# Write a Linux command to display a detailed listing of '''history''' directory.<br>How would this command differ if you wanted to also view hidden files as well?<br># Write a Linux command to change to the '''project''' directory.<br>What command would you issue to return to your home directory?<br>
# Write a Linux command to copy the '''project''' directory and its contents to the '''history''' directory.
# Write a Linux command to move the directory called '''directories''' to the '''history''' directory.
# Write a Linux command to remove both directories named '''1''' and '''2'''.
# Write a Linux command to remove the '''concepts''' directory and its contents.
# Write a Linux command to remove the '''concepts''' directory and prompt the user if they want to remove this directory’s contents.<br><br># Write a single Linux command to create the following empty files in the concepts directory:<br>#* '''myfile.txt'''<br>#* '''yourfile.txt'''<br>#* '''thefile.txt'''<br># Write a Linux command to view the contents of the myfile.txt text file to prove it is empty.<br>What is the difference between the commands: '''cat''', '''more''' and '''less'''?
# Write a Linux command to sort the contents of a file called '''practice/customers.txt'''
# Write a Linux command to display the first 4 lines of a file called '''practice/customers.txt'''
# Write a Linux command to display unique occurrences of consecutive lines in a file called '''practice/customers.txt'''
# Create a '''table''' listing each Linux command, useful options that are displayed near the top of this tutorial labelled: '''Tutorial Reference Material'''
 
_________________________________________________________________________________
_________________________________________________________________________________
 
 
[[Category:ULI101]]

Navigation menu