Open main menu

CDOT Wiki β

Changes

Tutorial2: Unix / Linux File Management

12,011 bytes added, 05:30, 2 June 2021
Part 2: Viewing Directory Contents / Copying & Moving Directories
===Main Objectives of this Practice Tutorial===
:* Understand the Purpose purpose of the Unix / Linux '''Directory Structuredirectories''' and '''directory pathnames'''.
:* List Use common '''Common DirectoriesUnix / Linux commands''' that are Contained in a Typical to perform '''Unix / Linux Filesystemdirectory management'''tasks.
:* Use Common '''Unix / Linux CommandsText Editors''' to Perform '''Directory Management''' Tasks.create and modify text files
:* Use Text Editors Common Unix / Linux commands to Create '''manage''' and Modify Text Files :* Use Common '''Unix / Linux Commandsmanipulate''' to Manipulate text filssfiles.
<br><br>
|- valign="top" style="padding-left:15px;"
|colspan="1" |Course NotesSlides:<ul><li>[https://ict.senecacollege.ca/~murray.saul/uli101/notes/Week02ULI101-Week2.pdf PDF] | [https://ict.senecacollege.ca/~murray.saul/uli101/notes/Week02ULI101-Week2.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><li>[http://czegel.com/seneca/uli101/lectures/Lecture2.html Les Czegel's Week 2 Notes (HTML)]</li></ul>
*[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/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/file.1.html file]<br>
|}
 
