Difference between revisions of "Tutorial2: Unix / Linux File Management"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 1: MANAGING DIRECTORy FILES)
 
(802 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=UNIX / LINUX FILE MANAGEMEN=
+
{{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}}
<br>
+
=UNIX / LINUX FILE MANAGEMENT CONCEPTS=
 
===Main Objectives of this Practice Tutorial===
 
===Main Objectives of this Practice Tutorial===
 
+
:* Understand the purpose of '''directories''' and '''directory pathnames'''.
:* x
+
:* Use common '''Unix / Linux commands''' to perform '''directory management''' tasks.
 
+
:* Use '''Text Editors''' to create and modify text files
:* x
+
:* Use Common Unix / Linux commands to '''manage''' and '''manipulate''' text files.
 
 
:* x
 
 
 
:* x
 
 
 
:* x
 
 
 
:* x
 
 
 
:* x<br><br>
 
  
 
===Tutorial Reference Material===
 
===Tutorial Reference Material===
Line 23: Line 13:
 
|- valign="top"
 
|- valign="top"
  
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;"|Course Notes / Software<br>
+
|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;"|Course Notes<br>
  
 
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
 
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
Line 31: Line 21:
 
|- valign="top" style="padding-left:15px;"
 
|- valign="top" style="padding-left:15px;"
  
|colspan="2" |Course Notes:<ul><li>[https://ict.senecacollege.ca/~uli101/notes/Week02.pdf PDF] | [https://ict.senecacollege.ca/~uli101/notes/Week02.pptx PPTX]</li></ul>Tutorials:<ul><li>[https://www.smashingmagazine.com/2010/05/vi-editor-linux-terminal-cheat-sheet-pdf Vi Reference Sheet]</li><li>[http://czegel.com/seneca/uli101/lectures/Lecture2.html Les Czegel's Week 2 Notes (HTML)]</li></ul>
+
|colspan="1" |'''Slides:'''<ul><li>Week 2 Lecture 1 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.1.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.1.pptx PPTX]</li><li>Week 2 Lecture 2 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-2.2.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/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>
  
  
|  style="padding-left:15px;" |Directory and File Management Commands
+
|  style="padding-left:15px;" |'''File Management:'''
 
*[http://man7.org/linux/man-pages/man1/pwd.1.html pwd]<br>
 
*[http://man7.org/linux/man-pages/man1/pwd.1.html pwd]<br>
 
*[http://man7.org/linux/man-pages/man1/cd.1p.html cd]<br>
 
*[http://man7.org/linux/man-pages/man1/cd.1p.html cd]<br>
 
*[http://man7.org/linux/man-pages/man1/ls.1p.html ls]<br>
 
*[http://man7.org/linux/man-pages/man1/ls.1p.html ls]<br>
 +
*[https://www.systutorials.com/docs/linux/man/1-tree/ tree]<br>
 
*[http://man7.org/linux/man-pages/man1/mkdir.1.html mkdir]<br>
 
*[http://man7.org/linux/man-pages/man1/mkdir.1.html mkdir]<br>
 
*[http://man7.org/linux/man-pages/man1/rmdir.1.html rmdir]<br>
 
*[http://man7.org/linux/man-pages/man1/rmdir.1.html rmdir]<br>
Line 44: Line 35:
 
*[http://man7.org/linux/man-pages/man1/rm.1.html rm] , [http://man7.org/linux/man-pages/man1/rm.1.html rm -r]<br>
 
*[http://man7.org/linux/man-pages/man1/rm.1.html rm] , [http://man7.org/linux/man-pages/man1/rm.1.html rm -r]<br>
  
|  style="padding-left:15px;"|Text File Management Commands
+
|  style="padding-left:15px;"|'''Text Editors /<br>File Content:'''
 +
*[http://man7.org/linux/man-pages/man1/vi.1p.html vi] , *[https://linux.die.net/man/1/nano nano]<br>
 
*[http://man7.org/linux/man-pages/man1/cat.1.html cat]<br>
 
*[http://man7.org/linux/man-pages/man1/cat.1.html cat]<br>
 
*[http://man7.org/linux/man-pages/man1/more.1.html more] , [http://man7.org/linux/man-pages/man1/less.1.html less]<br>
 
*[http://man7.org/linux/man-pages/man1/more.1.html more] , [http://man7.org/linux/man-pages/man1/less.1.html less]<br>
 +
* [http://man7.org/linux/man-pages/man1/head.1.html head] , [http://man7.org/linux/man-pages/man1/tail.1.html tail]
 
*[http://man7.org/linux/man-pages/man1/touch.1.html touch]<br>
 
*[http://man7.org/linux/man-pages/man1/touch.1.html touch]<br>
 
*[http://man7.org/linux/man-pages/man1/file.1.html file]<br>
 
*[http://man7.org/linux/man-pages/man1/file.1.html file]<br>
 
*[http://man7.org/linux/man-pages/man1/find.1.html find]<br>
 
*[http://man7.org/linux/man-pages/man1/find.1.html find]<br>
 
*[http://man7.org/linux/man-pages/man1/diff.1.html diff]<br>
 
*[http://man7.org/linux/man-pages/man1/diff.1.html diff]<br>
Text Editors
+
*[http://man7.org/linux/man-pages/man1/sort.1.html sort]<br>
*[http://man7.org/linux/man-pages/man1/vi.1p.html vi]<br>
+
*[http://man7.org/linux/man-pages/man1/uniq.1.html uniq]<br>
*[https://linux.die.net/man/1/nano nano]<br>
+
*[http://linuxcommand.org/lc3_man_pages/grep1.html grep]<br>
 +
 
 +
|colspan="1" style="padding-left:15px;" width="30%"|'''Brauer Instructional Videos:'''<ul><li>[https://www.youtube.com/watch?v=UfAFAipked0&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=3 Basic File Actions<br>(touch, ls -l, cp, cp -r, mv, mkdir, mkdir -p, rm, rmdir, rm -r)]</ul>
 +
 
 +
|}
 +
 
 +
= KEY CONCEPTS=
 +
 
 +
===Unix / 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 (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''', '''macOS''', etc. In Unix / 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'').
  
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=UfAFAipked0&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=3 Basic File Actions (touch, ls -l, cp, cp -r, mv, mkdir, mkdir -p, rm, rmdir, rm -r), Relative-to-home Paths]<br><br></li><li>[https://www.youtube.com/watch?v=ocU34PcYn2U&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=4 Reading/Writing to Files (echo, stdin, stdout, stderr, >, >>, 2>, cat, more, less, man, date, diff, diff -y, find, wc]</li></ul>
+
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'''"!
 +
 
 +
=== Directory Pathnames ===
 +
[[Image:path-name.png|thumb|right|180px|A pathname points to a file system location by '''following the directory tree hierarchy'''.]]
 +
''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)
 +
 
 +
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>
 +
 
 +
When you log into your Matrix 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.
  
===Unix / Linux File Management Concepts===
+
=INVESTIGATION 1: CREATING &amp; MANAGING DIRECTORIES=
 +
{{Admon/caution|This weekly tutorial must be completed by Friday in Week 3 by 11:59pm to obtain a grade of 2% towards this course.|}}
  
x
+
In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove''' directories in your Matrix account.
  
=INVESTIGATION 1: MANAGING DIRECTORy FILES=
+
'''Directory File Naming Rules'''
  
<br>
+
Before you learn how to create directories, it is important to understand what represents an appropriate '''directory filename'''.
x
 
  
===Part 1: Creating / Removing Directories ===
+
Listed below are some common '''file-naming rules''':
x
 
  
 +
* Unix/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)
  
'''Perform the Following Steps:'''
+
===Part 1: Creating Directories ===
  
# x
+
[[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 save and access files on your Linux server.
  
===Part 2: Creating / Copying / Moving / Removing Files ===
+
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.
  
x<br><br>
+
You will be creating the following '''directory structure''' within your home directory (refer to diagram on the right side).
  
 
'''Perform the Following Steps:'''
 
'''Perform the Following Steps:'''
  
# x<br><br>
+
'''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.
  
 +
For example, if your Seneca user ID was '''mkhan''' then you would display the pathname ''/home/your-seneca-id'' as: '''/home/mkhan'''
  
 +
# '''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>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -d /home/your-seneca-id/uli101xx</span><br><br>You should now see just the directory listed. You can also combine the '''-d''' and '''-l''' options to provide more detail regarding the newly-created directory.<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -d -l /home/your-seneca-id/uli101xx</span><br><br>How can you confirm from the output of this command that the file uli101xx is a directory?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -ld /home/your-seneca-id/uli101xx</span><br><br>Is the output from this command the same was the output from the previous command?<br>If so, what does this say about how to use multiple options for Linux commands?<br><br>
 +
# Issue the following Linux command to create the '''acp100xx''' and '''xyz100xx''' directories:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</span><br><br>'''NOTE:''' You can create multiple directories by issuing the '''mkdir''' command with <u>multiple arguments</u>.<br><br>
 +
# Issue the following Linux command to confirm that those directories have been created:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx</span><br><br>'''Using a FULL pathname starting from the root directory (i.e. /) requires is a LOT of typing!.<br>Since we are already located in our home directory, we don't have to start from<br>the root directory. This is called a "relative" pathname.'''<br><br>
 +
# 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 '''samples''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir notes tutorials samples</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 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 investigation,<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''' directories have 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/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/samples acp100xx xyz100xx</span>
 
<br><br>
 
<br><br>
 +
'''Running a Shell Script to Check your Work'''
 +
 +
Although you are being asked to create the directory structure, you might have made some mistakes:<br>For Example:<br>
 +
 +
* Forgetting to create a directory.
 +
* Making syntax errors (eg. spelling or mixing up character case)
 +
* Not creating subdirectories within specified directories.
 +
  
=INVESTIGATION 2: USING THE LINUX SHELL / ONLINE ASSIGNMENTS =
+
<table align="right"><tr valign="top"><td>[[Image:script-check-1.png|thumb|right|250px|If all 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>
 +
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:'''
 +
 +
# 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;">~uli101/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 part.
 
<br>
 
<br>
In this section, you will learn how to use the Linux Shell. Topics in the section include:
 
  
:* Learn the '''Linux command structure''' (arguments and options)
+
===Part 2: Viewing Directory Contents / Copying & Moving Directories===
:* Learn how to get '''help''' for Linux commands with manpages ('''man''' command).
 
:* Learn '''Basic Shell Editing'''
 
:* Learn how to perform '''online assignments'''.
 
:* Perform the '''first two sections of online assignment #1'''.<br><br>
 
  
===Part 1: Using the Linux Shell  ===
+
Now that you have learned how to efficiently create your directory structure, you will now learn how to issue Linux commands<br>to '''view directory contents''' as well as '''copy''' and '''move''' directories.
  
The Bash shell in Matrix allows you to interact with your account by issuing commands.
 
  
Some Linux commands can be issued by entering the Linux command line without arguments (e.g. '''pwd''', '''date''', '''ls''', '''cal'''),<br>but some Linux commands can be issued with arguments (e.g. '''cal 2002''', '''cd /bin''', '''ls -la''' ).<br><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 investigation. 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 uli101xx</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:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101xx xyz100xx</span><br><br>
 +
#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101xx directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree </span><br><br><br><br><br><br>
 +
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv acp100xx  xyz100xx/uli101xx/tutorials</span><br><br>
 +
#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;">~uli101/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 investigation.<br><br>
  
'''Linux Command Structure'''
+
===Part 3: Removing Directories ===
  
<span style="color:blue;font-weight:bold">command argument1 argument2 ... </span>
+
In this investigation, we will learn how to '''safely remove''' directories and their contents.
  
An '''argument''' can be a '''pathname''', '''text''', '''option''', etc.
 
  
For example:
+
'''Perform the Following Steps:'''
* The '''ls''' command displays a listing of filenames in the current directory
 
* The '''ls /bin''' command displays a listing of filenames in the /bin directory (as opposed to your current directory)
 
* The '''ls -l'''  command displays a detailed listing of filenames in the current directory
 
* The '''ls -l /bin'''  command displays a details listing of files in the /bin directory
 
  
 +
# Confirm that you are located in your home directory.<br><br>The '''rmdir''' command is used to remove <u>empty</u> directories.<br><br>
 +
# Issue the following Linux command to remove the empty directory called '''tutorials''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101xx/tutorials</span><br><br>
 +
# Issue a command to confirm that you removed the '''tutorials''' directory.<br><br>
 +
# Issue the following Linux command to remove the empty directory called '''uli101xx''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101xx</span><br><br>NOTE: You should get an '''error message''', since the '''uli101xx''' directory is '''NOT''' empty.<br><br>To remove non-empty directories, you can use the '''rm -r''' command.<br>The '''-r''' option stands for recursive, which can travel down the directory paths and their contents.<br><br>
 +
# Issue the following Linux command to remove the '''uli101xx''' directory and its contents: <span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101xx</span><br><br>
 +
# Issue the '''tree''' command to confirm that the uli101xx directory (contained in your home directory) and its contents have been removed.<br><br>'''NOTE:''' To <u>safely</u> remove non-empty directories, you can add the '''-i''' 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.<br><br>
 +
# 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;">~uli101/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>
  
'''Getting Help with Linux Commands'''
+
=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>
 +
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'''.
  
With the Linux OS containing over '''2500''' commands and utilities, it is good for a Linux user or sysadmin to learn about how to use commands “on-the-fly”.
+
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).
  
The '''man''' command can provide information on how to use a command (i.e. '''usage''', '''arguments''', '''options''', '''examples''').<br>The commands are classified into sections or “volumes”.
+
'''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 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'''. 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 Nano Text Editor===
 +
 
 +
You will now learn basic editing skills using the '''Nano''' text editor including '''creating''', '''editing''', and '''saving''' text files.
  
 
'''Perform the Following Steps:'''
 
'''Perform the Following Steps:'''
  
# Make certain that you are logged into your Matrix account.<br><br>
+
# Make certain that you are located in your home directory.<br><br>[[Image:directory-structure-10.png|right|150px|thumb|]]
# Issue the following command: <span style="color:blue;font-weight:bold">man man</span><br>How many volume numbers are contained in the man pages (like '''executable commands/programs''', '''games''', or '''system administrator commands''')?<br><br>
+
# '''Create''' the following directory structure (displayed on the right side) by issuing a '''single Linux command'''<br>(You should know how to do this from the previous INVESTIGATION).<br><br>
# Press the letter <span style="color:blue;font-weight:bold">q</span> to exit the man command.<br><br>
+
# Issue a Linux command to check that you correctly created those directories.<br><br>
# Issue the Linux command to get help with the command: '''ls'''<br><br>'''NOTE:''' You can use the following short-cut keys within the man command to <br>help navigate throughout this utility to get help with the ls command (refer to table below):<br><br><table cellpadding="3"><tr><th style="border-bottom: 1px solid black;">Keyboard Shortcut</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><td>'''ENTER'''</td><td>Move down one line</td></tr><tr><td>'''SPACEBAR'''</td><td>Move one screen down</td></tr><tr><td>'''&lt;ctrl&gt;&lt;f&gt;'''</td><td>Move one screen up</td></tr><tr><td>'''/pattern/'''</td><td>Search for Pattern</td></tr><tr><td>'''q'''</td><td>quit man utility</td></tr></table><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.]]
# Navigate through the man utility for the '''ls''' Linux command and note the option letters that correspond to the following:<br><ul><li>Include directory entries whose names begin with a dot</li><li>List in long format</li><li>Recursively list subdirectories encountered</li><li>Sort files by size</li><li>Display a slash (`/') immediately after each pathname that is a directory</li></ul><br>
+
# 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 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>
# Exit the man utility for the ls command.<br><br>
+
# Referring to the table above, practice navigating and editing your entered lines for practice.<br><br>
# Issue the ls command for each of those option letters you wrote down to see how this command differs for each option.<br><br>NOTE: The '''man''' utility can be used with the '''-k''' option to help list Linux commands that match a text pattern that is contained within the help screen for a Linux command.<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>
# Although we will not be learning about pipeline commands (using the | character) until later, try issuing the following pipeline command: <span style="color:blue;font-weight:bold">man -k copy | grep 8</span><br>How this this pipeline command make it easier to obtain information regarding man command?<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>
# If you wish to change your password, you can change it by issuing the command: <span style="color:blue;font-weight:bold">passwd</span><br>What option for the passwd command can be used to change user info (such as name)?<br>'''Hint:''' use the manpages for the passwd command.<br><br>'''NOTE:''' You are NOT required to change your Matrix password for this tutorial. The Matrix server is "synched" with the my.senecacollege.ca server, so it is better to have your Matrix password changed at the same time that you change your myseneca password.<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>
 +
# Perform some more editing operations based on the editing chart above.<br><br>
 +
# '''Save''' your editing session and '''exit''' your text editor.<br><br>
  
'''Command Line Editing'''
+
===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 '''creating''', '''editing''', and '''saving''' text files.
  
Learning '''shortcut keys''' in any OS terminal will allow you to be more productive as a sysadmin. You will focus on learning a few common Bash Shell keyboard shortcut keys
+
The '''vi''' ('''vim''') text editor (although taking longer to learn) has outstanding features to increase coding productivity.
and learn where you can access online help for additional shortcuts (if required).
+
 
 +
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.
  
 
'''Perform the Following Steps:'''
 
'''Perform the Following Steps:'''
  
# Type the following Linux command, but DON’T press the ENTER key:   <span style="color:blue;font-weight:bold">cd /bin</span><br><br>
+
# Make certain that you are located in your home directory.<br><br>
# Press the <span style="color:blue;font-weight:bold">ctrl-a</span> key combination. What happens?<br><br>
+
# Issue the following Linux command to edit a text file called '''othertext.txt''' in the '''linux/practice''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">vi textedit/practice/othertext.txt</span><br><br>'''NOTE:''' When using the '''vi''' text editor, you are placed in '''COMMAND''' mode,<br>so you need to issue a command to switch to ''INPUT'' mode.<br><br>[[Image:vi-text-editor-a.png|right|200px|thumb|Enter the following text that appears in this diagram.]]
# Press the <span style="color:blue;font-weight:bold">alt-F</span> key combination (ESC+F for Mac OSx). What happens?<br><br>
+
# Type the following key to enter ''INPUT'' mode: <span style="color:blue;font-weight:bold;font-family:courier;">i</span><br><br>You should notice you are in ''INSERT'' mode by seeing the notification '''-- INSERT --'''<br>at the bottom left-hand side of the application screen.<br><br>
# Press the <span style="color:blue;font-weight:bold">ctrl-w</span> shortcut. What happens?<br><br>
+
# Enter the line shown in the other diagram of the vi text editor on the right side.<br><br>
# Type the following command: <span style="color:blue;font-weight:bold">ls</span><br><br>
+
# While on the first line, press the following key to enter ''COMMAND'' mode: <span style="color:blue;font-weight:bold;font-family:courier;">ESC</span><br><br>You should see the ''-- INSERT --'' notification disappear indicating that you are in ''COMMAND'' mode.<br><br>'''NOTE:''' Refer to the table below for a list of<br>the most common '''vi''' ('''vim''') 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><td style="font-family:courier">'''i'''</td><td>Enter '''INPUT''' mode</td></tr><tr><td style="font-family:courier">'''&lt;esc&gt;'''</td><td>Return to '''COMMAND''' MODE</td></tr><tr><td style="font-family:courier">'''x'''</td><td>Delete text to the right in COMMAND mode</td></tr><tr><td style="font-family:courier">'''W'''</td><td>Move forward one word in COMMAND mode</td></tr><tr><td style="font-family:courier">'''B'''</td><td>Move back one word in COMMAND mode</td></tr><tr><td style="font-family:courier">'''dd'''</td><td>Cut line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''yy'''</td><td>Copy Line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''p / P'''</td><td>Paste Cut/Copied Text below/above line in COMMAND mode</td></tr><tr><td style="font-family:courier">'''u'''</td><td>Undo previous editing command</td></tr><tr><td style="font-family:courier">''':help'''</td><td>Display help screen in COMMAND mode</td></tr><tr><td style="font-family:courier">''':x'''</td><td>Save editing changes and exit (in COMMAND mode)</td></tr><tr><td style="font-family:courier">''':w name'''</td><td>Save editing changes to "name" in COMMAND mode</td></tr><tr><td style="font-family:courier">''':q!'''</td><td>Abort editing session and exit (in COMMAND mode)</td></tr></table><br>Once you can get used to working in INPUT and COMMAND mode, it is easier to perform<br>text editing operations in '''vi''' via ''COMMAND'' mode as opposed to nano!<br><br>
# Press the <span style="color:blue;font-weight:bold">ctrl-e</span> key combination and press <span style="color:blue;font-weight:bold">ENTER</span><br><br>
+
# Type the following keys to <u>copy</u> the current line: <span style="color:blue;font-weight:bold;font-family:courier;">yy</span><br><br>
# Press the up arrow key. What happens?<br><br>
+
# Type the following key to <u>paste</u> the copied line: <span style="color:blue;font-weight:bold;font-family:courier;">p</span><br><br>What did you notice?<br><br>
# Press the <span style="color:blue;font-weight:bold">ctrl-u</span> key combination. What happens?<br><br>
+
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">u</span><br><br>What did you notice?<br><br>
# Press the <span style="color:blue;font-weight:bold">up arrow</span> key combination, and see what happens when you press <span style="color:blue;font-weight:bold">BACKSPACE</span> , <span style="color:blue;font-weight:bold">&lt;ctrl&gt;&lt;BACKSPACE&gt;</span> and <span style="color:blue;font-weight:bold">&lt;ctrl&gt;&lt;h&gt;</span>. Why is it important to know those series of key combinations?<br><br>
+
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">3p</span><br><br>What did you notice?<br><br>[[Image:vi-text-editor-b.png|right|200px|thumb|Use a combination of '''arrow''' keys and the '''w''' and '''b''' keys while in COMMAND mode to change the correct words for the '''third''' and '''fourth''' lines.]]
# Press the <span style="color:blue;font-weight:bold">ctrl-u</span> key combination to clear the line.<br><br>
+
# Use the up or down '''arrow''' keys to move to the second line.<br><br>
# Press the <span style="color:blue;font-weight:bold">ctrl-l</span> key combination. What happens? What is the advantage of using this key combination then issuing the '''clear''' command?<br><br>
+
# Type either the '''w''' and/or '''b''' keys to move the cursor to the beginning of the word: '''first'''.<br><br>
 +
# Type the '''x''' keys to remove the word called '''first'''.<br><br>
 +
# Type the '''i''' key to enter INSERT mode.<br><br>
 +
# Type the word: '''second'''<br><br>
 +
# Press the '''ESC''' key to enter COMMAND mode.<br><br>
 +
# Repeat steps '''11''' to '''16''' to change the words for line number for lines '''3''' and '''4'''.<br><br>
 +
# Move to the 4th line (i.e. last line).<br><br>
 +
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">dd</span><br><br>What did you notice?<br><br>Let's save editing changes and exit the vi text editor.<br><br>
 +
# Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">:x</span> and press '''ENTER'''<br><br>What did you notice?<br><br>
 +
# Issue the same command that you performed in '''Step #2''' to confirm that you had properly edited that file.<br><br>
 +
# '''Save''' and '''exit''' your vi editing session.<br><br>[[Image:vi-text-editor-2.png|right|250px|thumb|The online vi-tutorial provides users "hands-on" experience of using the vi text editor.]]An '''online tutorial''' has been created to give you "hands-on" experience on how to use vi text editor. It is <u>recommended</u> that you run this online tutorial in your Matrix account to learn how to create and edit text files with the vi text editor.<br><br>
 +
# Issue the following to run the vi online tutorial:<br><span style="color:blue;font-weight:bold;font-family:courier;">/home/jason.carman/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>
  
===Part 2: Performing Online Assignments ===
+
: It is recommended to try both text editors, and choose the text editor that you feel that is easier to use.<br><br>
  
You are required to perform '''3 online assignments''' during this course. This online assignment is used to teach and reinforce Linux commands are techniques as well as using Linux commands to perform tasks and tests students on their knowledge of issuing Linux commands.
+
===Part 3: Manage &amp; Manipulate Text File Content===
  
In this section, you will learn to '''select the correct section''' for your online assignments, how to perform the '''first two sections''' of assignment #1, how to '''verify''' that you successfully completed both sections, and how to '''exit''' your online assignment #1.
+
We conclude this tutorial by learning to '''manage''', '''view''' or '''manipulate the <u>display</u>''' 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.
  
<br>
+
'''Perform the Following Steps:'''
'''Perform the following steps:'''
+
 
[[Image:assign-initial-window.png|thumb|right|250px|Initial Assignment Window to Allow Student to Select Correct Course Section. ]]
+
# Make certain that you are located in your home directory.<br><br>
# In your Matrix account, issue the following command:<br><span style="color:blue;font-weight:bold;font-family:courier">~uli101/assign1</span><br><br>
+
# Refer to the following table of '''Text File Management 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>
# A screen similar to the one displayed on the right will appear.<br><br><span style="color:red;">'''WARNING''': You need to select the CORRECT section for the course which you belong to.<br>If you do NOT select your correct section, your assignment may not be recorded for marks!</span><br><br>
+
# Issue the following Linux command to create three <u>empty</u> text files in your current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">touch a.txt b.txt c.txt</span><br><br>
# Select the '''letter(s)''' corresponding to the <u>correct</u> '''ULI101 section''' and '''professor''' and press <span style="color:blue;font-weight:bold">ENTER</span><br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -l a.txt b.txt 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.]]
# After you have selected your course section, There will be a screen that provides several important notes before proceeding.<br>Please take a few moments to read those notes and press <span style="color:blue;font-weight:bold">ENTER</span> to proceed.<br><br>[[Image:assn-section-completion.png|thumb|right|250px|Main Assignment Window to View Which Sections Need to be Completed. ]]
+
# 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>
# The assignment main menu will then be displayed.<br><br>'''NOTES:'''<ul><li>Near the bottom of the window, you will see '''"You are currently registered to"''' followed by the '''section letter''' and '''instructor name'''. Double-check your course timetable to confirm that this the correct section letter. If you have selected the WRONG section, type  <span style="color:blue;font-weight:bold">C</span> in the menu selection area and press <span style="color:blue;font-weight:bold">ENTER</span>. You will return back to the original window to select your correct course section.<br><br></li><li>Near the top of the window displays the sections to complete in the assignment. You are NOT required to complete all sections at the same time. You can check the assignment #1 link on the ULI101 main WIKI page to note the due date for assignment #1. Sections that are NOT completed will be displayed in '''reverse text with a blue background'''. On the other hand, when you compete a section, then the section will appear as regular text<br>(i.e not in reverse text).<br><br>[[Image:assn-completed-window.png|thumb|right|250px|Main Assignment Window to Confirm Which Sections Have or Have Not Been Completed. ]]</li><li>If you want to verify that you have completed sections for the assignment, look for the text '''"Marks earned so far for ULI101 Assignment:"''' and it will show how many sections have been completed. When it shows all sections completed, then your assignment is finished and you should receive full marks provided that you have selected your correct course section.<br><br></li></ul>
+
# '''Save''' and '''exit''' your editing session.<br><br>
# At the menu assignment #1 window, enter <span style="color:blue;font-weight:bold">1</span> in the menu selection area to begin the first section called '''"Introduction to Unix Commands"'''.<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>
# When you have completed this section, you will be returned to the main window.<br>View the contents of this window to confirm that you completed this section.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">more a.txt</span><br><br>'''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 used to navigate the man pages.<br><br>What is the advantage of using the more command?<br><br>
# When you have time this week, enter <span style="color:blue;font-weight:bold">2</span> in the menu selection area to begin the first section called '''"Basic Unix Commands"'''.<br><br>
+
# Type the letter "<span style="color:blue;font-weight:bold;font-family:courier;">q</span>" to exit the ''more'' command.<br><br>
# When you have completed this section, you will be returned to the main window.<br>View the contents of this window to confirm that you completed this section.<br><br>'''NOTE:''' Although you are NOT allowed to use reference sheets for course evaluations, it is recommended to create one for your personal use, and write down Linux commands, their arguments and options, so your can successfully perform quizzes, midterm exam, and final exam.<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>
# Proceed to the next section to perform additional practice. Linux is about "doing"! The more practice questions you perform, the better mark you will achieve for this course and have a better foundation of issuing Linux commands to be more productive in your future courses.<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 '''exit''' your text 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 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>Was there any output? If 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 do you think is the purpose of this output?<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>
 +
# Complete the '''LINUX PRACTICE QUESTIONS''' to get additional practice.
  
 
= LINUX PRACTICE QUESTIONS =
 
= LINUX PRACTICE QUESTIONS =
Line 183: Line 302:
 
simulate a quiz:
 
simulate a quiz:
  
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week1_practice.docx
+
* https://wiki.cdot.senecacollege.ca/uli101/files/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).
 
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. “/”).
  
'''Review Questions:'''
+
[[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?
 +
# Write a Linux command to change to the '''project''' directory.<br>What command would you issue to return to your home directory?
 +
# 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.
 +
# Write a single Linux command to create the following empty files in the concepts directory:
 +
#* '''myfile.txt'''
 +
#* '''yourfile.txt'''
 +
#* '''thefile.txt'''
 +
# Write a Linux command to view the contents of the myfile.txt text file to prove it is empty. 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 the last line of a file called '''practice/customers.txt'''
 +
# Write a Linux command to match a line containing the pattern '''Linux''' in 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'''
 +
 
 +
_________________________________________________________________________________
 +
 
 +
Author:  Murray Saul
  
# What is a “'''Linux Cluster'''”? What is an advantage of using a Linux cluster?
+
License: LGPL version 3
# List '''three unique server names''' at Seneca college and briefly explain the purpose of that server.  
+
Link:    https://www.gnu.org/licenses/lgpl.html
# List the steps to connect and login to your Matrix Linux server account from a '''workstation''' in a computer lab at Seneca College.
 
# List the steps to connect and login to your Matrix Seneca College account on your '''laptop''' or your '''home computer'''.
 
# List 3 unique ways to '''log-out''' of your Matrix account (not including closing the SSH window or application).
 
# What is the difference between a Linux '''command''' and an argument?
 
# What is the purpose of a Linux command '''option'''?
 
# What character(s) is used to '''separate''' commands and arguments?
 
# Create a '''table''' listing each Linux command, useful options that were mentioned in the online assignment #1 and command purpose for the following Linux commands: '''pwd''' , '''cd''' , '''ls''' , '''cal''' , '''date''' , '''who''' , '''w''' , '''whoami''' , '''who am I''' , '''clear'''
 
  
[[Tutorial2: Unix / Linux File Management]]
+
_________________________________________________________________________________
  
 
[[Category:ULI101]]
 
[[Category:ULI101]]

Latest revision as of 20:34, 4 September 2023

Stop (medium size).png
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
Andrew's students please go here: http://wiki.littlesvr.ca/wiki/OPS145_Lab_2

UNIX / LINUX FILE MANAGEMENT CONCEPTS

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.

Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
YouTube Videos
Slides:Tutorials:


File Management: Text Editors /
File Content:
Brauer Instructional Videos:

KEY CONCEPTS

Unix / Linux Directories

In Unix / Linux (as opposed to MS 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".

To better organize files (eg. text, images, documents, spreadsheets, programs) within your Matrix account, they should be stored in directories. To further organize many files, directories
may contain sub-directories.

The Unix / Linux file system is hierarchical, like other operating systems such as Windows, macOS, etc. In Unix / 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 (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 tree that displays a "directory tree diagram"!

Directory Pathnames

A pathname points to a file system location by following the directory tree hierarchy.

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)

The following table displays and defines commonly used directories (listed by directory pathname) for for ALL Unix / Linux Filesystems:

Common directory pathnames
for for ALL Unix / Linux Filesystems.
Directory PathnamePurpose
/Root directory (ancestor to all directories)
/homeUsed to store users’ home directories
/home/usernameA specific User's Home Directory
/binCommon system binaries (commands)
/usr/binCommon utilities (commands) for users.
/usr/sbinCommon utilities for system administration
/etcSystem administration files (eg. passwd)
/varDynamic files (log and mail files)
/tmp , /var/tmpTemporary files for programs
/devDevice driver files (terminals, printers, etc.)

When you log into your Matrix 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 & MANAGING DIRECTORIES

Stop (medium size).png
This weekly tutorial must be completed by Friday in Week 3 by 11:59pm to obtain a grade of 2% towards this course.

In this investigation, you will learn how to create, navigate, list directory contents and remove directories in your Matrix 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:

  • Unix/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: Creating Directories

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).

Perform the Following Steps:

NOTE: For several of 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 display the pathname /home/your-seneca-id as: /home/mkhan

  1. Login to your matrix account (you should know how to do this from performing Tutorial 1 INVESTIGATION 1.

  2. Issue a command to confirm that you are located in your home directory
    (you should know how to do this from performing Tutorial 1 INVESTIGATION 2

  3. Issue the following Linux command: mkdir /home/your-seneca-id/uli101xx
    (remember: use your Seneca username instead of "your-seneca-id")

    NOTE: You should always confirm that you have created a directory.
    This can be done by issuing the ls command.

    Creating and Confirming the Creation of a Directory.
  4. Issue the following Linux command: ls /home/your-seneca-id/uli101xx

    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.

  5. Issue the following Linux command: ls -d /home/your-seneca-id/uli101xx

    You should now see just the directory listed. You can also combine the -d and -l options to provide more detail regarding the newly-created directory.

  6. Issue the following Linux command: ls -d -l /home/your-seneca-id/uli101xx

    How can you confirm from the output of this command that the file uli101xx is a directory?

  7. Issue the following Linux command: ls -ld /home/your-seneca-id/uli101xx

    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?

  8. Issue the following Linux command to create the acp100xx and xyz100xx directories:
    mkdir /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx

    NOTE: You can create multiple directories by issuing the mkdir command with multiple arguments.

  9. Issue the following Linux command to confirm that those directories have been created:
    ls -ld /home/your-seneca-id/acp100xx /home/your-seneca-id/xyz100xx

    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.


  10. We will now create the subdirectories that are contained in the uli101xx directory.
    Issue the following Linux command to move to the uli101xx directory using a relative pathname:
    cd uli101xx

  11. Issue a Linux command to confirm that your current location is in the uli101xx directory.
    You should know how to issue this command from a previous tutorial.

  12. Issue the following Linux command to create the directories called notes, tutorials and samples:
    mkdir notes tutorials samples

  13. Issue a Linux command to confirm that those directories have been created
    (you should already know how to do this).

    There are better ways to create the same directory structure without using the cd command.

    Issuing the cd 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.

  14. Issue the following Linux command to return to your home directory: cd

  15. Issue a Linux command to confirm that you are now located in your home directory.

    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.

  16. Issue the following Linux command to remove all of the directories that you have created:
    rm -r uli101xx acp100xx xyz100xx

  17. Issue a Linux command to confirm that the uli101xx, acp100xx and xyz100xx directories have been removed.

  18. Issue the following single Linux command to create the entire directory structure:
    mkdir -p uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx

    NOTE: The -p option allows "parent" directories to be automatically created first to then create their subdirectories.

  19. Issue the following Linux command to confirm that all of the directories have been created:
    ls -ld uli101xx/ uli101xx/notes uli101xx/tutorials uli101xx/samples acp100xx xyz100xx



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.


If all checks pass, then user performed task correctly and can continue.
If there is a warning, then feedback is provided to user to correct and re-run checking script.

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:

  1. Make certain that your current directory is your home directory.

  2. Issue the following Linux command to run a checking script:
    ~uli101/week2-check-1

  3. 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 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:

Output of the tree command to display directory structure.
Output of the ls -R command to display directory structure.
  1. Issue the following Linux command: tree

    NOTE: You should see the directory structure that you created in the previous investigation. You can also issue the tree command using 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).

  2. Issue the following Linux command: ls -lR

    What directories do you see?

  3. Issue the following Linux command: ls -lR uli101xx

    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.

    Output of the tree command to confirm copy of uli101xx directory (and contents) to the xyz100xx directory.
    Output of the tree command to confirm movement of acp100 directory directory (and contents) to the xyz100xx diredtory.
  4. Issue the following Linux command:
    cp -R uli101xx xyz100xx

  5. Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101xx directory:
    tree





  6. Issue the following Linux command:
    mv acp100xx xyz100xx/uli101xx/tutorials

  7. Issue the following Linux command to display the directory structure of your home directory to confirm you moved the acp100xx directory:
    tree

    Let's run a checking script to see if you moved and copied the directories correctly.

  8. Make certain that your current directory is your home directory.

  9. Issue the following Linux command run checking script:
    ~uli101/week2-check-2

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

Part 3: Removing Directories

In this investigation, we will learn how to safely remove directories and their contents.


Perform the Following Steps:

  1. Confirm that you are located in your home directory.

    The rmdir command is used to remove empty directories.

  2. Issue the following Linux command to remove the empty directory called tutorials: rmdir uli101xx/tutorials

  3. Issue a command to confirm that you removed the tutorials directory.

  4. Issue the following Linux command to remove the empty directory called uli101xx: rmdir uli101xx

    NOTE: You should get an error message, since the uli101xx directory is NOT empty.

    To remove non-empty directories, you can use the rm -r command.
    The -r option stands for recursive, which can travel down the directory paths and their contents.

  5. Issue the following Linux command to remove the uli101xx directory and its contents: rm -r uli101xx

  6. Issue the tree command to 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 -i 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.

  7. Issue the following Linux command (entering "y" when prompted): rm -ri xyz100xx

    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.

  8. Issue the tree command to confirm that all of those recently created directories have been removed.

  9. Issue the following Linux command to run a checking script to confirm removal of those directories:
    ~uli101/week2-check-3

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

INVESTIGATION 2: MANAGING TEXT FILES

The Nano Text Editor is Easy to Operate for Novice Users.
The vi Text Editor, although taking longer to master, has outstanding features and allow the user to be more productive with editing text files.

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 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. 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 Nano Text Editor

You will now learn basic editing skills using the Nano text editor including creating, editing, and saving text files.

Perform the Following Steps:

  1. Make certain that you are located in your home directory.

    Directory-structure-10.png
  2. Create the following directory structure (displayed on the right side) by issuing a single Linux command
    (You should know how to do this from the previous INVESTIGATION).

  3. Issue a Linux command to check that you correctly created those directories.

  4. Issue the following Linux command to edit a text file called mytext.txt in the linux/practice directory:
    nano textedit/practice/mytext.txt

    NOTE: When using the Nano text editor, you are placed in INPUT mode, so you can enter text immediately.

    Enter the following text that appears in this diagram.
  5. Enter the lines shown in the other diagram of the nano text editor on the right side.

    NOTE: Refer to the table below for a list of common Nano navigation & editing commands:

    Key(s)Purpose
    <ctrl><SPACEBAR>,
    <esc><6>
    Move forward/backward one word
    <ctrl><a>,
    <ctrl><e>
    Move to beginning/end of line
    <ctrl><k>Cut line
    <esc>6Copy Line
    <ctrl><u>Paste Cut/Copied Text
    <ctrl><g>Display help screen (ctrl-x to exit help screen)
    <ctrl><x>Save editing changes and exit

  6. Referring to the table above, practice navigating and editing your entered lines for practice.

  7. To save your editing session, press: <ctrl>x

  8. You will be prompted to modify your file: type the letter y for yes.

  9. The name of the file will be displayed, Press ENTER.to save editing changes for that file name.

    NOTE: This prompt for file name allows you to change the name of the file if you wish.
    By pressing ENTER, it will accept the default filename.

  10. Perform some more editing operations based on the editing chart above.

  11. Save your editing session and exit your text editor.

Part 2: Creating Text Files Using The vi Text Editor

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.

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.

Perform the Following Steps:

  1. Make certain that you are located in your home directory.

  2. Issue the following Linux command to edit a text file called othertext.txt in the linux/practice directory:
    vi textedit/practice/othertext.txt

    NOTE: When using the vi text editor, you are placed in COMMAND mode,
    so you need to issue a command to switch to INPUT mode.

    Enter the following text that appears in this diagram.
  3. Type the following key to enter INPUT mode: i

    You should notice you are in INSERT mode by seeing the notification -- INSERT --
    at the bottom left-hand side of the application screen.

  4. Enter the line shown in the other diagram of the vi text editor on the right side.

  5. While on the first line, press the following key to enter COMMAND mode: ESC

    You should see the -- INSERT -- notification disappear indicating that you are in COMMAND mode.

    NOTE: Refer to the table below for a list of
    the most common vi (vim) navigation & editing commands:

    Key(s)Purpose
    iEnter INPUT mode
    <esc>Return to COMMAND MODE
    xDelete text to the right in COMMAND mode
    WMove forward one word in COMMAND mode
    BMove back one word in COMMAND mode
    ddCut line in COMMAND mode
    yyCopy Line in COMMAND mode
    p / PPaste Cut/Copied Text below/above line in COMMAND mode
    uUndo previous editing command
    :helpDisplay help screen in COMMAND mode
    :xSave editing changes and exit (in COMMAND mode)
    :w nameSave editing changes to "name" in COMMAND mode
    :q!Abort editing session and exit (in COMMAND mode)

    Once you can get used to working in INPUT and COMMAND mode, it is easier to perform
    text editing operations in vi via COMMAND mode as opposed to nano!

  6. Type the following keys to copy the current line: yy

  7. Type the following key to paste the copied line: p

    What did you notice?

  8. Type the following keys: u

    What did you notice?

  9. Type the following keys: 3p

    What did you notice?

    Use a combination of arrow keys and the w and b keys while in COMMAND mode to change the correct words for the third and fourth lines.
  10. Use the up or down arrow keys to move to the second line.

  11. Type either the w and/or b keys to move the cursor to the beginning of the word: first.

  12. Type the x keys to remove the word called first.

  13. Type the i key to enter INSERT mode.

  14. Type the word: second

  15. Press the ESC key to enter COMMAND mode.

  16. Repeat steps 11 to 16 to change the words for line number for lines 3 and 4.

  17. Move to the 4th line (i.e. last line).

  18. Type the following keys: dd

    What did you notice?

    Let's save editing changes and exit the vi text editor.

  19. Type the following keys: :x and press ENTER

    What did you notice?

  20. Issue the same command that you performed in Step #2 to confirm that you had properly edited that file.

  21. Save and exit your vi editing session.

    The online vi-tutorial provides users "hands-on" experience of using the vi text editor.
    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 online tutorial in your Matrix account to learn how to create and edit text files with the vi text editor.

  22. Issue the following to run the vi online tutorial:
    /home/jason.carman/vi-tutorial

  23. In the tutorial menu, select the first menu item labelled "USING THE VI TEXT EDITOR"

  24. 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. As far as this author is aware,
    there is NO "hands-on" tutorial for the nano text editor in this particular format.

  25. 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".

  26. When you want to exit the tutorial, select the menu option to exit the tutorial.

It is recommended to try both text editors, and choose the text editor that you feel that is easier to use.

Part 3: Manage & 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.

Perform the Following Steps:

  1. Make certain that you are located in your home directory.

  2. Refer to the following table of Text File Management Commands:

    Linux CommandPurpose
    touchCreate empty file(s) / Updates Existing File's Date/Time Stamp
    catDisplay text file's contents without editing (small files)
    more , lessDisplay / Navigate within large text files without editing
    cpCopy text file(s)
    mvMove / Rename text files
    rmRemove text file(s)
    sortSorts (rearranges) order of file contents when displayed. Content is sorted alphabetically by default. The -nItalic text option sorts numerically, -r performs a reverse sort
    head , tailDisplays the first / last 10 lines of a text file by default. An option using a value will display the number of lines (e.g. head -5 filename will display first 5 lines, tail -5 filename will display last 5 lines).
    grepDisplays file contents that match a pattern
    uniqDisplays identical consecutive lines only once
    diff file1 file2Displays differences between 2 files
    fileGives info about the contents of the file (e.g. file with no extention)
    findTo find files matching specified characteristics:

  3. Issue the following Linux command to create three empty text files in your current directory:
    touch a.txt b.txt c.txt

  4. Issue the following Linux command: ls -l a.txt b.txt c.txt

    Check the size in the detailed listing to confirm that these newly-created files are empty.

    Nano text editor containing numbers 1 to 40
    on separate lines.
  5. Use the nano 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).

  6. Save and exit your editing session.

  7. Issue the following Linux command: cat a.txt

    Can you see all of the contents?

  8. Issue the following Linux command: more a.txt

    NOTE: The more command uses the same navigation keys as with the man command
    (refer to week 1 notes for reference). Try using keys that you used to navigate the man pages.

    What is the advantage of using the more command?

  9. Type the letter "q" to exit the more command.

  10. Issue the following Linux command: less a.txt

    Is there any difference between the more and less commands?
    (again press q to quit)

  11. issue the following Linux command: sort a.txt

    Why does the output not look what you expected? Why?

  12. Issue the following Linux command: sort -n a.txt

    Try the same command using both the -n and -r options to see what happens.

  13. issue the following Linux command: head a.txt

    What is the output from this command display?

  14. issue the following Linux command: head -7 a.txt

    What is the output from this command display?

  15. issue the following Linux command: tail a.txt

    What is the output from this command display?
    How would you issue this command to display only the last line contained in that file?

  16. Issue the following Linux command: grep 2 a.txt

    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".
  17. 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).

  18. Save your editing session and exit your text editor.

  19. Issue the following Linux command: uniq a.txt

    What do you notice happened to those newly created lines?

  20. Issue the following Linux command: cp a.txt a.txt.bk

  21. Issue the following Linux command: cp a.txt b.txt

  22. Issue the following Linux command: mv a.txt aa.txt

  23. Issue a Linux command to view the directory contents.

    What happened to the file called a.txt? Why?

  24. Issue the following Linux command: file b.txt

    What sort of information did it provide?

  25. Issue the following Linux command: diff aa.txt b.txt

    Was there any output? If not, why?

  26. Issue the following Linux command: diff aa.txt c.txt

    What do you think is the purpose of this output?

  27. Issue the following Linux command: find -P .

    What is the output of this command?

  28. Issue the following Linux command:
    rm aa.txt b.txt a.txt.bk c.txt

  29. Issue the ls command to verify that these files have been removed.

  30. Complete the LINUX PRACTICE QUESTIONS to get additional practice.

LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

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. “/”).

Tree-diagram.png
  1. Write a single Linux command to create the directory structure starting from your home directory from the diagram displayed on the right.
  2. Write a Linux command to display a detailed listing of history directory.
    How would this command differ if you wanted to also view hidden files as well?
  3. Write a Linux command to change to the project directory.
    What command would you issue to return to your home directory?
  4. Write a Linux command to copy the project directory and its contents to the history directory.
  5. Write a Linux command to move the directory called directories to the history directory.
  6. Write a Linux command to remove both directories named 1 and 2.
  7. Write a Linux command to remove the concepts directory and its contents.
  8. Write a Linux command to remove the concepts directory and prompt the user if they want to remove this directory’s contents.
  9. Write a single Linux command to create the following empty files in the concepts directory:
    • myfile.txt
    • yourfile.txt
    • thefile.txt
  10. Write a Linux command to view the contents of the myfile.txt text file to prove it is empty. What is the difference between the commands: cat, more and less?
  11. Write a Linux command to sort the contents of a file called practice/customers.txt
  12. Write a Linux command to display the first 4 lines of a file called practice/customers.txt
  13. Write a Linux command to display the last line of a file called practice/customers.txt
  14. Write a Linux command to match a line containing the pattern Linux in a file called practice/customers.txt
  15. Write a Linux command to display unique occurrences of consecutive lines in a file called practice/customers.txt
  16. Create a table listing each Linux command, useful options that are displayed near the top of this tutorial labelled: Tutorial Reference Material

_________________________________________________________________________________

Author: Murray Saul

License: LGPL version 3 Link: https://www.gnu.org/licenses/lgpl.html

_________________________________________________________________________________