Changes

Jump to: navigation, search
INVESTIGATION 1: ABSOLUTE / RELATIVE / RELATIVE-TO-HOME PATHNAMES
|- valign="top" style="padding-left:15px;"
|colspan="1" |Course NotesSlides:<ul><li>[https://ict.senecacollege.ca/~murray.saul/uli101/notes/Week02ULI101-Week3.pdf PDF] | [https://ict.senecacollege.ca/~murray.saul/uli101/notes/Week02ULI101-Week3.pptx PPTX]</li></ul>
Filename Expansion Symbols
* Asterisk '''*''' , Question '''?''' ,<br>Character Class '''[ ]''' and '''[! ]'''<br>
 
| style="padding-left:15px;"|Quotation Symbols
|}
 
= KEY CONCEPTS =
===Pathname Types===
====Relative-to-home Pathnames====
<table align="right"><tr><td>[[Image:relative-to-home-1.png|thumb|right|250px|Directories in red display the path from the home directory of the current user (which is '''userid''') to the '''examples''' directory (i.e. the relative-to-home pathname: '''~/uli101/examples'''.]]</td><td>[[Image:relative-to-home-2.png|thumb|right|250px|Directories in red display the path from the current another user's home directory location (which is '''uli101''') to the their '''examplesnotes''' directory (i.e. the relative -to-home pathname: '''~jane/uli101/examplesnotes''' or ./uli101/examples]]</td></tr></table>
You can specify a pathname as relative-to-home by using a tilde and slash at the start, e.g. '''~/uli101/notes.html'''
But this method is '''inefficient''': it requires a LOT of typing and knowing which filenames to include as separate arguments.<br>You can use a special character to indicate to the Bash shell to match all files that end with the extension ".txt":
[[Image:globbing-demo-2.png|thumb|right|400px|'''Globbing''' is the process of expanding filenames as separate arguments that match an argument that uses a '''wildcard symbol''' <br>(such as '''*''').]]
<span style="font-family:courier;>'''ls *.txt'''<br>
===Quoting Special Characters===
xLike discussed in the above section, there are some special characters that the shell uses to perform<br>an operation; for example, the filename expansion symbols: '''*''' or '''?'''  There are '''3 methods''' to make those special characters '''act like text characters''' (referred to "'''quoting'''" special characters):  <table cellpadding="5"><tr><th style="border-bottom: 1px solid black;text-align:left;">Quoting Method</th><th style="border-bottom: 1px solid black;text-align:left;">Example</th></tr><tr><td>Place the character '''\''' <u>before</u> a special character</td><td><span style="font-family:courier">'''echo \*'''</span></td></tr><tr valign="top"><td>Contain special characters within '''double-quotes'''<br>'''NOTE:''' Double quotes works for most special<br>characters, but not all special characters (such as $)<br></td><td><span style="font-family:courier">'''echo "* hello *"'''</span></td></tr><tr valign="top"><td>Contain Special character within single '''quotes'''<br>(Quotes out ALL special characters)</td><td><span style="font-family:courier">'''echo '* hello *''''</span></td></tr></table>
=INVESTIGATION 1: ABSOLUTE / RELATIVE / RELATIVE-TO-HOME PATHNAMES=
'''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 to ensure you created the CORRECT directory structure displayed as in the diagram:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week3-check-1</span><br><br>
# If you encounter errors and re-run the checking script until you receive a congratulations message.<br><br>
<div style="font-family:courier;margin-left:50px;font-weight:bold;">abc.txt<br>def.text<br>hij.TxT<br>1a4.txt<br>123.TXT<br>456.txt<br>6u9.txt<br>
ab2.html<br>1234.txt<br>abcdef.txt<br>abcde.txt<br><br></div>
<ol><li value="4">Issue the '''ls''' command to get a listing of files in your commands directory.<br><br>The output should look identical to the diagram displayed below.<br><br>You can refer to this listing to see all files so you can then predict the output from Linux commands that use filename expansion symbols.<br><br>[[Image:listing-1.png|left|650px|thumb|]]<br><br><br><br></li><li>Before proceeding, let's run a checking script to make certain that you created these regular files correctly:<br>Make certain that your current directory is '''your home directory'''.<br><br></li><li>Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week3-check-3</span><br><br></li><li>If you encounter errors and re-run the checking script until you receive a congratulations message, and continue in this section.<br><br>'''NOTE: To become comfortable with using filename expansion symbols, an excellent practice method is to create multiple files (you have already done that), look at the Linux command using filename expansion symbols and view the listing of files in the current directory that match and then write down on a paper the expected output of commands that use filename expansion symbols. You can then run the command to see the results in order to check your work.'''<br><br>Let's try this for each of the situation below:<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls ???.txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls ?????.txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls ??????.txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls [0-9].txt</span><br>Write down the expected output on paper, then issue the command to check your answer. Did the command work? What does this teach you about the character class [ ] symbol?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls [0-9][0-9][0-9].txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls [a-z][a-z][a-z].txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command (using character class with UPPERCASE letters): <span style="color:blue;font-weight:bold;font-family:courier;">ls [A-Z][A-Z][A-Z].txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command (using character class using alpha-numeric characters): <span style="color:blue;font-weight:bold;font-family:courier;">ls [a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9].txt</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls *.txt</span><br>Write down the expected output on paper, then issue the command to check your answer. Did ALL text files get listed? Why not?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls *.[tT][xX][tT]</span><br>Write down the expected output on paper, then issue the command to check your answer.Did ALL text files get listed this time? If so, why?<br><br></li><li>'''NOTE:''' We have just been using filename expansion symbols just with the ls command.<br>Filename expansion symbols can be used for ANY Linux file management command (e.g. '''cat''', '''more''', '''less''', '''cp''', '''mv''', '''rm''', '''ls''', etc.).<br><br>Let's get some practice issuing these other Linux file management commands.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">file *.[tT][xX][tT]</span><br>Write down the expected output on paper, then issue the command to check your answer.<br><br><li>Change to the '''commands''' directory using an '''absolute''' pathname.<br><br></li><li>Issue a Linux command to confirm that you are now in the '''commands''' directory.<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/l.* .</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">rm *</span><br><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/l? .</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">rm l[!s]</span><br>View the contents of the contents directory. What did this command do?<br><br></li><li>Use a text editor (nano or vi) to create the file called '''ab''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file ab</span><br><br></li><li>Use a text editor (nano or vi) to create the file called '''cd''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file cd</span><br><br></li><li>Use a text editor (nano or vi) to create the file called '''ef''' in the '''commands''' directory that contains the line of text below,<br>and then save editing changes to this file:<br><span style="font-family:courier;">This is file ef</span><br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat ??</span><br><br>View the contents of the contents directory. What did this command do? Why does the output look strange?<br><br></li><li>Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat [!l][!s]</span><br><br>View the contents of the contents directory. What did this command do? Does the output look better? If so, why?<br><br></li><li>Before proceeding, let's run a checking script to make certain that you created these regular files correctly:<br>Make certain that your current directory is '''your home directory'''.<br><br></li><li>Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">bash /home/murray.saul/scripts/week3-check-4</span><br><br></li><li>If you encounter errors and re-run the checking script until you receive a congratulations message, and continue in the next INVESTIGATION.<br><br></ol>
= INVESTIGATION 3: QUOTING SPECIAL CHARACTERS =
In this section, you will learn 3 methods to quote special characters so they behave only like text.
x:'''Perform the Following Steps:'''
# Make certain that you are currently located in your home directory.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo hello there</span><br><br>'''NOTE:''' the '''echo''' command is used to display text onto your terminal.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo * hello *</span><br><br>What happened? What is shown in addition to the text "hello". Why do you think those filenames are also being displayed?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo \* hello*</span><br><br>What do you notice? What does the \ character do?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo "* hello *"</span><br><br>Is there a difference between this command and the previous command?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo '* hello *'</span><br><br>Is there any difference between this command and the two previous commands?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">echo $USER</span><br><br>NOTE: USER is an environment variable that stores the current user's login name. The $ character immediately followed by the environment name causes it to expand to the value that the variable contains. Therefore the echo command displays the current login name of the user.<br><br># Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">echo '$USER'</span><br><br>What happened? Why is the output display like this?<br><br># Issue the following Linux command (using double quotes): <span style="color:blue;font-weight:bold;font-family:courier;">echo "$USER"</span><br><br>What happened? Why?<br><br># Let's use quoting special characters for a couple of other commands. Move to the '''lesson2''' directory. Confirm that you are currently located in the ''lesson2'' directory.<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls .</span><br><br># Issue the '''ls'''Perform command to verify that there are copied files in your lesson2 directory.<br><br>NOTE: Although it is NOT RECOMMENDED to create a filename using a special character (remember that file naming rule?), we will create an empty file called "*"<br><br># Issue the Following Stepsfollowing Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">touch '*'</span><br><br># Issue the '''ls''' command. Do you now see a file called "*" in addition to the other copied files?<br><br># Issue the following Linux command to remove the file called "*": <span style="color:blue;font-weight:bold;font-family:courier;">rm *</span><br><br># Issue the '''ls''' command to view the files in your current directory. What happened?!? Why is it dangerous to use special characters when creating filenames?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp /bin/ls .</span><br><br># Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">touch '*'</span><br><br># Issue the following Linux command (using single quotes): <span style="color:blue;font-weight:bold;font-family:courier;">rm '*'<br><br># Issue the '''ls''' command to confirm that the file called "*" has been in your current directory. What happened this time?<br><br># After you complete the Review Questions sections to get additional practice, then work on your online assignment 1 and complete the '''remaining''' sections 3 to 6 labelled: '''Directory Management''' , '''Practice Commands To Create A Directory Structure''' , '''Create A Directory Structure''' and '''Practice Specifying Path Names'''. When you have completed these remaining sections, and you main menu indicates you have completed 6 out of 6 sections, then you will have successfully completed your assignment #1.<br><br>
# X
<br><br>
= LINUX PRACTICE QUESTIONS =
[[Image:dir13week3-practice-question.png|left|300px|thumb|]]
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
# Write a single Linux command using relative pathnames to create the directory structure displayed in the diagram above.
# Write a single Linux command to create the empty files “.answers.txt” and “questions.txt” shown in the diagram above using absolute pathnames.
# Write a Linux command to display the following message:<br>*** Hello ***<br><br>
# Write a Linux command to display the following message (including quotation marks):<br>“This is my message”<br><br>
 
 
 
 
 
 
[[Category:ULI101]]
13,420
edits

Navigation menu