Difference between revisions of "Tutorial8: Links / Process Management"
(→INVESTIGATION 1: LINKING FILES) |
|||
(237 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Admon/caution|DO NOT USE THIS VERSION OF THE LAB. This page will no longer be updated.|'''New version here:''' https://seneca-ictoer.github.io/ULI101/A-Tutorials/tutorial8<br />'''Andrew's students please go here:''' http://wiki.littlesvr.ca/wiki/OPS145_Lab_7}} | ||
=LINKING FILES / MANAGING PROCESSES= | =LINKING FILES / MANAGING PROCESSES= | ||
<br> | <br> | ||
Line 5: | Line 6: | ||
:* Define the term '''i-node''' as it relates to the Unix/Linux File System | :* Define the term '''i-node''' as it relates to the Unix/Linux File System | ||
− | :* Issue the '''ls -i''' command to view '''i-node''' | + | :* Issue the '''ls -i''' command to view '''i-node''' (index) numbers associated with Unix/Linux files |
:* Define the terms '''Hard''' and '''Symbolic''' Links | :* Define the terms '''Hard''' and '''Symbolic''' Links | ||
Line 34: | Line 35: | ||
|- valign="top" style="padding-left:15px;" | |- valign="top" style="padding-left:15px;" | ||
− | |colspan="2" | | + | |colspan="2" |'''Slides:'''<ul><li>Week 8 Lecture 1 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-8.1.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-8.1.pptx PPTX]</li><li>Week 8 Lecture 2 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-8.2.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-8.2.pptx PPTX] <br></li></ul> |
− | | style="padding-left:15px;" |Links | + | | style="padding-left:15px;" |'''Links:''' |
* [http://www.linfo.org/hard_link.html#:~:text=A%20hard%20link%20is%20merely,created%20to%20other%20hard%20links. Hard Links] | * [http://www.linfo.org/hard_link.html#:~:text=A%20hard%20link%20is%20merely,created%20to%20other%20hard%20links. Hard Links] | ||
* [https://www.computerhope.com/jargon/s/symblink.htm#:~:text=Alternatively%20referred%20to%20as%20a,or%20directory%20using%20its%20path.&text=In%20Linux%20and%20Unix%20symbolic,created%20using%20the%20mklink%20command. Symbolic Links]<br> | * [https://www.computerhope.com/jargon/s/symblink.htm#:~:text=Alternatively%20referred%20to%20as%20a,or%20directory%20using%20its%20path.&text=In%20Linux%20and%20Unix%20symbolic,created%20using%20the%20mklink%20command. Symbolic Links]<br> | ||
− | Managing Processes | + | '''Managing Processes:''' |
* [https://en.wikipedia.org/wiki/Inode inode] | * [https://en.wikipedia.org/wiki/Inode inode] | ||
* [https://opensource.com/article/18/9/linux-commands-process-management Manipulating Processes] | * [https://opensource.com/article/18/9/linux-commands-process-management Manipulating Processes] | ||
− | | style="padding-left:15px;"|Linux Commands | + | | style="padding-left:15px;"|'''Linux Commands:''' |
* [http://man7.org/linux/man-pages/man1/ln.1.html ln] | * [http://man7.org/linux/man-pages/man1/ln.1.html ln] | ||
* [http://man7.org/linux/man-pages/man1/ps.1.html ps] | * [http://man7.org/linux/man-pages/man1/ps.1.html ps] | ||
Line 52: | Line 53: | ||
* [http://man7.org/linux/man-pages/man1/jobs.1p.html jobs] | * [http://man7.org/linux/man-pages/man1/jobs.1p.html jobs] | ||
* [http://man7.org/linux/man-pages/man1/kill.1.html kill] | * [http://man7.org/linux/man-pages/man1/kill.1.html kill] | ||
+ | * [https://man7.org/linux/man-pages/man1/sleep.1.html sleep] | ||
* [http://linuxcommand.org/lc3_man_pages/aliash.html alias] , [https://man.linuxreviews.org/man1p/unalias.1p.html unalias] | * [http://linuxcommand.org/lc3_man_pages/aliash.html alias] , [https://man.linuxreviews.org/man1p/unalias.1p.html unalias] | ||
* [https://mediatemple.net/community/products/dv/204404624/using-the-history-command history] | * [https://mediatemple.net/community/products/dv/204404624/using-the-history-command history] | ||
− | |colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=tZ94-qH9unM&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=9 Inodes and Links]</li><li>[https://www.youtube.com/watch?v=q93POTgH-aQ&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=7 Processes and Jobs]</li></ul> | + | |colspan="1" style="padding-left:15px;" width="30%"|'''Brauer Instructional Videos:'''<ul><li>[https://www.youtube.com/watch?v=tZ94-qH9unM&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=9 Inodes and Links]</li><li>[https://www.youtube.com/watch?v=q93POTgH-aQ&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=7 Processes and Jobs]</li></ul> |
|} | |} | ||
Line 72: | Line 74: | ||
[[Image:inode-number-3.png|thumb|right|450px|The '''i-node number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.]] | [[Image:inode-number-3.png|thumb|right|450px|The '''i-node number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.]] | ||
− | The | + | An '''i-node''' is a '''database''' containing information (e.g. ''file type'', ''owner'', ''permissions'', etc.) for all files that are created on the Unix/Linux filesystem.<br><br>The ''i-node number'' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.<br><br>Referring to the diagram on the far right, issuing the <span style="font-family:courier;font-weight:bold;">ls</span> command with the <span style="font-family:courier;font-weight:bold;">-i</span> option displays the i-node number for each file. You can see that <u>each</u> file (whether it is a directory or regular file) has its own unique<br>i-node number. |
===Hard Links=== | ===Hard Links=== | ||
Line 80: | Line 82: | ||
− | '''Advantages''' | + | '''Advantages:''' If only one hard link remains (even if original file has been removed), the '''data in that hard linked file is NOT lost'''. The data in hard linked files are '''automatically updated''' when original file are updated. |
− | '''Disadvantages''' | + | '''Disadvantages:''' Hard links '''take-up extra space''', you '''cannot hard link directories''',<br>and you '''cannot hard link files from other Unix/Linux servers''' (since the inode number may already be used by the other Unix/Linux server). |
''Examples:'' | ''Examples:'' | ||
− | <span style="font-family:courier" | + | <span style="font-family:courier">ln myfile.txt myfile1.hard.lnk<br>ln myfile.txt ~/backups/myfile.hard.lnk<br>'''</span> |
===Symbolic Links=== | ===Symbolic Links=== | ||
Line 95: | Line 97: | ||
− | '''Advantages''' | + | '''Advantages:''' symbolic links are '''shortcuts''' to other files, where the symbolic link only contains the pathname to the original file, you '''can create symbolic links'''<br>'''on different Unix/Linux servers''', and that you '''can create symbolic links for directories'''. |
− | '''Disadvantages''' | + | '''Disadvantages:''' Symbolic links are NOT good for backup purposes<br>since a symbolic link can point to a nonexistent file (referred to as a "broken link"). |
''Examples:'' | ''Examples:'' | ||
− | <span style="font-family:courier" | + | <span style="font-family:courier">ln -s otherfile.txt otherfile1.sym.lnk<br>ln -s otherfile.txt ~/backups/otherfile.sym.lnk<br>'''</span> |
<br><br> | <br><br> | ||
Line 111: | Line 113: | ||
'''Characteristics of Processes:''' | '''Characteristics of Processes:''' | ||
− | * Each process has | + | * Each process has an '''owner''' |
− | * Each process has a unique ID (PID) | + | * Each process has a unique ID ('''PID''') |
− | * Processes keep | + | * Processes keep their ''PID'' for their '''entire life'''. |
− | * Usually a parent sleeps ( | + | * Usually a parent sleeps (''suspends'') when a child is running (the exception is when the child process is running in the background) |
− | *UNIX / Linux processes are hierarchical. The process structure can have '''child processes''', '''great grandchild processes''', etc. | + | *UNIX / Linux processes are '''hierarchical'''. The process structure can have '''child processes''', '''great grandchild processes''', etc. |
− | Users can '''manage processes''' to become more '''productive''' while working in the Unix / Linux Command-line environment. | + | Users can '''manage processes''' to become more '''productive''' while working in the Unix / Linux Command-line environment.<br>Processes that run in the terminal are known as '''foreground''' processes. You can run or send processes currently running<br>in the ''foreground'' to the '''background''' to free-up your terminal (e.g. issue other Linux commands).<br><br>Below are a listing of common '''Linux commands''' and '''keyboard shortcuts''' to manage foreground and background processes: |
− | + | <table cellpadding="5" width="80%"><tr valign="top"><th width="25%" style="border-bottom: 1px solid black;text-align:left;">Linux Command /<br>Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">ps</span></td><td>Displays snapshot information about processes.<br>''Examples: ''<span style="font-family:courier;font-weight:bold">ps , ps -l , ps -ef , ps -u , ps aux</span><br></td></tr><tr valign="top"><td>'''<span style="font-family:courier;font-weight:bold;">top</span>'''</td><td>The '''top''' command provides a realtime status of running processes.<br>'''NOTE:''' You can press '''ctrl-c''' to exit<br></td></tr><tr valign="top"><td><span style="font-family:courier;">'''ctrl-c'''</span></td><td>'''Terminates''' a process running in the foreground</td></tr><tr valign="top"><td><span style="font-family:courier;">'''ctrl-z'''</span></td><td>Sends a process running in the foreground into the '''background'''.</td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">fg</span></td><td>Moves a background job from the current environment into the foreground.<br>''Example: ''<span style="font-family:courier;font-weight:bold">fg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">bg</span></td><td>Runs (starts) the most recent process that was placed into the background.<br>''Example: ''<span style="font-family:courier;font-weight:bold">bg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">jobs</span></td><td>The '''jobs''' utility displays the '''status''' of jobs that were started in the current shell environment. ''Example:<br>''<span style="font-family:courier;">jobs<br>[1]+ Stopped vim a <span style="font-size:8pt;"> <-- Job #1 (+ most recent process / background)</span><br>[2] Running sleep 200 & <span style="font-size:8pt;"> <-- Job #2</span><br>[3] Running sleep 300 & <span style="font-size:8pt;"> <-- Job #3</span><br>[4]- Running sleep 400 &<span style="font-size:8pt;"> <-- Job #4 (- second recent process / background)</span><br><br></span></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">kill</span></td><td>The kill command sends the specified signal to the specified processes or process groups. If no signal is specified, the '''TERM''' signal is sent. The default action for this signal is to terminate the process.<br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">kill PID , kill -9 PID , kill %job-number , <br>kill -9 %job-number</span></td></tr></table> | |
− | |||
− | |||
− | <table cellpadding="5" width="80%"><tr valign="top"><th width="25%" style="border-bottom: 1px solid black;text-align:left;">Linux Command /<br>Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">ps</span></td><td> | ||
===Aliases / Command History=== | ===Aliases / Command History=== | ||
− | + | <br> | |
− | ''' | + | '''Aliases:''' |
− | + | <br><br> | |
− | + | An '''alias''' is a '''nickname''' to an existing command or group of commands.<br><br>An alias existing in '''system memory''' and will be '''lost''' when your current Linux session ends,<br>unless the alias is set in a '''start-up file''' (e.g. <span style="font-family:courier;font-weight:bold;">~/.bashrc</span>. You will learn about using start-up files <u>later</u> in this course.<br><br> | |
''Examples:'' | ''Examples:'' | ||
Line 142: | Line 141: | ||
'''Command History:''' | '''Command History:''' | ||
− | + | <br><br> | |
− | The filename | + | The filename <span style="font-family:courier;font-weight:bold;>~/.bash_history</span> stores recently executed command lines |
<br><br> | <br><br> | ||
''Examples of commands that use command history:'' | ''Examples of commands that use command history:'' | ||
− | <table width="80%"><tr valign="top"><td width="30%"><span style="font-family:courier;font-weight:bold;>up arrow</span> or <span style="font-family:courier;font-weight:bold;>down arrow</span> </td><td>move to '''previous''' command or '''next''' command within Bash shell prompt</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>fc -l</span></td><td> display last '''16''' commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>history | more</span></td><td>display all stored commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!num</span></td><td>'''re-execute''' an issued command number by command number (determined from ''history'' command)</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!xxx</span></td><td>'''re- | + | <table width="80%"><tr valign="top"><td width="30%"><span style="font-family:courier;font-weight:bold;>up arrow</span> or <span style="font-family:courier;font-weight:bold;>down arrow</span> </td><td>move to '''previous''' command or '''next''' command within Bash shell prompt</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>fc -l</span></td><td> display last '''16''' commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>history | more</span></td><td>display all stored commands</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!num</span></td><td>'''re-execute''' an issued command number by command number (determined from ''history'' command)</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!xxx</span></td><td>'''re-run''' a most recent previously-issued command beginning with string "xxx"</td></tr></table><br><br> |
=INVESTIGATION 1: LINKING FILES= | =INVESTIGATION 1: LINKING FILES= | ||
− | <br> | + | <span style="color:red;">'''ATTENTION''': This online tutorial will be required to be completed by '''Friday in week 9 by midnight''' to obtain a grade of '''2%''' towards this course</span><br><br> |
− | In this | + | |
+ | In this investigation, you will learn how to create '''hard links''' and '''symbolic links''' on your Matrix account,<br>and observe the <u>advantages</u> and <u>limitations</u> of using both types of links. | ||
Line 158: | Line 158: | ||
# '''Login''' to your matrix account.<br><br> | # '''Login''' to your matrix account.<br><br> | ||
− | # Issue a command to '''confirm''' you are located in your home directory.<br><br> | + | # Issue a Linux command to '''confirm''' you are located in your '''home''' directory.<br><br>'''NOTE:''' You will remain in your '''home''' directory to get practice using pathnames.<br><br> |
− | # Issue the following Linux command to create a directory called | + | # Issue the following Linux command to create a directory called '''~/links''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/links</span><br><br> |
− | # Issue the '''ls''' command to confirm that the directory '''~/links''' exists.<br><br> | + | # Issue the '''ls -ld''' command to confirm that the directory '''~/links''' exists.<br><br> |
− | # Use a text editor to create a file called | + | # Use a text editor to create a file called <span style="font-weight:bold;font-family:courier;">~/links/data-file.txt </span><br>(i.e. <u>without</u> changing to the links directory).<br><br> |
− | # Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line 1<br>This is line 2<br>This is line 3<br><br></span> | + | # Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line 1<br>This is line 2<br>This is line 3<br><br></span>[[Image:hard-link-inode1.png|thumb|right|450px|'''Hard links''' <u>share</u> the same '''i-node''' with regular files on a Unix / Linux filesystem.]] |
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
− | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt</span><br><br>View the '''i-node''' number for this file. What does this i-node number represent?<br><br>We will now create a '''hard link''' file | + | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt</span><br><br>View the '''i-node''' number for this file. What does this ''i-node'' number represent?<br><br>We will now create a '''hard link''' file to demonstrate how creating hard links are useful for '''back-ups'''.<br><br> |
# Issue the following Linux command to create the following '''hard link''' in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/links/data-file.hard.lnk</span><br><br> | # Issue the following Linux command to create the following '''hard link''' in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/links/data-file.hard.lnk</span><br><br> | ||
− | # Issue the following Linux command to display i-node ID numbers for <u>both</u> files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk</span><br><br>What do you notice about both of those file's i-node numbers?<br><br> | + | # Issue the following Linux command to display ''i-node'' ID numbers for <u>both</u> files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk</span><br><br>What do you notice about both of those file's ''i-node'' numbers?<br><br> |
− | # Use a text editor to edit <span style="font-weight:bold;font-family:courier;">~/links/data-file.txt</span><br>and add some lines to the bottom of that file.<br><br> | + | # Use a text editor to edit <span style="font-weight:bold;font-family:courier;">~/links/data-file.txt</span><br>and '''add some lines of text''' to the <u>bottom</u> of that file.<br><br> |
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
− | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links/data-file.hard.lnk</span><br><br>You should notice that the hard linked file also contains the additional line(s) that you added to the original file.<br>This is very useful for backing up your files <u>without</u> using the '''cp''' command!<br><br> | + | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links/data-file.hard.lnk</span><br><br>You should notice that the hard linked file also contains the additional line(s) that you added to the <u>original</u> file.<br>This is very useful for backing up your files <u>without</u> using the '''cp''' command!<br><br> |
− | # Use a text editor to edit the hard-linked file <span style="font-weight:bold;font-family:courier;">~/links/data-file | + | # Use a text editor to edit the hard-linked file <span style="font-weight:bold;font-family:courier;">~/links/data-file.hard.lnk</span><br>and add some lines to the bottom of this file.<br><br> |
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
− | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links/data-file. | + | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links/data-file.txt</span><br><br>What happened to this '''original''' file?</u> file?<br>What does this mean in terms of creating hard-linked files for back-ups?<br><br> |
# Issue the following Linux command to create a hard-linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/data-file.hard.lnk<br><br> | # Issue the following Linux command to create a hard-linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">ln ~/links/data-file.txt ~/data-file.hard.lnk<br><br> | ||
− | # Issue the following Linux command to compare all file's i-node numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk ~/data-file.hard.lnk</span><br><br>What do you notice about all of those file's ''i-node'' numbers?<br><br> | + | # Issue the following Linux command to compare all file's ''i-node'' numbers:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk ~/data-file.hard.lnk</span><br><br>What do you notice about all of those file's ''i-node'' numbers?<br><br> |
− | # Issue the following Linux command to check that you created those hard links: <br><span style="color:blue;font-weight:bold;font-family:courier;"> | + | # Issue the following Linux command to check that you created those hard links: <br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week8-check-1</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<br><br> |
# Issue the following Linux command to remove the '''~/links''' directory and its contents: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links</span><br><br> | # Issue the following Linux command to remove the '''~/links''' directory and its contents: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links</span><br><br> | ||
# Issue a Linux command to confirm that the '''~/links''' directory has been removed.<br><br> | # Issue a Linux command to confirm that the '''~/links''' directory has been removed.<br><br> | ||
− | # Issue the following Linux command to view the contents of your linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/data-file.hard.lnk</span><br><br>What do you notice? What does this tell you about hard links?<br><br> | + | # Issue the following Linux command to view the contents of your linked file in your '''home''' directory:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/data-file.hard.lnk</span><br><br>What do you notice? What does this tell you about hard links?<br><br>We will now learn how to create '''symbolic links'''.<br><br> |
− | # Issue the following Linux command to create a directory called '''~/links2''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/links2</span><br><br> | + | # Issue the following Linux command to create a directory called '''~/links2''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/links2</span><br><br>'''NOTE:''' You will remain in your '''home''' directory to get practice using pathnames.<br><br> |
− | # Issue the <span style="font-weight:bold;font-family:courier;">ls</span> command to confirm that the directory called '''~/links2''' exists.<br><br> | + | # Issue the <span style="font-weight:bold;font-family:courier;">ls -ld</span> command to confirm that the directory called '''~/links2''' exists.<br><br> |
− | # Use a text editor to create a file called | + | # Use a text editor to create a file called <span style="font-weight:bold;font-family:courier;">~/links2/text-file.txt</span><br>(i.e. <u>without</u> changing to the '''links2''' directory). <br><br>[[Image:symbolic-link-inode1.png|thumb|right|500px|'''Symbolic links''' are '''pointers''' (i.e. pathnames) to '''regular files''' and '''directories'''.<br>They do '''NOT''' share the same '''i-node'''.]] |
# Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line one<br>This is line two<br>This is line three<br><br></span> | # Enter the following text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is line one<br>This is line two<br>This is line three<br><br></span> | ||
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
# Issue the following Linux command to create the following '''symbolic''' link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br> | # Issue the following Linux command to create the following '''symbolic''' link in the same directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br> | ||
− | # Issue the following Linux command to display i-node numbers for <u>both</u> files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br>What do you notice about both of these file's i-node numbers?<br>What do you notice about the size of the file | + | # Issue the following Linux command to display ''i-node'' numbers for <u>both</u> files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk</span><br><br>What do you notice about both of these file's ''i-node'' numbers?<br>What do you notice about the size of the file <span style="font-weight:bold;font-family:courier;">~/links2/text-file.sym.lnk''</span>?<br>What '''pathname''' do you think this ''symbolic-linked'' file represents?<br><br> |
− | # Issue the following Linux command to create the following symbolic link in your '''home''' directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/text-file.sym.lnk</span><br><br> | + | # Issue the following Linux command to create the following '''symbolic link''' in your '''home''' directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~/links2/text-file.txt ~/text-file.sym.lnk</span><br><br> |
− | # Issue the following Linux command to display i-node numbers for all of those files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk ~/text-file.sym.lnk</span><br><br>What do you notice? What is the file size of <span style="font-weight:bold;font-family:courier;">~ | + | # Issue the following Linux command to display ''i-node'' numbers for all of those files:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk ~/text-file.sym.lnk</span><br><br>What do you notice about all of those file's i-node numbers?<br>What is the file size of <span style="font-weight:bold;font-family:courier;">~/text-file.sym.lnk</span>?<br>What '''pathname''' do you think this ''symbolic-linked'' file contains?<br><br> |
− | # Use a text editor to edit the '''symbolic''' link file called | + | # Use a text editor to edit the '''symbolic''' link file called <span style="font-weight:bold;font-family:courier;">~/links2/text-file.sym.lnk</span><br>and add some lines to the bottom of that file.<br><br> |
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
− | # Issue the following Linux command to view the contents of the '''original''' file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links2/text-file.txt</span><br><br>What did you notice? This happened because when you edited the | + | # Issue the following Linux command to view the contents of the '''original''' file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links2/text-file.txt</span><br><br>What did you notice? This happened because when you edited the symbolic-linked file,<br>you were redirected (via ''pathname'') to the <u>original</u> file.<br><br> |
− | # Use a text editor to edit the original file called | + | # Use a text editor to edit the '''original''' file called <span style="font-weight:bold;font-family:courier;">~/links2/text-file.txt</span><br>and add some lines to the bottom of that file.<br><br> |
# Save your editing session and exit your text editor.<br><br> | # Save your editing session and exit your text editor.<br><br> | ||
− | # Issue the following Linux command to view the contents of the '''symbolic''' linked file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links2/text-file.sym.lnk</span><br><br>What did you notice? Again, when you view the contents of the | + | # Issue the following Linux command to view the contents of the '''symbolic''' linked file:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/links2/text-file.sym.lnk</span><br><br>What did you notice? Again, when you view the contents of the symbolic-linked file,<br>you are redirected (via ''pathname'') to the <u>original</u> file.<br><br> |
− | # Issue the following Linux command to check that you created those symbolic links: <br><span style="color:blue;font-weight:bold;font-family:courier;"> | + | # Issue the following Linux command to check that you created those symbolic links: <br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week8-check-2</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<br><br> |
# Issue the following Linux command to remove the '''~/links2''' directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links2</span><br><br> | # Issue the following Linux command to remove the '''~/links2''' directory: <br><span style="color:blue;font-weight:bold;font-family:courier;">rm -rf ~/links2</span><br><br> | ||
− | # | + | # Issue a Linux command to confirm that the '''~/links2''' directory has been removed.<br><br> |
− | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file | + | # Issue the following Linux command to view the contents of the<br>'''original''' file called <span style="font-weight:bold;font-family:courier;">~/links2/text-file.txt:</span><br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/text-file.sym.lnk</span><br><br>What happened? Why did does this happen?<br><br>[[Image:symbolic-link-inode3.png|thumb|right|500px|Example of a '''broken link''' when a symbolic link points to a '''non-existent file'''.]] |
− | # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~ | + | # Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file.sym.lnk</span><br><br>This output indicates a "'''broken link'''" and indicates this is not an effective method of backing up files.<br><br> |
− | # Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;"> | + | # Issue a command to delete the <span style="font-weight:bold;font-family:courier;">~/text-file.sym.lnk</span> file which is a ''broken link''.<br><br> |
+ | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~jason.carman/example t8example</span><br><br>[[Image:symbolic-link-inode2.png|thumb|right|500px|'''Symbolic links''' can be used to point to '''directories''' as well as regular files. Symbolic links can also point to files on '''other''' Unix/Linux filesystems.]] | ||
+ | # Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">ls -ld t8example</span><br><br>What do you notice? Symbolic links are good for creating "short-cuts" to <u>both</u> '''regular files''' and '''directories'''. | ||
− | :In the next investigation, you will learn how to manage processes on your Matrix server.<br><br> | + | :In the next investigation, you will learn how to '''manage processes''' on your Matrix server.<br><br> |
=INVESTIGATION 2: MANAGING PROCESSES = | =INVESTIGATION 2: MANAGING PROCESSES = | ||
− | In this | + | In this investigation, you will learn how to '''manage processes''' on a Unix / Linux server. |
Line 212: | Line 214: | ||
# Make certain that you are logged into your Matrix account.<br><br> | # Make certain that you are logged into your Matrix account.<br><br> | ||
− | # Issue | + | # Issue a Linux command to confirm that you are located in your '''home''' directory.<br><br>The '''sleep''' command '''pauses for a specified number of seconds''' before returning to the shell prompt.<br>In this tutorial, we will be using this command to '''simulate''' the management of "long-running" processes.<br><br> |
− | # Notice that this process will run for '''700 seconds''', and is forcing the user to wait until this process finishes.<br>A process that is running in the terminal is referred to as a '''foreground | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br>Notice that this process will run for '''700 seconds''', and is forcing the user to '''wait''' until this process finishes.<br>A process that is '''running in the terminal''' is referred to as a '''foreground processes'''.<br><br>The Unix/Linux system is designed to allow users to send '''preemptive signals''' to manage those processes.<br><br> |
− | # Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold"> | + | # Press the following '''key combination''' to '''terminate''' the command running on the terminal: <span style="color:blue;font-family:courier;font-weight:bold">ctrl-c</span><br><br>You should notice that the process that was running in the foreground has been '''interrupted''' (i.e. terminated).<br>'''NOTE:''' The '''ctrl-c''' key combination sends '''SIGINT''' ('''Signal Interrupt''' - which is signal '''#2''')<br>to ''terminate'' a process that is running on the terminal (i.e. a '''foreground''' process).<br><br> |
# Reissue the Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br> | # Reissue the Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br> | ||
− | # Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold"> | + | # Press the '''key combination''': <span style="color:blue;font-family:courier;font-weight:bold">ctrl-z</span><br><br>[[Image:process-jobs1.png|thumb|right|300px|Running a command in the terminal, pressing '''ctrl-z''' to place into the background, and issuing the '''jobs''' command to view processes in the background.]] |
− | # You should now see output similar to what is displayed below:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>'''NOTE:''' This indicates that this process has been placed into the '''background'''.<br>This is useful in order to "'''free-up'''" the terminal to run other commands | + | # You should now see output similar to what is displayed below:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>'''NOTE:''' This indicates that this process has been placed into the '''background'''.<br>This is useful in order to "'''free-up'''" the terminal to run other Linux commands.<br><br> |
− | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>This display indicates that this process (that is now in the background) has '''stopped'''.<br>In other words, the ''sleep'' command is NOT counting-down to zero to terminate.<br><br>'''NOTE:''' You need to use the '''bg''' command to '''run''' that process that was sent into the '''background'''.<br><br>[[Image:process-jobs2.png|thumb|right|300px|Using the '''bg''' command to '''run recent process''' that was placed into background from using '''ctrl-z''' keys.]] | |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ Stopped sleep 700</span><br><br>This display indicates that this process (that is now in the background) has '''stopped'''.<br>In other words, the ''sleep'' command is NOT counting-down to zero to terminate.<br><br>'''NOTE:''' You need to use the '''bg''' command to run that process that was sent into the background.<br><br> | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">bg</span><br><br>'''NOTE:''' You can use the bg command WITHOUT arguments to run recent in the background. From the '''jobs''' command, the process that has a plus sign "+" indicates the most recent process placed into the background.<br><br> |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">bg</span><br><br>'''NOTE:''' You can use the bg command WITHOUT arguments to | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ sleep 700 &</span><br><br>This display indicates that this process in the background is '''running in the background'''<br>(indicated by the ampersand character "'''&'''"). Now this command has resume pausing until '''700 seconds'''.<br><br>[[Image:process-jobs3.png|thumb|right|300px|Using the '''ampersand''' character '''&''' to run a '''series of processes''' in the background.]] |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">jobs</span><br><br>You should see the following output similar that was displayed above:<br><span style="font-family:courier;font-weight:bold">[1]+ sleep 700 &</span><br><br>This display indicates that this process in the background is '''running in the background'''<br>( | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">fg</span><br><br>You should notice that the ''sleep'' command is now running in the '''foreground'''.<br><br> |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">fg</span><br><br>You should notice that the ''sleep'' command is now running in the '''foreground'''.<br><br> | + | # Press the '''key combination''' to '''terminate''' the process running in the '''foreground''':<br><span style="color:blue;font-family:courier;font-weight:bold">ctrl-c</span> <br><br>You can issue Linux commands with ampersand "'''&'''" in your terminal to '''run''' processes automatically in the '''background''' <u>without</u> having to issue ''ctrl-z'' and ''bg'' short-cut keys.<br><br> |
− | + | # Issue the following Linux commands:<br><span style="color:blue;font-family:courier;font-weight:bold">sleep 500 & sleep 600 & sleep 700 &</span><br><br> | |
− | # Issue the following Linux | + | # Issue the '''jobs''' command. What do you notice?<br><br>In the jobs command output, jobs that display a plus sign ('''+''') indicates the '''most recent''' process<br>placed in to the background, and a minus sign ('''-''') indicates the '''second most recent''' process<br>placed into the background.<br><br>The '''kill''' command issued to terminate processes that are running in the '''foreground''' or '''background'''.<br>Issuing the kill command <u>without</u> options would send the '''SIGTERM''' signal (eg. ''signal terminate'' - which is signal '''#15''').<br><br>[[Image:process-jobs4.png|thumb|right|300px|Using the '''kill %1''' command to '''terminate''' job #1.]] |
− | # Issue the '''jobs''' command. What do you notice?<br><br> | + | # Issue the following Linux command to '''terminate''' the '''first''' job running in the background:<br><span style="color:blue;font-family:courier;font-weight:bold">kill %1</span><br><br>'''NOTE:''' You can specify job number preceded by percent % with the<br>'''kill''', '''bg''', and '''fg''' commands to specify the processes' job number.<br><br> |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">kill %1</span><br><br>'''NOTE:''' You can specify job number preceded by percent % with the<br>'''kill''', '''bg''', and '''fg''' commands to specify the processes' job number.<br><br> | ||
# Issue the '''jobs''' command. What do you notice?<br><br> | # Issue the '''jobs''' command. What do you notice?<br><br> | ||
# Issue the following Linux commands:<br><span style="color:blue;font-family:courier;font-weight:bold">kill %2</span><br><span style="color:blue;font-family:courier;font-weight:bold">kill %3</span><br><br> | # Issue the following Linux commands:<br><span style="color:blue;font-family:courier;font-weight:bold">kill %2</span><br><span style="color:blue;font-family:courier;font-weight:bold">kill %3</span><br><br> | ||
− | # Issue the '''jobs''' command. What do you notice?<br><br> | + | # Issue the '''jobs''' command (you may have to issue the ''jobs'' command several times to get final result).<br> What do you notice?<br><br>[[Image:process-jobs5.png|thumb|right|300px|Using round brackets to '''group''' a series of commands to be run as '''one process'''.]] |
# Let's use '''grouping''' to run several commands in sequence within a single process.<br><br> | # Let's use '''grouping''' to run several commands in sequence within a single process.<br><br> | ||
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">(sleep 400; sleep 500; sleep 600) &</span><br><br> | + | # Issue the following Linux command:<br><span style="color:blue;font-family:courier;font-weight:bold">(sleep 400; sleep 500; sleep 600) &</span><br><br> |
− | # Issue the '''jobs''' command. What do you notice?<br><br> | + | # Issue the '''jobs''' command. What do you notice?<br>You should notice all commands are run in a group as just one process.<br><br> |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold"> | + | # Issue the following Linux command to terminate the first job running in the '''background''':<br><span style="color:blue;font-family:courier;font-weight:bold">kill %1</span><br><br>NOTE: If issuing the kill command does not work, then you would need to send a STRONGER signal<br>to "'''kill'''" (not "''SIGTERM'' - which is signal ''#15''") the process. The '''SIGKILL''' signal (signal '''#9''')<br>would be required to do this by issuing the '''kill''' command with the option: '''-9'''.<br><br> |
− | # Issue the | + | # Issue the '''jobs''' command and make certain there are no processes that are running in the '''background'''.<br><br>You can also manipulate processes by their '''PID''' ('''process ID'''). Let's terminate our Matrix Bash shell process<br>by using the '''kill''' command using that processes' '''PID'''.<br><br> |
− | # | + | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">ps</span><br><br> |
− | # Issue the following Linux command (using the PID number instead of "PID"):<br><span style="color:blue;font-family:courier;font-weight:bold">kill | + | #Note in the '''ps''' command output the PID of the process called '''bash'''.<br><br>You will be using that PID when issuing the <u>next</u> Linux command.<br><br> |
− | + | # Issue the following Linux command (using the bash processes' PID number instead of "PID"):<br><span style="color:blue;font-family:courier;font-weight:bold">kill PID</span><br><br>What did you notice?<br><br>'''FYI:''' If the command did NOT work, issue the following Linux command (using the bash processes' PID number instead of "PID"):<br><span style="color:blue;font-family:courier;font-weight:bold">kill -9 PID</span><br><br> | |
− | :In the next investigation, you will learn how to create aliases and view command history on your Matrix server.<br><br> | + | |
+ | :In the next investigation, you will learn how to '''create aliases''' and '''view command history''' on your Matrix server.<br><br> | ||
=INVESTIGATION 3: ALIASES / COMMAND HISTORY= | =INVESTIGATION 3: ALIASES / COMMAND HISTORY= | ||
<br> | <br> | ||
− | In this | + | In this investigation, you will learn how to '''manage aliases''' and '''Linux command history''' on your Matrix account. |
'''Perform the Following Steps:''' | '''Perform the Following Steps:''' | ||
− | # Make certain that you are logged into your Matrix account.<br><br> | + | # Make certain that you are logged into your Matrix account.<br><br>[[Image:alias-1.png|thumb|right|400px|Issuing the '''alias''' command (<u>without</u> arguments) will display a list of existing aliases on your Unix / Linux system.]] |
− | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">alias</span><br><br> | + | # Issue a Linux command to confirm that you are located in your '''home''' directory.<br><br> |
+ | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">alias | more</span><br><br>Observe those <u>existing</u> aliases that have previously been declared. Take a few moments to run those aliases to see what happens.<br><br> | ||
# Issue the following to create an alias: <span style="color:blue;font-family:courier;font-weight:bold">alias lal='ls -al'</span><br><br> | # Issue the following to create an alias: <span style="color:blue;font-family:courier;font-weight:bold">alias lal='ls -al'</span><br><br> | ||
# Issue the following alias: <span style="color:blue;font-family:courier;font-weight:bold">lal</span><br><br>What do you notice?<br><br> | # Issue the following alias: <span style="color:blue;font-family:courier;font-weight:bold">lal</span><br><br>What do you notice?<br><br> | ||
Line 255: | Line 258: | ||
# Issue the following command to confirm that this newly-created alias is stored in memory:<br><br><span style="color:blue;font-family:courier;font-weight:bold">alias | grep "lh"</span><br><br> | # Issue the following command to confirm that this newly-created alias is stored in memory:<br><br><span style="color:blue;font-family:courier;font-weight:bold">alias | grep "lh"</span><br><br> | ||
# Issue the following alias: <span style="color:blue;font-family:courier;font-weight:bold">lh</span> <br><br>What do you think this command does?<br><br> | # Issue the following alias: <span style="color:blue;font-family:courier;font-weight:bold">lh</span> <br><br>What do you think this command does?<br><br> | ||
− | # Logout of your Matrix account | + | # '''Logout''' of your Matrix account and then '''login''' to your Matrix account.<br><br> |
+ | # Reissue the '''lal''' alias. What happened?<br><br> | ||
# Reissue the '''lh''' alias. What happened?<br><br> | # Reissue the '''lh''' alias. What happened?<br><br> | ||
− | # Issue the '''alias''' command without any arguments to see if it is stored in memory.<br><br> | + | # Issue the '''alias | grep lh''' command without any arguments to see if it is stored in memory.<br><br> |
− | # Reissue the command to create the '''lh''' alias in '''step # | + | # Reissue the command to create the '''lh''' alias in '''step #6'''.<br><br> |
# Run the '''lh''' alias to confirm that it is properly set in memory.<br><br> | # Run the '''lh''' alias to confirm that it is properly set in memory.<br><br> | ||
# Issue the following Linux command to edit your ~/.bashrc startup file:<br><span style="color:blue;font-family:courier;font-weight:bold">nano ~/.bashrc</span><br><br> | # Issue the following Linux command to edit your ~/.bashrc startup file:<br><span style="color:blue;font-family:courier;font-weight:bold">nano ~/.bashrc</span><br><br> | ||
− | # Add the following line at the bottom of this file:<br><span style="font-family:courier;font-weight:bold;>alias lh='ls --human-readable --size -1 -S --classify'</span><br><br> | + | # Add the following line at the '''bottom''' of this file:<br><span style="font-family:courier;font-weight:bold;>alias lh='ls --human-readable --size -1 -S --classify'</span><br><br> |
# Save your editing changes and exit your text editor.<br><br> | # Save your editing changes and exit your text editor.<br><br> | ||
− | # Logout of your Matrix account, | + | # '''Logout''' of your Matrix account, then '''login''' to your Matrix account.<br><br> |
# Reissue the '''lh''' alias. What happened?<br><br> | # Reissue the '''lh''' alias. What happened?<br><br> | ||
# Issue the following Linux command: <span style="font-family:courier;font-weight:bold;>unalias lh</span><br><br> | # Issue the following Linux command: <span style="font-family:courier;font-weight:bold;>unalias lh</span><br><br> | ||
# Run the '''lh''' alias to see what happens.<br><br>What happenned?<br><br> | # Run the '''lh''' alias to see what happens.<br><br>What happenned?<br><br> | ||
− | # Logout of your Matrix account, | + | # '''Logout''' of your Matrix account, then '''login''' to your Matrix account.<br><br> |
# Reissue the '''lh''' alias. What happened? Why?<br><br> | # Reissue the '''lh''' alias. What happened? Why?<br><br> | ||
− | # Issue the following Linux command | + | # Reissue the '''lal''' alias. Why '''didn't''' this alias work?<br><br>The checking script below is designed to act as a '''filter''' with a '''pipeline command'''.<br>This will allow to check if your '''lh''' alias exists when it is checked in this program.<br><br> |
+ | # Issue the following Linux pipeline command:<br><span style="color:blue;font-weight:bold;font-family:courier;">alias | ~uli101/week8-check-3</span><br><br>If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.<br>If you receive a congratulation message that there are no errors, then proceed with this tutorial.<br><br>We will complete this investigation by learning to execute p'''reviously issued commands''' by using '''command history'''.<br><br> | ||
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep "lh"</span><br><br>What do you notice?<br><br> | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep "lh"</span><br><br>What do you notice?<br><br> | ||
− | # | + | # Type an exclamation mark '''!''' followed by the number by one of those commands<br>listed in the '''history list''' and press <span style="color:blue;font-family:courier;font-weight:bold">ENTER</span><br><br>What happened?<br><br> |
# Type the following: <span style="color:blue;font-family:courier;font-weight:bold">!unalias</span> and press <span style="color:blue;font-family:courier;font-weight:bold">ENTER</span><br><br>What happened?<br><br> | # Type the following: <span style="color:blue;font-family:courier;font-weight:bold">!unalias</span> and press <span style="color:blue;font-family:courier;font-weight:bold">ENTER</span><br><br>What happened?<br><br> | ||
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep "lh"</span><br><br>What happened?<br><br> | # Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep "lh"</span><br><br>What happened?<br><br> | ||
− | |||
= LINUX PRACTICE QUESTIONS = | = LINUX PRACTICE QUESTIONS = | ||
Line 283: | Line 287: | ||
simulate a quiz: | simulate a quiz: | ||
− | https:// | + | https://wiki.cdot.senecacollege.ca/uli101/files/uli101_week8_practice.docx |
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc). | Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc). | ||
Line 298: | Line 302: | ||
# Aliases / History:<ol type="a"><li>Write a linux command to create an '''alias''' called '''ld''' that issues the command: '''ls -ld'''</li><li>Write a linux command to unset the '''alias''' created in the previous question.</li><li>Issue a Linux command to list '''history''' of commands that match the pattern called '''touch'''.</li></ol><br> | # Aliases / History:<ol type="a"><li>Write a linux command to create an '''alias''' called '''ld''' that issues the command: '''ls -ld'''</li><li>Write a linux command to unset the '''alias''' created in the previous question.</li><li>Issue a Linux command to list '''history''' of commands that match the pattern called '''touch'''.</li></ol><br> | ||
# Create a '''table''' listing each Linux command, useful options and command purpose for the following Linux commands:<br>'''ln''' , '''ps''' , '''top''' , '''fg''' , '''bg''' , '''jobs''' , '''kill''' , '''alias''' , '''unalias''' , '''history''' | # Create a '''table''' listing each Linux command, useful options and command purpose for the following Linux commands:<br>'''ln''' , '''ps''' , '''top''' , '''fg''' , '''bg''' , '''jobs''' , '''kill''' , '''alias''' , '''unalias''' , '''history''' | ||
+ | |||
+ | |||
+ | _________________________________________________________________________________ | ||
+ | |||
+ | Author: Murray Saul | ||
+ | |||
+ | License: LGPL version 3 | ||
+ | Link: https://www.gnu.org/licenses/lgpl.html | ||
+ | |||
+ | _________________________________________________________________________________ | ||
+ | |||
[[Category:ULI101]] | [[Category:ULI101]] |
Latest revision as of 19:35, 4 September 2023
Contents
LINKING FILES / MANAGING PROCESSES
Main Objectives of this Practice Tutorial
- Define the term i-node as it relates to the Unix/Linux File System
- Issue the ls -i command to view i-node (index) numbers associated with Unix/Linux files
- Define the terms Hard and Symbolic Links
- Issue the ln command to create hard and symbolic links
- Define term process as it relates to the Unix/ Linux operating system
- Run and terminate processes in the foreground and background
- Display and manipulate background and foreground processes
- Use alias and history commands in Unix/Linux
Tutorial Reference Material
Course Notes |
Concepts / Commands |
YouTube Videos | ||
Slides:
|
Links:
Managing Processes: |
Linux Commands: | Brauer Instructional Videos: |
KEY CONCEPTS
i-node (index) ID Number of a File
An i-node is a database containing information (e.g. file type, owner, permissions, etc.) for all files that are created on the Unix/Linux filesystem.
The i-node number is like a finger-print, and is considered to be unique for each file on the Unix / Linux file system.
Referring to the diagram on the far right, issuing the ls command with the -i option displays the i-node number for each file. You can see that each file (whether it is a directory or regular file) has its own unique
i-node number.
Hard Links
A Hard link is a reference to the physical data on a file system.
It does this by creating a file that shares the same i-node number with the original file.
Advantages: If only one hard link remains (even if original file has been removed), the data in that hard linked file is NOT lost. The data in hard linked files are automatically updated when original file are updated.
Disadvantages: Hard links take-up extra space, you cannot hard link directories,
and you cannot hard link files from other Unix/Linux servers (since the inode number may already be used by the other Unix/Linux server).
Examples:
ln myfile.txt myfile1.hard.lnk
ln myfile.txt ~/backups/myfile.hard.lnk
Symbolic Links
A Symbolic Link is an indirect pointer to a file and are also known as soft link or symlink. The symbolic link file contains the pathname to the original file.
Advantages: symbolic links are shortcuts to other files, where the symbolic link only contains the pathname to the original file, you can create symbolic links
on different Unix/Linux servers, and that you can create symbolic links for directories.
Disadvantages: Symbolic links are NOT good for backup purposes
since a symbolic link can point to a nonexistent file (referred to as a "broken link").
Examples:
ln -s otherfile.txt otherfile1.sym.lnk
ln -s otherfile.txt ~/backups/otherfile.sym.lnk
Managing Processes
All commands/programs (tasks) that are running on a Unix/Linux computer system are referred to as processes.
Characteristics of Processes:
- Each process has an owner
- Each process has a unique ID (PID)
- Processes keep their PID for their entire life.
- Usually a parent sleeps (suspends) when a child is running (the exception is when the child process is running in the background)
- UNIX / Linux processes are hierarchical. The process structure can have child processes, great grandchild processes, etc.
Users can manage processes to become more productive while working in the Unix / Linux Command-line environment.
Processes that run in the terminal are known as foreground processes. You can run or send processes currently running
in the foreground to the background to free-up your terminal (e.g. issue other Linux commands).
Below are a listing of common Linux commands and keyboard shortcuts to manage foreground and background processes:
Linux Command / Key Combination | Purpose |
---|---|
ps | Displays snapshot information about processes. Examples: ps , ps -l , ps -ef , ps -u , ps aux |
top | The top command provides a realtime status of running processes. NOTE: You can press ctrl-c to exit |
ctrl-c | Terminates a process running in the foreground |
ctrl-z | Sends a process running in the foreground into the background. |
fg | Moves a background job from the current environment into the foreground. Example: fg %job-number |
bg | Runs (starts) the most recent process that was placed into the background. Example: bg %job-number |
jobs | The jobs utility displays the status of jobs that were started in the current shell environment. Example: jobs [1]+ Stopped vim a <-- Job #1 (+ most recent process / background) [2] Running sleep 200 & <-- Job #2 [3] Running sleep 300 & <-- Job #3 [4]- Running sleep 400 & <-- Job #4 (- second recent process / background) |
kill | The kill command sends the specified signal to the specified processes or process groups. If no signal is specified, the TERM signal is sent. The default action for this signal is to terminate the process. Examples: kill PID , kill -9 PID , kill %job-number , kill -9 %job-number |
Aliases / Command History
Aliases:
An alias is a nickname to an existing command or group of commands.
An alias existing in system memory and will be lost when your current Linux session ends,
unless the alias is set in a start-up file (e.g. ~/.bashrc. You will learn about using start-up files later in this course.
Examples:
alias (Alias command without an argument will display all the aliases currently set)
alias dir=ls
alias ls='ls -al'
alias clearfile='cat /dev/null >'
unalias alias-name (removes alias from memory)
Command History:
The filename ~/.bash_history stores recently executed command lines
Examples of commands that use command history:
up arrow or down arrow | move to previous command or next command within Bash shell prompt |
fc -l | display last 16 commands |
history | more | display all stored commands |
!num | re-execute an issued command number by command number (determined from history command) |
!xxx | re-run a most recent previously-issued command beginning with string "xxx" |
INVESTIGATION 1: LINKING FILES
ATTENTION: This online tutorial will be required to be completed by Friday in week 9 by midnight to obtain a grade of 2% towards this course
In this investigation, you will learn how to create hard links and symbolic links on your Matrix account,
and observe the advantages and limitations of using both types of links.
Perform the Following Steps:
- Login to your matrix account.
- Issue a Linux command to confirm you are located in your home directory.
NOTE: You will remain in your home directory to get practice using pathnames. - Issue the following Linux command to create a directory called ~/links:
mkdir ~/links - Issue the ls -ld command to confirm that the directory ~/links exists.
- Use a text editor to create a file called ~/links/data-file.txt
(i.e. without changing to the links directory). - Enter the following text displayed below:
This is line 1
This is line 2
This is line 3 - Save your editing session and exit your text editor.
- Issue the following Linux command:
ls -li ~/links/data-file.txt
View the i-node number for this file. What does this i-node number represent?
We will now create a hard link file to demonstrate how creating hard links are useful for back-ups. - Issue the following Linux command to create the following hard link in the same directory:
ln ~/links/data-file.txt ~/links/data-file.hard.lnk - Issue the following Linux command to display i-node ID numbers for both files:
ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk
What do you notice about both of those file's i-node numbers? - Use a text editor to edit ~/links/data-file.txt
and add some lines of text to the bottom of that file. - Save your editing session and exit your text editor.
- Issue the following Linux command:
cat ~/links/data-file.hard.lnk
You should notice that the hard linked file also contains the additional line(s) that you added to the original file.
This is very useful for backing up your files without using the cp command! - Use a text editor to edit the hard-linked file ~/links/data-file.hard.lnk
and add some lines to the bottom of this file. - Save your editing session and exit your text editor.
- Issue the following Linux command:
cat ~/links/data-file.txt
What happened to this original file?</u> file?
What does this mean in terms of creating hard-linked files for back-ups? - Issue the following Linux command to create a hard-linked file in your home directory:
ln ~/links/data-file.txt ~/data-file.hard.lnk - Issue the following Linux command to compare all file's i-node numbers:
ls -li ~/links/data-file.txt ~/links/data-file.hard.lnk ~/data-file.hard.lnk
What do you notice about all of those file's i-node numbers? - Issue the following Linux command to check that you created those hard links:
~uli101/week8-check-1
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 with this tutorial. - Issue the following Linux command to remove the ~/links directory and its contents:
rm -rf ~/links - Issue a Linux command to confirm that the ~/links directory has been removed.
- Issue the following Linux command to view the contents of your linked file in your home directory:
cat ~/data-file.hard.lnk
What do you notice? What does this tell you about hard links?
We will now learn how to create symbolic links. - Issue the following Linux command to create a directory called ~/links2:
mkdir ~/links2
NOTE: You will remain in your home directory to get practice using pathnames. - Issue the ls -ld command to confirm that the directory called ~/links2 exists.
- Use a text editor to create a file called ~/links2/text-file.txt
(i.e. without changing to the links2 directory). - Enter the following text displayed below:
This is line one
This is line two
This is line three - Save your editing session and exit your text editor.
- Issue the following Linux command to create the following symbolic link in the same directory:
ln -s ~/links2/text-file.txt ~/links2/text-file.sym.lnk - Issue the following Linux command to display i-node numbers for both files:
ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk
What do you notice about both of these file's i-node numbers?
What do you notice about the size of the file ~/links2/text-file.sym.lnk?
What pathname do you think this symbolic-linked file represents? - Issue the following Linux command to create the following symbolic link in your home directory:
ln -s ~/links2/text-file.txt ~/text-file.sym.lnk - Issue the following Linux command to display i-node numbers for all of those files:
ls -li ~/links2/text-file.txt ~/links2/text-file.sym.lnk ~/text-file.sym.lnk
What do you notice about all of those file's i-node numbers?
What is the file size of ~/text-file.sym.lnk?
What pathname do you think this symbolic-linked file contains? - Use a text editor to edit the symbolic link file called ~/links2/text-file.sym.lnk
and add some lines to the bottom of that file. - Save your editing session and exit your text editor.
- Issue the following Linux command to view the contents of the original file:
cat ~/links2/text-file.txt
What did you notice? This happened because when you edited the symbolic-linked file,
you were redirected (via pathname) to the original file. - Use a text editor to edit the original file called ~/links2/text-file.txt
and add some lines to the bottom of that file. - Save your editing session and exit your text editor.
- Issue the following Linux command to view the contents of the symbolic linked file:
cat ~/links2/text-file.sym.lnk
What did you notice? Again, when you view the contents of the symbolic-linked file,
you are redirected (via pathname) to the original file. - Issue the following Linux command to check that you created those symbolic links:
~uli101/week8-check-2
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 with this tutorial. - Issue the following Linux command to remove the ~/links2 directory:
rm -rf ~/links2 - Issue a Linux command to confirm that the ~/links2 directory has been removed.
- Issue the following Linux command to view the contents of the
original file called ~/links2/text-file.txt:
cat ~/text-file.sym.lnk
What happened? Why did does this happen? - Issue the following Linux command:
ls -l ~/text-file.sym.lnk
This output indicates a "broken link" and indicates this is not an effective method of backing up files. - Issue a command to delete the ~/text-file.sym.lnk file which is a broken link.
- Issue the following Linux command:
ln -s ~jason.carman/example t8example - Issue the following Linux command:
ls -ld t8example
What do you notice? Symbolic links are good for creating "short-cuts" to both regular files and directories.
- In the next investigation, you will learn how to manage processes on your Matrix server.
INVESTIGATION 2: MANAGING PROCESSES
In this investigation, you will learn how to manage processes on a Unix / Linux server.
Perform the Following Steps:
- Make certain that you are logged into your Matrix account.
- Issue a Linux command to confirm that you are located in your home directory.
The sleep command pauses for a specified number of seconds before returning to the shell prompt.
In this tutorial, we will be using this command to simulate the management of "long-running" processes. - Issue the following Linux command: sleep 700
Notice that this process will run for 700 seconds, and is forcing the user to wait until this process finishes.
A process that is running in the terminal is referred to as a foreground processes.
The Unix/Linux system is designed to allow users to send preemptive signals to manage those processes. - Press the following key combination to terminate the command running on the terminal: ctrl-c
You should notice that the process that was running in the foreground has been interrupted (i.e. terminated).
NOTE: The ctrl-c key combination sends SIGINT (Signal Interrupt - which is signal #2)
to terminate a process that is running on the terminal (i.e. a foreground process). - Reissue the Linux command: sleep 700
- Press the key combination: ctrl-z
- You should now see output similar to what is displayed below:
[1]+ Stopped sleep 700
NOTE: This indicates that this process has been placed into the background.
This is useful in order to "free-up" the terminal to run other Linux commands. - Issue the following Linux command: jobs
You should see the following output similar that was displayed above:
[1]+ Stopped sleep 700
This display indicates that this process (that is now in the background) has stopped.
In other words, the sleep command is NOT counting-down to zero to terminate.
NOTE: You need to use the bg command to run that process that was sent into the background. - Issue the following Linux command: bg
NOTE: You can use the bg command WITHOUT arguments to run recent in the background. From the jobs command, the process that has a plus sign "+" indicates the most recent process placed into the background. - Issue the following Linux command: jobs
You should see the following output similar that was displayed above:
[1]+ sleep 700 &
This display indicates that this process in the background is running in the background
(indicated by the ampersand character "&"). Now this command has resume pausing until 700 seconds. - Issue the following Linux command: fg
You should notice that the sleep command is now running in the foreground.
- Press the key combination to terminate the process running in the foreground:
ctrl-c
You can issue Linux commands with ampersand "&" in your terminal to run processes automatically in the background without having to issue ctrl-z and bg short-cut keys.
- Issue the following Linux commands:
sleep 500 & sleep 600 & sleep 700 & - Issue the jobs command. What do you notice?
In the jobs command output, jobs that display a plus sign (+) indicates the most recent process
placed in to the background, and a minus sign (-) indicates the second most recent process
placed into the background.
The kill command issued to terminate processes that are running in the foreground or background.
Issuing the kill command without options would send the SIGTERM signal (eg. signal terminate - which is signal #15). - Issue the following Linux command to terminate the first job running in the background:
kill %1
NOTE: You can specify job number preceded by percent % with the
kill, bg, and fg commands to specify the processes' job number. - Issue the jobs command. What do you notice?
- Issue the following Linux commands:
kill %2
kill %3 - Issue the jobs command (you may have to issue the jobs command several times to get final result).
What do you notice? - Let's use grouping to run several commands in sequence within a single process.
- Issue the following Linux command:
(sleep 400; sleep 500; sleep 600) & - Issue the jobs command. What do you notice?
You should notice all commands are run in a group as just one process. - Issue the following Linux command to terminate the first job running in the background:
kill %1
NOTE: If issuing the kill command does not work, then you would need to send a STRONGER signal
to "kill" (not "SIGTERM - which is signal #15") the process. The SIGKILL signal (signal #9)
would be required to do this by issuing the kill command with the option: -9. - Issue the jobs command and make certain there are no processes that are running in the background.
You can also manipulate processes by their PID (process ID). Let's terminate our Matrix Bash shell process
by using the kill command using that processes' PID. - Issue the following Linux command: ps
- Note in the ps command output the PID of the process called bash.
You will be using that PID when issuing the next Linux command. - Issue the following Linux command (using the bash processes' PID number instead of "PID"):
kill PID
What did you notice?
FYI: If the command did NOT work, issue the following Linux command (using the bash processes' PID number instead of "PID"):
kill -9 PID
- In the next investigation, you will learn how to create aliases and view command history on your Matrix server.
INVESTIGATION 3: ALIASES / COMMAND HISTORY
In this investigation, you will learn how to manage aliases and Linux command history on your Matrix account.
Perform the Following Steps:
- Make certain that you are logged into your Matrix account.
- Issue a Linux command to confirm that you are located in your home directory.
- Issue the following Linux command: alias | more
Observe those existing aliases that have previously been declared. Take a few moments to run those aliases to see what happens. - Issue the following to create an alias: alias lal='ls -al'
- Issue the following alias: lal
What do you notice? - Issue the following to create another alias (lowercase l and h):
alias lh='ls --human-readable --size -1 -S --classify' - Issue the following command to confirm that this newly-created alias is stored in memory:
alias | grep "lh" - Issue the following alias: lh
What do you think this command does? - Logout of your Matrix account and then login to your Matrix account.
- Reissue the lal alias. What happened?
- Reissue the lh alias. What happened?
- Issue the alias | grep lh command without any arguments to see if it is stored in memory.
- Reissue the command to create the lh alias in step #6.
- Run the lh alias to confirm that it is properly set in memory.
- Issue the following Linux command to edit your ~/.bashrc startup file:
nano ~/.bashrc - Add the following line at the bottom of this file:
alias lh='ls --human-readable --size -1 -S --classify' - Save your editing changes and exit your text editor.
- Logout of your Matrix account, then login to your Matrix account.
- Reissue the lh alias. What happened?
- Issue the following Linux command: unalias lh
- Run the lh alias to see what happens.
What happenned? - Logout of your Matrix account, then login to your Matrix account.
- Reissue the lh alias. What happened? Why?
- Reissue the lal alias. Why didn't this alias work?
The checking script below is designed to act as a filter with a pipeline command.
This will allow to check if your lh alias exists when it is checked in this program. - Issue the following Linux pipeline command:
alias | ~uli101/week8-check-3
If you encounter errors, then view the feedback to make corrections, and then re-run the checking script.
If you receive a congratulation message that there are no errors, then proceed with this tutorial.
We will complete this investigation by learning to execute previously issued commands by using command history. - Issue the following Linux command: history | grep "lh"
What do you notice? - Type an exclamation mark ! followed by the number by one of those commands
listed in the history list and press ENTER
What happened? - Type the following: !unalias and press ENTER
What happened? - Issue the following Linux command: history | grep "lh"
What happened?
LINUX PRACTICE QUESTIONS
The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.
Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:
https://wiki.cdot.senecacollege.ca/uli101/files/uli101_week8_practice.docx
Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).
Review Questions:
- Hard Links:
- What is the purpose of creating a hard-link?
- What is a limitation of a hard link?
- Write a single Linux command to create a hard link called ~/backup/myfile.txt.lnk for the existing file called ~/myfile.txt
- Write a single Linux command to display the i-node number for both files. Are the i-node numbers identical?
- Symbolic (Soft) Links:
- What is the purpose of creating a symbolic (soft) link?
- What is a limitation of a symbolic (soft) link?
- Write a single Linux command to create a symbolic link called ~/shortcuts/murray.saul.lnk
to the existing directory called ~murray.saul - Are the i-node numbers identical for both of those files?
- What data is contained in the file called ~/shortcuts/murray.saul.lnk?
- Background / Foreground Processes:
- Write a single Linux command to run the program called ~/clean.sh in the background.
- Write a single Linux command to place the previously issued program in the foreground.
- Write a single Linux command to confirm that this program is running in the background.
- What key-combination would you issue to send that program again into the background?
- Write a single Linux command to have that process sent into the background to continue running?
- Managing Background processes:
Use the following diagram to answer the accompanying questions.
Each of the following questions will use the diagram below and are treated as independent situations.
[1] Stopped vim a
[2]- Stopped vim b
[3]+ Stopped vim c- Write a single Linux command to bring the second-recently process placed in the background into the foreground.
- Write a single Linux command to terminate job #3.
- Write a single Linux command to display running processes in “real-time”.
- Write a single Linux command to terminate a process that has the following PID: 22384
- Aliases / History:
- Write a linux command to create an alias called ld that issues the command: ls -ld
- Write a linux command to unset the alias created in the previous question.
- Issue a Linux command to list history of commands that match the pattern called touch.
- Create a table listing each Linux command, useful options and command purpose for the following Linux commands:
ln , ps , top , fg , bg , jobs , kill , alias , unalias , history
_________________________________________________________________________________
Author: Murray Saul
License: LGPL version 3 Link: https://www.gnu.org/licenses/lgpl.html
_________________________________________________________________________________