Changes

Jump to: navigation, search

Tutorial2: Unix / Linux File Management

250 bytes removed, 11:04, 9 January 2020
INVESTIGATION 1: MANAGING DIRECTORIES
* 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 later
* 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.
# '''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 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; 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 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>'''NOTE:''' You should now notice that you can create multiple directories by issuing 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-family: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;font-weight:bold;font-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''' 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 a single Linux command.<br><br>Using this inefficient method tends to show that you are a novice or "'''newbie'''", and you may '''lose marks''' if you issue multiple Linux commands to performed tasks that can be performed using only a single Linux command.<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 structure''' 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 -r uli101 acp100 xyz100</span><br><br>'''NOTE:''' 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</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 uli101 uli101/notes uli101/tutorials uli101/examples acp100 xyz100</span><br><br>
'''Downloading & Running a Shell Script to Check your Work'''
Although you are being asked to create the directory structure, this author has no idea that you might have performed it correctlymade some mistakes:<br>For Example:<br>
* Not creating directories or some directoriesForgetting to create a directory.* You are making mistakes in directory name syntax 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 that you haven't made 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.
'''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 -lR''' 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 section. 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 uli101</span><br><br>Note the differences between both of these commands.<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 '''uli101''' directory (and contents) to the '''xyz''' 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 '''xyz''' diredtory.]]</td></tr></table>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cp -R uli101 xyz100</span><br><br>Note the differences between both of these commands.<br><br>
#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">tree</span><br><br>
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">mv acp100 xyz100/uli101/tutorials</span><br><br>Note the differences between both of these commands.<br><br>
#Issue the following Linux command to display the directory structure of your home directory to confirm you copied the uli101 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>
===Part 3: Removing Directories ===
In a previous section, you learned to remove empty and non-empty directories to learn how to create a directory structure more efficiently.<br>In this section, 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 '''uli101/tutorials''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101/tutorials</span><br><br>
# Issue the following Linux command to remove the empty directory called '''uli101''': <span style="color:blue;font-weight:bold;font-family:courier;">rmdir uli101</span><br><br>NOTE: You should get an error message, since the '''uli101''' 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 '''uli101''' directory and its contents: <span style="color:blue;font-weight:bold;font-family:courier;">rm -r uli101</span><br><br>
# Issue the '''tree''' command to confirm that the uli101 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 xyz100</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 you have removed all of the directories that you earlier created.<br><br>
# Make certain that your current directory is '''your home directory'''.<br><br>
# Issue the following Linux command to download a '''shell script''' to your home directory that will check your work:<br><span style="pointer-events: none;cursor: default;color:blue;font-family:courier;font-weight:bold">wget https&#58;//matrix.senecacollege.ca/~murray.saul/uli101/week1-check-3</span><br><br>
13,420
edits

Navigation menu