= 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".<br><br>A path points to a file system location by ]]To better '''following the directory tree hierarchyorganize''' expressed in a string of characters in which path componentsfiles (eg. text, images, documents, spreadsheets, separated by a delimiting characterprograms) within your Matrix account, represent each directory. The they should be stored in '''delimiting character''' is most commonly the slash ("'''/directories'''").]]The UnixTo further organize <u>many</Linux file system is '''hierarchical'''u> files, similar to other operating systems such asdirectories<br>may contain '''Windows'sub-directories'', '''Mac OSX''', etc.Files are organized in directories. Directories may contain sub-directories.
The Unix/Linux file system is '''hierarchical''', like other operating systems such as '''Windows''', '''Mac OSX''', etc.In Unix / Linux (as opposed to MS Windows), there are no drive letters (such as '''C:''', or '''D:'''). <br>All files and directories appear under a single ancestor directory called the "'''root directory'''".
Learning how to issue Linux commands for '''navigating the Linux filesystem ''' 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. are can be createdas required. The hierarchical structure resembles an "''upside- down tree''". There is actually a command called '''tree''' that can display displays a "'''directory tree diagram'''"!
<br><br>
=== Directory Pathnames ===
[[Image:path-name.png|thumb|right|180px|A pathname points to a file system location by '''following the directory tree hierarchy'''.]]
<i>A '''pathname''' is used to specify a '''route to a file''' within the file system.<br>
<br>
A pathname '''points''' to a file system location by '''following the directory tree hierarchy''' expressed in a string of characters in which path components, separated by a delimiting character, represent each directory. The '''delimiting character''' is most commonly the slash ("'''/'''").</i> Reference: https://en.wikipedia.org/wiki/Path_(computing)
A The following table displays and defines '''path''', the general form of the name of a file or directory, specifies a '''unique locationcommonly used directories''' in a file system.<br>A path points to a file system location by '''following the directory tree hierarchy''' expressed in a string of characters in which path components, separated (listed by a delimiting character, represent each directory. The '''delimiting character''' is most commonly the slash ("'''pathname) for for ALL Unix /'''"). ReferenceLinux Filesystems: https://en.wikipedia.org/wiki/Path_(computing)<br>
 
The following table displays and defines commonly used directories<br>(listed by directory pathname) for for ALL Unix / Linux Filesystems:<br>
 
Please take a few moments to review these pathnames.
<br><br>
When you log into your Matrix account, you are automatically directed to your '''home''' directory. <br>This directory is where the user can store files, and create subdirectories to organize their files.
We will now learn to create and manage subdirectories within <u>your</u> home directory.<br><br>=INVESTIGATION 1: CREATING &amp; MANAGING DIRECTORIES=
<span style=INVESTIGATION 1"color: MANAGING DIRECTORIES=red;">'''ATTENTION''': Depending on your ULI101 instructor, you may be required to complete this tutorial for '''marks''' in this course.<br>Please refer to your instructor's course notes and lecture notes regarding evaluation for this course.<br><br>The due date for successfully completing this tutorial (i.e. '''tutorial 2''') is by '''Friday by midnight''' next week (i.e. '''Week 3''').<br>If your instructor has NOT assigned marks for completing this tutorial, you can perform it for '''practice'''.</span><br><br>
<br>In this investigation, you will learn how to '''create''', '''navigate''', '''listing 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''':
* 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 ===
[[Image:directory-structure-3.png|thumb|right|350px|'''Tree Diagram''' of Directory Structure to Create in your '''Home ''' Directory (displayed in blue text).]]Creating subdirectories within your home directory makes it more '''efficient''' to <br>save and access files on your Linux server. A comparison would be rooms in a '''house'''. If there were no rooms, just one large room in a 3,000 square foot house, it would be "messy" and difficult to locate items. Each '''room''' in a house is used to for a specific purpose to be more productive to perform a task such as a kitchen, bedroom, bathroom, etc.  You would like to create a directory structure within your home directory as displayed in the diagram on the right aide.
A comparison would be rooms in a '''house'''. If there were no rooms, just one large room in a 3,000 square foot house, it would be "messy" and difficult to locate items. Each '''room''' in a house is used to for a<br>'''specific purpose''' to be more productive to perform a task such as a kitchen, bedroom, bathroom, etc.
You should note from will be creating the previous section that the '''rootfollowing ''' directory is the <br>"'''starting point'''" in the Matrix file system, that the '''home''' directory is used to store all Matrix user accounts by their userid, and that structure'''within your userid''' contained within the home directory represents '''<ubr>'''YOUR'''</u> home directory''' where you can create files (both directory files, text files, etcrefer to diagram on the right side).
'''Perform the Following Steps:'''
# '''Login''' your matrix account.<br><br># Issue a command to '''confirm''' you are located in your home directory. You should know how to do this from the previous tutorial.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mkdir uli101</span><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 For several of a Directory.]]# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls uli101</span><br><br>There are no contents that are contained in this newly-created directory; thereforecommands, no contents appear. A useful option you will be using '''-d''' can be used to confirm that the actual <u>directoryyour</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 uli101</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 uli101</span><br><br>How can you confirm from the output of this command that the file uli101 is a directory?<br><br># Issue the following Linux command:<span style="color:blue;font-weight:bold;font-family:courier;">ls -ld uli101</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 acp100 and xyz100 directories:<span style="color:blue;font-weight:bold;font-family:courier;">mkdir acp100 xyz100</span><br><br>Seneca username'''NOTE:''' You should notice that you can create multiple directories by issuing to replace the mkdir with multiple arguments.<br><br># Issue the following Linux command to confirm that those directories have been created: <span style="color:blue;font-weight:bold;font-familytext:courier;">ls -ld acp100 xyz100</span><br><br># We will now create the subdirectories that are contained in the '''uli101''' directory.<br>Issue the following Linux command to move to the uli101 directory: <span style="color:blue;fontyour-weight:bold;fontseneca-family:courier;">cd uli101</span><br><br># Issue a command to confirm that your current location is in the uli101 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'id'' and '''examples''': <span style="color:blue;font-weight:bold;font-family:courier;">mkdir notes tutorials examples</span><br><br># Issue a Linux command to confirm that those directories have been created. You should know how to do this...<br><br>#This technique is considered to be '''inefficient'''. There are options and ways to create the same directory structure without using the cd command and only issue in a single Linux commandpathname.<br><br>Using this inefficient method tends to show that you are a novice or "'''newbie'''"For example, and you may if your Seneca user ID was '''lose marksmkhan''' if then you issue multiple Linux commands to performed tasks that can be performed using only a single Linux command.would display<br><br>Although we will teach you how to '''remove''' directories and their contents in a future section, let's change back to your '''home''' directory and issue a command to '''remove the directory structurepathname ''' that you just created so you can learn a more efficient method of creating the same directory structure.<br><br># Issue Linux commands to move to your /home directory and confirm that your current directory is /your home directory. You should know how to do this...<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 seneca-r uli101 acp100 xyz100</span><br><br>id'''NOTEas:''' You will learn how to '''safely''' remove directories and their contents in another section.<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 uli101/notes uli101/tutorials uli101/examples acp100 xyz100<home/span><br><br>'''NOTE:''mkhan' 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 uli101 uli101/notes uli101/tutorials uli101/examples acp100 xyz100</span><br><br>
# '''Login''' to your matrix account (you should know how to do this from performing [https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_1:_ACCESSING_YOUR_MATRIX_LINUX_ACCOUNT Tutorial 1 INVESTIGATION 1]]).<br><br># Issue a command to '''confirm''' that you are located in your home directory<br>(you should know how to do this from performing [[https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_2:_USING_THE_LINUX_SHELL_.2F_ONLINE_ASSIGNMENTS 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 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''' that been removed.<br><br># Issue the following single Linux command to create the entire directory structure:<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir -p uli101xx/notes uli101xx/tutorials uli101xx/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>'''Downloading & 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 syntax errors (eg. spelling or mixing up character case)
* Not creating subdirectories within specified directories.
<table align="right"><tr valign="top"><td>[[Image:script-check-1.png|thumb|right|250px|If all all check pass, then user performed task correctly and can continue.]]</td><td>[[Image:script-check-2.png|thumb|right|450px|If there is a warning, then feedback is provided to user to correct and re-run checking script.]]</td></table>
To check that you haven't made for mistakes so you won't encounter problems in the next section , 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.
# Make certain that your current directory is '''your home directory'''.<br><br>
# Issue the following Linux command to download run a '''shell checking script''' to your home directory that will check your work:<br><span style="pointer-events: none;cursor: default;color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">wget https&#58;//matrix.senecacollege.ca/~murray.saul/uli101/week1week2-check-1</span><br><br># Run 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 check your work by issuing the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash week1-check-1</span><br>next part.<br>
===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 <br>to '''view directory contents contained in directories, ''' as well as '''copy ''' and '''move ''' directories.
'''Perform the Following Steps:'''
<table align="right"><tr valign="top"><td>[[Image:directory-structure-5.png|thumb|right|250px|Output of the '''tree''' command to display directory structure.]]</td><td>[[Image:directory-structure-6.png|thumb|right|250px|Output of the '''ls -lRR''' command to display directory structure.]]</td></table># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br>'''NOTE:''' You should see the directory structure that you created in the previous sectioninvestigation. You can also issue the '''tree ''' command using a directory pathname to display the directory structure for a specific scope.<br><br>You can also use the '''-R ''' option for the '''ls ''' command to display all directories and subdirectories for a specified directory path (referred to as a '''recursive directory listing''').<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR</span><br><br>What directories do you see?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -lR uli101uli101xxx</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 '''uli101uli101xx''' directory (and contents) to the '''xyzxyz100xx''' 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 '''xyzxyz100xx''' diredtory.]]</td></tr></table># Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101 xyz100uli101xx xyz100xx</span><br><br>#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 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 acp100 acp100xx xyz100xyz100xx/uli101uli101xx/tutorials</span><br><br>#Issue the following Linux command to display the directory structure of your home directory to confirm you copied moved the uli101 acp100xx directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br>Let's download and 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 to download a '''shell run checking script''' to your home directory that will check your work:<br><span style="pointer-events: none;cursor: default;color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">wget https&#58;//matrix.senecacollege.ca/~murray.saul/uli101/week1week2-check-2</span><br><br># Run If you encounter errors. make corrections and re-run the checking script until you receive a congratulations message, and proceed to check your work by issuing the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash week1-check-2</span>next investigation.<br><br>
===Part 3: Removing Directories ===
In this sectioninvestigation, we will learn how to '''safely remove''' directories and their contents.
# 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 '''uli101/tutorials''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101uli101xx/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 '''uli101uli101xx''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101uli101xx</span><br><br>NOTE: You should get an '''error message''', since the '''uli101uli101xx''' 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 '''uli101uli101xx''' directory and its contents: <span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101uli101xx</span><br><br># Issue the '''tree''' command to confirm that the uli101 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 xyz100xyz100xx</span><br><br>'''NOTE:''' You should have removed all directories that you have created.<br><br>Let's download and run a checking script to see if confirm that you have correctly removed all of the those directories that you earlier created.<br><br># Make certain that your current directory is Issue the '''your home directorytree'''command to confirm that all of those recently created directories have been removed.<br><br># Issue the following Linux command to download run a '''shell checking script''' to your home directory that will check your workconfirm removal of those directories:<br><span style="pointer-events: none;cursor: default;color:blue;font-familyweight:courierbold;font-weightfamily:boldcourier;">wget https&#58;//matrix.senecacollege.ca/~murray.saul/uli101/week1week2-check-3</span><br><br># Run If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message, and proceed to check your work by issuing the following:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash week1-check-3</span>next INVESTIGATION.<br><br>
=INVESTIGATION 2: MANAGING TEXT FILES=
<table align="right"><tr valign="top"><td>[[Image:nano-text-editor-1.png|thumb|right|250px|The '''Nano Text Editor''' is Easy to Operate for Novice Users.]]</td><td>[[Image:vi-text-editor-1.png|thumb|right|300px|The Vi '''vi Text Editor''', although taking longer to master, has outstanding features and allow the user to be more productivewith editing text files.]]</td></tr></table>
<br>
Since IT students will be working in the Unix / Linux command line environment, it is useful for them to learn a least a couple of 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 in the Linux system, 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''' would find can use a text editor useful to '''edit configuration files'''. These students in upper grades will become familiar with the process of installing, configuring, and running network services and text editors are an important tools<br>to help setup but also "tweak" or make '''periodic changes in networking services configuration'''.
A general rule is for a student to '''expose themselves to a number of different text editors and then use one that they feel most comfortable working with'''. The two most readily-available command line text editors in Linux are '''Nano''' and '''Vivi'''. <br>The Nano text editor would seem like an easier-to-use text editor, but Vi vi (although taking longer to learn) <br>has outstanding features and allow the user to be more productivewith editing text files.
<br><br>
===Part 1: Creating Text Files Using The Nano Text Editor===
<table align="right"><tr><td>[[Image:directory-structure-10.png|right|250px|thumb|]]</td><td>[[Image:nano-text-editor-2.png|right|450px|thumb|Enter the following text that appears in this diagram.]]</td></tr></table>
The tutorial for using this text editor will be very basic including '''creating''', '''editing''', and '''saving''' text files.
You will now learn basic editing skills using the '''Nano''' text editor including '''creating''', '''editing''', and '''saving''' text files.
<br><br>
'''Perform the Following Steps:'''
# Make certain that you are located in your home directory.<br><br>[[Image:directory-structure-10.png|right|150px|thumb|]]# '''Create ''' the following directory structure (displayed on the right side) by issuing a '''single Linux command:'''<br>(You should know how to perform do this taskfrom the previous INVESTIGATION).<br><br># Issue a Linux command to check that you correctly created those directories.<br><br># Issue the following Linux command to edit a text file called '''mytext.txt''' in the '''linux/practice''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">nano uli101textedit/practice/mytext.txt</span><br><br>'''NOTE:''' When using the '''Nano ''' text editor, you are placed in '''INPUT ''' mode, so you can enter text immediately.<br><br>[[Image:nano-text-editor-2.png|right|400px|thumb|Enter the following text that appears in this diagram.]]# Enter the lines shown in the other diagram of the nano text editor on the right side.<br><br>'''NOTE:''' The Refer to the table below for a list of<br>common '''^''' symbol represents press the Nano'''navigation &ltamp;ctrl&gt;''' followed by a character.editing commands:<br><br># To '''save''' your editing session, press: <span table cellpadding="5"><tr><th style="colorborder-bottom:blue1px solid black;font">Key(s)</th><th style="border-weightbottom:bold1px solid black;">Purpose</th></tr><tr valign="top"><td style="font-family:courier;">'''&lt;ctrl&gt;&lt;xSPACEBAR&gt;</span>''',<br><br># You will be prompted to modify your file: type the letter '''y&lt;esc&gt;&lt;6&gt;''' for yes.<br/td><td>Move forward/backward one word</td></tr><brtr valign="top"># The name of the file will be displayed: press <span td style="color:blue;font-weight:bold;font-family:courier;">ENTER</span>.<br><br>'''NOTE:&lt;ctrl&gt;&lt;a&gt;''' This prompt for file name allows you to change the name of the file if you wish.,<br>By pressing '''ENTER&lt;ctrl&gt;&lt;e&gt;''', it will accept the default filename.<br/td><brtd># Refer Move to the table below for a list beginning/end of the most common Nano editing commands:line<br/td><br><table cellpadding="5"/tr><tr><th td style="borderfont-bottomfamily: 1px solid black;">Keyboard Combination</th><th style="border-bottom: 1px solid black;courier">Purpose</th></tr><tr><td>'''&lt;ctrl&gt;&lt;k&gt;'''</td><td>Cut line</td></tr><tr><tdstyle="font-family:courier">'''&lt;alt&gt;&lt;M&gt;&lt;^esc&gt;6'''</td><td>Copy Line</td></tr><tr><tdstyle="font-family:courier">'''&lt;ctrl&gt;&lt;u&gt;'''</td><td>Paste Cut/Copied Text</td></tr><tr><tdstyle="font-family:courier">'''&lt;ctrl&gt;&lt;g&gt;'''</td><td>Display help screen (ctrl-x to exit help screen)</td></tr><tr><td style="font-family:courier">'''&lt;ctrl&gt;&lt;x&gt;'''</td><td>Save editing changes and exit</td></tr></table><br># Referring to the table above, practice navigating and editing your entered lines for practice.<br><br># To '''save''' your editing session, press: <span style="color:blue;font-weight:bold;font-family:courier;">&lt;ctrl&gt;x</span><br><br># You will be prompted to modify your file: type the letter <span style="font-family:courier;color:blue;font-weight:bold">y</span> for '''yes'''.<br><br># The name of the file will be displayed, Press <span style="color:blue;font-weight:bold;font-family:courier;">ENTER</span>.to save editing changes for that file name.<br><br>'''NOTE:''' This prompt for file name allows you to change the name of the file if you wish.<br>By pressing '''ENTER''', it will accept the default filename.<br><br># Perform some more editing operations based on the editing chart above.<br><br># '''Save''' your editing session and '''exit''' your text editor.<br><br>
===Part 2: Creating Text Files Using The Vi vi Text Editor=== [[Image:vi-text-editor-2screen.png|right|350px300px|thumb|The online vi-tutorial provides users "hands-on" experience of using Using the '''vi ''' text editor.]]An You will now learn basic editing skills using the '''vi'''(''online tutorial'vim'' was created two decades ago to provide you "hands-on" experience in using the Vi ') text editor.  It is including<ubr>recommended</u> that you run this online tutorial in your Matrix account to learn how to create '''creating''', '''editing''', and edit '''saving''' text files with the Vi text editor.
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:'''
# Make certain that you are located in your home directory.<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.]]# 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># Enter the line shown in the other diagram of the vi text editor on the right side.<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># 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># 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># Type the following keys: <span style="color:blue;font-weight:bold;font-family:courier;">u</span><br><br>What did you notice?<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.]]# Use the up or down '''arrow''' keys to move to the second line.<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 vi online tutorial:<br><span style="color:blue;font-weight:bold;font-family:courier;">/home/murray.saul/vi-tutorial<br><br>
# In the tutorial menu, select the first menu item labelled "'''USING THE VI TEXT EDITOR'''"<br><br>
#Read and follow the instructions in the tutorial. Eventually, it will display a '''simulated''' Vi vi environment <br>and will receive stepprovide you with "'''hands-byon'''" practice using the vi text editor. As far as this author is aware,<br>there is NO "hands-step instructions on how to use this " tutorial for the nano text editorin 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 3: Manage Text File Content===It is recommended to try both text editors, and choose the text editor that you feel that is easier to use.<br><br>
===Part 3: Manage &amp; Manipulate Text File Content=== We finish conclude this tutorial by learning to use commands that '''manage ''', '''view''' or '''manipulate the content contained instead <u>display</u>''' of text files whether it relate .<br>This is HIGHLY ADVISED in case you only want to to viewing file content or manipulating view contents and '''NOT''' edit text file contentcontents which can cause accidental erasure of data.
'''Perform the Following Steps:'''
# View Make certain that you are located in your home directory.<br><br># Refer to the following table of '''Text File Management commands Your instructor should have demonstrated in class how to use these commands)Commands:'''<br><br><table cellpadding="5"><tr><th style="border-bottom: 1px solid black;" width="20%">Linux Command</th><th style="border-bottom: 1px solid black;">Purpose</th></tr><tr><tdstyle="font-family:courier">'''touch'''</td><td>Create empty file(s) / Updates Existing File's Date/Time Stamp</td></tr><tr><tdstyle="font-family:courier">'''cat'''</td><td>Display text file's contents without editing (small files)</td></tr><tr><tdstyle="font-family:courier">'''more , less'''</td><td>Display / Navigate within large text files without editing</td></tr><tr><tdstyle="font-family:courier">'''cp'''</td><td>Copy text file(s)</td></tr><tr><tdstyle="font-family:courier">'''mv'''</td><td>Move / Rename text files</td></tr><tr><tdstyle="font-family:courier">'''rm'''</td><td>Remove text file(s)</td></tr><tr valign="top"><tdstyle="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><tdstyle="font-family:courier">'''file'''</td><td>Gives info about the contents of the file (e.g. file with no extention)</td></tr><tr valign><td style="topfont-family:courier"><td>'''find'''</td><td>To find files matching specified characteristics:<!-- <table><tr valign="top"><td width="2030%">'''<span style="font-family:courier">find . -name "file*"</span>'''</td><td>lists 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>lists 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>lists List files modified less than 5 minutes ago</td></tr><tr> valign="top"><td>'''<span style="font-family:courier">find -P .</span>'''</tabletd><td>Lists file pathnames in the current directory</td></tr></table> --> </td></tr><br/table><br># Make certain that you are located Issue the following Linux command to create three <u>empty</u> text files in your '''home''' current directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">touch a.txt b.txt c.txt</span><br><br># Use Issue the '''touch''' following Linux command to create the empty files called ''': <span style="color:blue;font-weight:bold;font-family:courier;">ls -l a.txt''', '''b.txtc.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 ''', and '''c.txt1 to 40'''<br><br>on separate lines.]]# Use the '''nano''' text editor to edit the empty file called '''a.txt'''. <br><br># Type the number "'''1'''" and press '''ENTER'''. On the second line, type the number "'''2'''" and press '''ENTER'''. <br>Continue entering increasing number values until you reach the number '''40''' on line 40<br>(refer to the diagram on the right).<br><br># '''Save ''' and '''exit''' your editing session.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat a.txt</span><br><br>Can you see all of the contents?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">more a.txt</span><br><br>Can '''NOTE:''' The '''more''' command uses the same navigation keys as with the '''man''' command<br>(refer to week 1 notes for reference). Try using keys that you view or at least used to navigate to see all of the contents? man pages.<br><br>What is the advantage of using the more command?<br><br># Type the letter "<span style="color:blue;font-weight:bold;font-family:courier;">q</span>" to exit the ''more '' command.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">less a.txt</span><br><br>Is there any difference between the '''more ''' and '''less ''' commands?<br>(again press '''q''' to quit)<br><br># issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort a.txt</span><br><br>Why does the output not look what you expected? Why?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort -n a.txt</span><br><br>Try the same command using both the '''-n''' and '''-r''' options to see what happens.<br><br># issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head a.txt</span><br><br>What is the output from this command display?<br><br># issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">head -7 a.txt</span><br><br>What is the output from this command display?<br><br># issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tail a.txt</span><br><br>What is the output from this command display?<br>How would you issue this command to display only the '''last line''' contained in that file?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">grep 2 a.txt</span><br><br>What type of output appear? Why did these lines appear (what do they all have in common)?<br><br>[[Image:end-of-line.png|right|200px|thumb|Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting of the <u>same</u> text: "'''end of line'''".]]# Edit the '''a.txt''' file and add to the bottom 5 new lines each consisting<br>of the <u>same</u> text: "'''end of line'''" (refer to diagram on right).<br><br># '''Save''' your editing session and '''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 one of the commands you learned to display the contents of the file called '''b.txt''' without editing.<br><br>What happened to this file?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">mv a.txt aaa.txt.bk</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 aaa.txt.bk b.txt</span><br><br>What was the Was there any output? Why do you think caused the result of this outputIf not, why?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">diff aaa.txt.bk c.txt</span><br><br> What reason would do you think is the purpose of this type of output occur?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">find -P . </span><br><br>What is the output of this command?<br><br>
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm aa.txt b.txt a.txt.bk c.txt</span><br><br># Issue the '''ls ''' command to verify that these files have been removed.<br><br># After you complete the Review Questions sections '''LINUX PRACTICE QUESTIONS''' to get additional practice, then work on your <br>'''online assignment#1''', '''section 2 labelled ''' "''Basic Unix Commands''" (parts '''4''' to '''6''') in this section which are respectively labelled: '<br> "''Managing Files''' " , '"''Accessing Files''' " and '"''Review Exercise'''".<br><br>
= LINUX PRACTICE QUESTIONS =
'''Review Questions:'''
 For each of the following questions, use a pathname starting from the '''root''' directory (i.e. “/”).<br><br> [[Image:directory-structuretree-11diagram.png|right|350px|thumb|]]
