Open main menu

CDOT Wiki β

Changes

Tutorial 2 - Unix / Linux File Management

15,518 bytes added, 18:12, 30 August 2021
INVESTIGATION 1: MANAGING DIRECTORIES
When you log into your Matrix account, you are automatically directed to your '''home''' directory.<br>This directory is where the user can store files, and create subdirectories to organize their files.
= INVESTIGATION 1: CREATING &amp; MANAGING DIRECTORIES = <span style="color:red;">'''ATTENTION''': The due date for successfully completing this tutorial (i.e. tutorial 2) is by Friday, November 5th by midnight (Week 8).</span><br><br> In this investigation, you will learn how to '''create''', '''navigate''', '''list directory contents''' and '''remove''' directories in your Matrix account.  '''Directory File Naming Rules''' 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 === [[Image:1_-_700px-Directory-structure-3.png|thumb|right|350px|'''Tree Diagram''' of Directory Structure to Create in your '''Home''' Directory (displayed in blue text).]]Creating subdirectories within your home directory makes it more '''efficient''' to<br>save and access files on your Linux server. A comparison would be rooms in a '''house'''. If there were no rooms, just one large room in a 3,000 square foot house, it would be "messy" and difficult to locate items. Each '''room''' in a house is used to for a<br>'''specific purpose''' to be more productive to perform a task such as a kitchen, bedroom, bathroom, etc. You will be creating the following '''directory structure''' within your home directory<br>(refer to diagram on the right side).  '''Perform the Following Steps:''' '''NOTE:''' For several of the following commands, you will be using '''<u>your</u> Seneca username''' to replace the text:"'''your-seneca-id'''" in a pathname.<br><br>For example, if your Seneca user ID was '''mkhan''' then you would display<br>the pathname ''/home/your-seneca-id'' as: '''/home/mkhan'''<br><br> # '''Login''' to your matrix account (you should know how to do this from performing [https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_1:_ACCESSING_YOUR_MATRIX_LINUX_ACCOUNT Tutorial 1 INVESTIGATION 1]]).<br><br># Issue a command to '''confirm''' that you are located in your home directory<br>(you should know how to do this from performing [[https://wiki.cdot.senecacollege.ca/wiki/Tutorial_1:_Using_Your_Matrix_Server_Account#INVESTIGATION_2:_USING_THE_LINUX_SHELL_.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>'''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.  <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 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> ===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''' 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 -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 uli101xxx</span><br><br>Note the differences between both of these commands (i.e. using and <u>not</u> using a pathname).<br><br>You can copy and move entire directories (and their contents) to other directories.<br><br><table align="right"><tr valign="top"><td>[[Image:directory-structure-7.png|thumb|right|250px|Output of the '''tree''' command to confirm copy of '''uli101xx''' directory (and contents) to the '''xyz100xx''' directory.]]</td><td>[[Image:directory-structure-8.png|thumb|right|300px|Output of the '''tree''' command to confirm movement of '''acp100 directory''' directory (and contents) to the '''xyz100xx''' diredtory.]]</td></tr></table># Issue the following Linux command:<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> ===Part 3: Removing Directories === In this investigation, we will learn how to '''safely remove''' directories and their contents.  '''Perform the Following Steps:''' # 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>
= INVESTIGATION 2: MANAGING TEXT FILES =
= LINUX PRACTICE QUESTIONS =