# Write a single Linux command to create the directory structure starting from your home directory from the diagram displayed on the right.
# Write a Linux command to display a detailed listing of '''history''' directory.<br>How would this command differ if you wanted to also view hidden files as well?<br>
# Write a Linux command to remove both directories named '''1''' and '''2'''.
# Write a Linux command to remove the '''concepts''' directory and its contents.
# Write a Linux command to remove the '''concepts''' directory and prompt the user if they want to remove this directory’s contents.<br><br># Write a single Linux command to create the following empty files in the concepts directory:<br>'''myfile.txt'''<br>'''yourfile.txt'''<br>'''thefile.txt'''<br><br>
# Write a Linux command to view the contents of the myfile.txt text file to prove it is empty.<br>What is the difference between the commands: '''cat''', '''more''' and '''less'''?
# Write a Linux command to sort the contents of a file called '''uli101practice/customers.txt'''# Write a Linux command to display the first 4 lines of a file called '''uli101practice/customers.txt'''# Write a Linux command to display the last line of a file called '''uli101practice/customers.txt'''# Write a Linux command to match a line containing the pattern '''Linux''' in a file called '''uli101practice/customers.txt'''# Write a Linux command to display unique occurrences of consecutive lines in a file called '''uli101practice/customers.txt'''
# Create a '''table''' listing each Linux command, useful options that are displayed near the top of this tutorial labelled: '''Tutorial Reference Material'''
 
[[Category:ULI101]]
13,420
edits