Difference between revisions of "Tutorial8: Links / Process Management"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 3: ALIASES / COMMAND HISTORY)
(INVESTIGATION 3: ALIASES / COMMAND HISTORY)
(48 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
:* Understand the purpose and why links are used in Unix / Linux
 
:* Understand the purpose and why links are used in Unix / Linux
  
:* Define the term '''inode''' number as it relates to a file on Unix / Linux
+
:* Define the term '''inode''' as it relates to a file on Unix / Linux
  
 
:* Define the terms: '''Hard''' Link and '''Symbolic''' Link
 
:* Define the terms: '''Hard''' Link and '''Symbolic''' Link
  
:* Issue the '''ln''' command to create hard and symbolic links
+
:* Issue the '''ln''' command to create '''hard''' and '''symbolic''' links
  
 
:* Define and understand the purpose of a '''process''' in Unix / Linux
 
:* Define and understand the purpose of a '''process''' in Unix / Linux
Line 16: Line 16:
  
 
:* '''Display''' and '''manipulate''' background and foreground processes
 
:* '''Display''' and '''manipulate''' background and foreground processes
 +
 +
:* Manage '''aliases''' and Unix/Linux command '''history'''
 
<br>
 
<br>
  
Line 36: Line 38:
  
 
|  style="padding-left:15px;" |Links
 
|  style="padding-left:15px;" |Links
* Hard Links
+
* [http://www.linfo.org/hard_link.html#:~:text=A%20hard%20link%20is%20merely,created%20to%20other%20hard%20links. Hard Links]
* 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
 
* Process Information  
 
* Process Information  
Line 51: 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]
 +
* [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]
  
 
|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 64: Line 68:
 
'''inode (index) Number of a File:'''
 
'''inode (index) Number of a File:'''
  
<table align="right"><tr valign="top"><td>[[Image:inode-number-1.png|thumb|right|300px|Each inode stores the attributes and disk block locations of the object's data.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-2.png|thumb|right|360px|The '''inode number''' is like a '''finger-print''', and usually is '''unique''' for each file on the Unix / Linux file system.]]</td></tr></table>
+
<table align="right"><tr valign="top"><td>[[Image:inode-number-1.png|thumb|right|300px|Each inode stores the attributes and disk block locations of the object's data.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-2.png|thumb|right|360px|The '''inode number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.]]</td></tr></table>
 
''The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data.''
 
''The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data.''
  
Line 70: Line 74:
  
  
The '''inode number''' is like a '''finger-print''', and usually is '''unique''' for each file on the Unix / Linux file system.<br>Referring to the diagram on the far right, issuing the '''ls''' command with the '''-i''' option<br>displays the inode number for each file. You can see that <u>each</u> file<br>(whether it is a directory or regular file) has its own unique inode number.
+
The '''inode number''' is like a '''finger-print''', and is considered to be '''unique''' for each file on the Unix / Linux file system.<br>Referring to the diagram on the far right, issuing the '''ls''' command with the '''-i''' option<br>displays the inode number for each file. You can see that <u>each</u> file<br>(whether it is a directory or regular file) has its own unique inode number.
  
  
Line 77: Line 81:
  
 
<table align="right"><tr valign="top"><td>[[Image:hard-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])<br>Image manipulated by author]]]</td><td>[[Image:inode-number-3.png|thumb|right|500px|'''Hard links''' share the '''same inode number'''. Hard links are very good for '''backup''' purposes.]]</td></tr></table>
 
<table align="right"><tr valign="top"><td>[[Image:hard-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])<br>Image manipulated by author]]]</td><td>[[Image:inode-number-3.png|thumb|right|500px|'''Hard links''' share the '''same inode number'''. Hard links are very good for '''backup''' purposes.]]</td></tr></table>
A '''Hard link''' is a reference to the physical data on a file  system.
+
A '''Hard link''' is a '''reference''' to the physical data on a file  system.
  
'''Advantages''' of hard links are that if one hard link remains (even if original file has been removed), the data in that hard linked file is NOT lost, a hard links will automatically change when a change to that original file or hard links occur since they share the same '''i-node number'''<br>and the Unix/Linux OS treats them all as the same file.
+
'''Advantages''' of hard links are that if one hard link remains (even if original file has been removed), the '''data in that hard linked file is NOT lost''', as well as hard linked files will automatically be updated to that of the original file since they share the same '''i-node number'''.
  
'''Disadvantages''' of hard links are that they take-up extra space,<br>you cannot hard link directory files, and you cannot hard link files from other Unix/Linux servers<br>(since the inode number may already be used by the other Unix/Linux server).
+
'''Disadvantages''' of hard links are that they '''take-up extra space''',<br>you cannot hard link directories, and you cannot hard link files from other Unix/Linux servers<br>(since the inode number may already be used by the other Unix/Linux server).
  
  
Line 93: Line 97:
  
 
<table align="right"><tr valign="top"><td>[[Image:symbolic-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-4.png|thumb|right|500px|Symbolic links do NOT share the same i-node number. '''Symbolic links''' are very good for short-cuts to other files (including directories) but NOT for backup purposes.]]</td></tr></table>
 
<table align="right"><tr valign="top"><td>[[Image:symbolic-link-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]</td><td>[[Image:inode-number-4.png|thumb|right|500px|Symbolic links do NOT share the same i-node number. '''Symbolic links''' are very good for short-cuts to other files (including directories) but NOT for backup purposes.]]</td></tr></table>
A '''Symbolic Link''' is an indirect pointer to a file and are also known as '''soft link''' or '''symlink'''.
+
A '''Symbolic Link''' is an '''indirect pointer''' to a file and are also known as '''soft link''' or '''symlink'''.
  
'''Advantages''' of symbolic links are that they 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.
+
'''Advantages''' of symbolic links are that they 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''' of symbolic links are that they are NOT good for backup purposes<br>since a symbolic link can point to a nonexistent file  (referred to as a "broken link").
 
'''Disadvantages''' of symbolic links are that they are NOT good for backup purposes<br>since a symbolic link can point to a nonexistent file  (referred to as a "broken link").
Line 109: Line 113:
  
 
[[Image:process-diagram-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
 
[[Image:process-diagram-1.png|thumb|right|300px|(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
All programs that are executing on a Unix/Linux computer system are referred to as '''processes''':
+
All programs that are running on a Unix/Linux computer system are referred to as '''processes''':
  
 
* Each process has an '''owner'''
 
* Each process has an '''owner'''
* Each process has a unique ID ('''PID''')  Processes in UNIX can run in the '''foreground''' or '''background'''
+
* Each process has a unique ID ('''PID''')   
 +
* Processes in UNIX can run in the '''foreground''' or '''background'''
 
* Programs / Commands can be run in the '''background''' by placing an '''ampersand''' '''&''' after the command<br>(eg. '''program-name  &''')
 
* Programs / Commands can be run in the '''background''' by placing an '''ampersand''' '''&''' after the command<br>(eg. '''program-name  &''')
  
 
UNIX processes are '''hierarchical''':
 
UNIX processes are '''hierarchical''':
  
* The process structure has a '''root''', '''parents''', and '''children'''
+
* The process structure has a '''root''', '''parents''', and '''children''', etc.
 
* Creation of a new process is called '''forking''' or  '''spawning'''
 
* Creation of a new process is called '''forking''' or  '''spawning'''
* The Parent process can spawn a child and children can spawn their own children
+
* The Parent process can spawn a child process, and in turn spawn their own child processes
 
* Processes keep their '''PID''' for their entire life
 
* Processes keep their '''PID''' for their entire life
* Usually a parent sleeps when a child is executing<br>(the exception is when the child process is executing in the background)
+
* Usually a parent '''sleeps''' when a child is executing<br>(the exception is when the child process is running in the background)
  
 
Users can learn to manage processes to become more productive while working in the Unix / Linux Command-line environment.
 
Users can learn to manage processes to become more productive while working in the Unix / Linux Command-line environment.
Line 128: Line 133:
 
Below are common Linux commands / keyboard shortcuts to manage processes:
 
Below are common Linux commands / keyboard shortcuts to manage processes:
  
<table cellpadding="5" width="80%"><tr valign="top"><th width="25%" style="border-bottom: 1px solid black;text-align:left;">Linux Command / Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''ps'''</td><td>The '''ps''' (''process status'') command displays  snapshot information about processes. By default, the ps command displays information only about the current terminal ('''ps -l''' provides a detailed listing, '''ps -U''' username shows all)<br><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>'''top'''</td><td>The '''top''' command provides a continuous  update including resource usage<br>'''NOTE:''' You can press '''ctrl-c''' to exit<br></td></tr><tr valign="top"><td>'''fg'''</td><td>The '''fg''' (foreground) command moves a background job from the current environment into the foreground. The fg command issued without arguments will place the most recent process in the background to the foreground. ''Example: ''<span style="font-family:courier;font-weight:bold">fg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;c&gt;'''</span></td><td>'''Terminates''' a process running in the foreground</td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;z&gt;'''</span></td><td>Sends a process running in the foreground into the '''background'''.</td></tr><tr valign="top"><td>'''bg'''</td><td>The '''bg''' utility shall resume suspended jobs from the current environment. The bg command issued without arguments will run 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>'''jobs'''</td><td>The '''jobs''' utility shall display the status of jobs that were started in the current shell environment<br>''Examples:<br>''<span style="font-family:courier;">jobs<br>[1]+  Stopped  vim a <span style="font-size:8pt;">&nbsp; &lt;-- Job #1 (+ means most recent process sent to background)</span><br>[2]&nbsp; Running          sleep 200 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #2</span><br>[3] &nbsp;Running              sleep 300 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #3</span><br>[4]-  Running                sleep 400 &<span style="font-size:8pt;">&nbsp; &lt;-- Job #4 (- means second recent process sent to background)</span><br><br></span></td></tr><tr valign="top"><td>'''kill'''</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 / Key Combination</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''ps'''</td><td>The '''ps''' (''process status'') command displays  snapshot information about processes. By default, the ps command displays information only about the current terminal ('''ps -l''' provides a detailed listing, '''ps -U''' username shows all)<br><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>'''top'''</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>'''fg'''</td><td>The '''fg''' (foreground) command moves a background job from the current environment into the foreground. The fg command issued without arguments will place the most recent process in the background to the '''foreground'''. ''Example: ''<span style="font-family:courier;font-weight:bold">fg %job-number</span></td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;c&gt;'''</span></td><td>'''Terminates''' a process running in the foreground</td></tr><tr valign="top"><td><span style="font-family:courier;">'''&lt;ctrl&gt;&lt;z&gt;'''</span></td><td>Sends a process running in the foreground into the '''background'''.</td></tr><tr valign="top"><td>'''bg'''</td><td>The '''bg''' utility '''resumes''' suspended jobs from the current environment. The bg command issued without arguments will run 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>'''jobs'''</td><td>The '''jobs''' utility displays the '''status''' of jobs that were started in the current shell environment<br>''Examples:<br>''<span style="font-family:courier;">jobs<br>[1]+  Stopped  vim a <span style="font-size:8pt;">&nbsp; &lt;-- Job #1 (+ means most recent process sent to background)</span><br>[2]&nbsp; Running          sleep 200 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #2</span><br>[3] &nbsp;Running              sleep 300 & <span style="font-size:8pt;">&nbsp;&lt;-- Job #3</span><br>[4]-  Running                sleep 400 &<span style="font-size:8pt;">&nbsp; &lt;-- Job #4 (- means second recent process sent to background)</span><br><br></span></td></tr><tr valign="top"><td>'''kill'''</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>
  
 
===Aliases / Command History===
 
===Aliases / Command History===
Line 145: Line 150:
 
<span style="font-family:courier;font-weight:bold;>alias dir=ls<br>alias ls='ls -al'<br>alias clearfile='cat /dev/null >'</span>
 
<span style="font-family:courier;font-weight:bold;>alias dir=ls<br>alias ls='ls -al'<br>alias clearfile='cat /dev/null >'</span>
  
 +
'''<span style="font-family:courier;font-weight:bold;>unalias alias-name</span>'''  (removes alias from memory)
  
  
 
'''Command History:'''
 
'''Command History:'''
  
'''~/.bash_history''' stores recently executed command lines
+
The filename '''~/.bash_history''' stores recently executed command lines
  
<table width="60%"><tr><td><span style="font-family:courier;font-weight:bold;><up></span> or <span style="font-family:courier;font-weight:bold;><down></span>      </td><td>move to previous command or next command</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</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 command number "num"</td></tr><tr><td><span style="font-family:courier;font-weight:bold;>!xxx</span></td><td>re-execute last command beginning with string "xxx"</td></tr></table>
+
<table width="60%"><tr><td width="25%"><span style="font-family:courier;font-weight:bold;><up></span> or <span style="font-family:courier;font-weight:bold;><down></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-execute''' an issued command beginning with string "xxx"</td></tr></table><br><br>
  
 
=INVESTIGATION 1: LINKING FILES=
 
=INVESTIGATION 1: LINKING FILES=
Line 161: Line 167:
 
'''Perform the Following Steps:'''
 
'''Perform the Following Steps:'''
  
# '''Login''' 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 command to '''confirm''' you are located in your home directory.<br><br>
 
# 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 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>
Line 167: Line 173:
 
# Use a text editor to create a file called '''~/links/data-file.txt'''<br><br>
 
# Use a text editor to create a file called '''~/links/data-file.txt'''<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>
# 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>Note the '''i-node''' number for this file. What does this i-node number represent?<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>
 
# 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<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<br><br>
 
# Issue the '''ls -li''' command for the '''~/links''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br><br>
 
# Issue the '''ls -li''' command for the '''~/links''' directory.<br><br>What do you notice about both of these file's i-node numbers?<br><br>
Line 177: Line 184:
 
# 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>What do you noticed what happened to this original file?<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>What do you noticed what happened to this original file?<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 '''ls -i''' command to determine the i-node number for the file called '''~/data-file.hard.lnk'''<br><br>What do you notice about this file's ''i-node'' number?
+
# Issue the '''ls -i''' command to determine the i-node number for the file called '''~/data-file.hard.lnk'''<br><br>What do you notice about this file's ''i-node'' number?<br><br>
 
# Make certain you are currently located in your '''home''' directory.<br><br>
 
# Make certain you are currently located in your '''home''' directory.<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>
# Issue the following Linux command to create a directory called '''~/links2''':<br><span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/links</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>
 
# Issue the '''ls''' command to confirm that the directory called '''~/links2''' exists.<br><br>
 
# Issue the '''ls''' command to confirm that the directory called '''~/links2''' exists.<br><br>
 
# Use a text editor to create a file called '''~/links2/text-file.txt'''<br><br>
 
# Use a text editor to create a file called '''~/links2/text-file.txt'''<br><br>
 
# 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>
 
# 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 '''ls -li''' command for the '''~/links2''' directory.<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 ~/links2/text-file.sym.lnk?<br>What pathname do you think it represents?<br><br>
+
# Issue the '''ls -li''' command for the '''~/links2''' directory.<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 ''~/links2/text-file.sym.lnk''?<br>What pathname do you think it represents?<br><br>
 
# Change to your '''home''' directory.<br><br>
 
# Change to your '''home''' directory.<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 ls -l command for the ~/text-file.txt.sym.lnk file.<br><br>What do you notice? What is the file size?<br>What pathname do you think this file contains?<br><br>
+
# Issue the '''ls -l''' command for the '''~/text-file.txt.sym.lnk''' file.<br><br>What do you notice? What is the file size?<br>What pathname do you think this file contains?<br><br>
 
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/text-file.txt.sym.lnk</span><br><br>What did you notice?<br><br>
 
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat ~/text-file.txt.sym.lnk</span><br><br>What did you notice?<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>
# Re-issue the Linux command you performed in '''step #34'''.<br><br>What happened? Why?<br><br>
+
# Re-issue the Linux command you performed in '''step #31'''.<br><br>What happened? Why?<br><br>
 
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file.txt.sym.lnk</span><br><br>What do you notice?<br><br>
 
# Issue the following Linux command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls -l ~/text-file.txt.sym.lnk</span><br><br>What do you notice?<br><br>
 
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~uli101 linux</span><br><br>
 
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ln -s ~uli101 linux</span><br><br>
Line 204: Line 212:
  
 
In this section, you will learn how to manage processes on a Unix / Linux server.
 
In this section, you will learn how to manage processes on a Unix / Linux server.
 
  
  
Line 210: Line 217:
  
 
# 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 the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br>'''NOTE:''' The sleep command just waits for a specified number of seconds before completing<br>in order to return to the shell prompt. It is useful in order to force a pause in a sequence of commands.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 700</span><br><br>'''NOTE:''' The '''sleep''' command waits for a specified number of seconds before returning to the shell prompt. It is useful in order to force a pause in a sequence of commands.<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 process'''.<br><br>The Unix/Linux system is designed to allow users to send preemptive signals to manage those 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 process'''.<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">&lt;ctrl&gt;&lt;c&gt;</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''') to terminate a process that is running.<br><br>
+
# Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;c&gt;</span><br><br>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''') to terminate a process that is running.<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">&lt;ctrl&gt;&lt;z&gt;</span><br><br>
 
# Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;z&gt;</span><br><br>
# 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.<br><br>
+
# 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.<br><br>
# Clear your bash shell terminal.<br><br>
+
# Clear your bash shell terminal screen.<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>
+
# 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 specify the most recent process<br>that was placed into the background. From the '''jobs''' command, the process that has a plus sign "+"<br>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 specify the most recent process<br>that was placed into the background. From the '''jobs''' command, the process that has a plus sign "+"<br>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">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>(denoted by the ampersand character "&"). Now the command is counting-down to zero.<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]+ sleep 700 &</span><br><br>This display indicates that this process in the background is '''running in the background'''<br>(denoted by the ampersand character "&"). Now the command is counting-down to zero.<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>You can run commands with ampersand "&" to run processes in the background.<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>You can run commands with ampersand "'''&'''" to run processes in the background.<br><br>  
# Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;c&gt;</span> to stop the process running in the foreground.<br><br>  
+
# Press the key combination: <span style="color:blue;font-family:courier;font-weight:bold">&lt;ctrl&gt;&lt;c&gt;</span> to '''terminate''' the process running in the foreground.<br><br>  
 
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 500 & sleep 600 & sleep 700 &</span><br><br>
 
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">sleep 500 & sleep 600 & sleep 700 &</span><br><br>
 
# Issue the '''jobs''' command. What do you notice?<br><br>
 
# Issue the '''jobs''' command. What do you notice?<br><br>
Line 228: Line 235:
 
# 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>You can also manipulate processes by their '''PID''' ('''process ID''').<br><br>
 
# Issue the '''jobs''' command. What do you notice?<br><br>You can also manipulate processes by their '''PID''' ('''process ID''').<br><br>
# 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: <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><br>
 
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">ps</span><br><br>What is the PID of the previously issued Linux command in the background?<br><br>
 
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">ps</span><br><br>What is the PID of the previously issued Linux command in the background?<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 PID</span><br><br>'''NOTE:''' You may need to issue the kill -9 PID command if the previous commands did not work.<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 PID</span><br><br>
# Issue the '''jobs''' command to confirm there are no processes running in the background.<br><br>
+
# Issue the '''jobs''' command. Is the sleep process still running?<br><br>'''NOTE:''' You need to issue the ''kill -9 PID'' command if the previous commands did not work.<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 -9 PID</span><br><br>
 +
# Issue the '''jobs''' and '''ps''' commands to confirm there are no processes running in the background.<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>
<br>
 
  
 
=INVESTIGATION 3: ALIASES / COMMAND HISTORY=
 
=INVESTIGATION 3: ALIASES / COMMAND HISTORY=
  
 
<br>
 
<br>
In this section, you will learn how to manage aliases and view and access history on your Matrix account.
+
In this section, you will learn how to manage aliases and Linux command history on your Matrix account.
  
  
Line 248: Line 255:
 
# 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 the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">alias</span><br><br>Take a few moments to view existing aliases that have previously been declared.<br>Take a few moments to run those aliases to see what happens.<br><br>
 
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">alias</span><br><br>Take a few moments to view existing aliases that have previously been declared.<br>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 ls='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>
# Run this alias to see what it does.<br><br>
+
# Run the alias '''lal''' to see what it does.<br><br>
 
# Issue the following to create another alias (lowercase l and h):<br><span style="color:blue;font-family:courier;font-weight:bold">alias lh='ls --human-readable --size -1 -S --classify'</span><br><br>
 
# Issue the following to create another alias (lowercase l and h):<br><span style="color:blue;font-family:courier;font-weight:bold">alias lh='ls --human-readable --size -1 -S --classify'</span><br><br>
 
# Issue the '''alias''' command without any arguments to confirm that it is stored in memory.<br><br>
 
# Issue the '''alias''' command without any arguments to confirm that it is stored in memory.<br><br>
 
# Run the '''lh''' command to see what happens. What do you think this command does?<br><br>
 
# Run the '''lh''' command to see what happens. What do you think this command does?<br><br>
 
# Logout of your Matrix account, than log into your Matrix account.<br><br>
 
# Logout of your Matrix account, than log into your Matrix account.<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''' command without any arguments to see if it is stored in memory.<br><br>
# x
+
# Reissue the command to create the '''lh''' alias in '''step #5'''.<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>
 
+
# 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>
# After you complete the Review Questions sections to get additional practice, then work on your<br>online assignment 2 and complete section4 labelled: '''Linking files and directories'''.
+
# Logout of your Matrix account, than log into your Matrix account.<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>
 +
# Run the '''lh''' command to see what happens.<br><br>
 +
# Logout of your Matrix account, than log into your Matrix account.<br><br>
 +
# Reissue the '''lh''' alias. What happened? Why?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history</span><br><br>What do you notice?<br><br>
 +
# Press an exclamation mark ! immediately followed by the number by one of those commands 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>
 +
# Issue the following Linux command: <span style="color:blue;font-family:courier;font-weight:bold">history | grep ln</span><br><br>What happened?<br><br>
 +
# After you complete the Review Questions sections to get additional practice, then work on your<br>'''online assignment 2''' and complete '''section4''' labelled: '''Linking files and directories'''.<br><br>
  
 
= LINUX PRACTICE QUESTIONS =
 
= LINUX PRACTICE QUESTIONS =
Line 277: Line 294:
 
'''Review Questions:'''
 
'''Review Questions:'''
  
# Write a single Linux command to create a hard link called '''~/backup/myfile.txt.lnk''' to the existing file called '''~/myfile.txt'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br>In this case, will the inode numbers for those files above be the same or different?<br><br>
+
# Write a single Linux command to create a hard link called '''~/backup/myfile.txt.lnk''' to the existing file called '''~/myfile.txt'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br><br>
# Write a single Linux command to create a symbolic link called '''~/shortcuts/murray.saul.lnk''' to the existing directory called '''~murray.saul'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br>In this case, will the inode numbers for those files above be the same or different?<br><br>What data is contained in the file called '''~/shortcuts/murray.saul.lnk'''?<br>What would be the size of the file called '''~/shortcuts/murray.saul.lnk'''?<br><br>
+
# Write a single Linux command to create a symbolic link called '''~/shortcuts/murray.saul.lnk''' to the existing directory called '''~murray.saul'''<br>Write a single Linux command to display detailed information for those files above displaying their i-node numbers.<br><br>What data is contained in the file called '''~/shortcuts/murray.saul.lnk'''?<br>What would be the size of the file called '''~/shortcuts/murray.saul.lnk'''?<br><br>
 
# Write a single Linux command to run the program called '''~/clean.sh''' in the background.<br>What command would you issue to place the previously issued program in the foreground?<br>What command would you issue to confirm that this program is running in the background?<br>What key-combination would you issue to send that program again into the background?<br><br>
 
# Write a single Linux command to run the program called '''~/clean.sh''' in the background.<br>What command would you issue to place the previously issued program in the foreground?<br>What command would you issue to confirm that this program is running in the background?<br>What key-combination would you issue to send that program again into the background?<br><br>
 
# Write a single Linux command to display running processes in “real-time”.
 
# Write a single Linux command to display running processes in “real-time”.
 
# Write a single Linux command to terminal a process that has the following PID: '''22384'''
 
# Write a single Linux command to terminal a process that has the following PID: '''22384'''
 
# Use the following diagram to answer the accompanying questions.<br>Each of the following questions will use the diagram below and are treated as independent situations.<br><br><span style="font-family:courier;font-weight:bold;">[1]&nbsp;  Stopped                vim a<br>[2]-  Stopped                vim b<br>[3]+  Stopped                vim c</span><br><br>Write a single Linux command to bring the second-recently process placed in the background into the foreground.<br>Write a single Linux command to terminate the '''job #3'''.<br><br>
 
# Use the following diagram to answer the accompanying questions.<br>Each of the following questions will use the diagram below and are treated as independent situations.<br><br><span style="font-family:courier;font-weight:bold;">[1]&nbsp;  Stopped                vim a<br>[2]-  Stopped                vim b<br>[3]+  Stopped                vim c</span><br><br>Write a single Linux command to bring the second-recently process placed in the background into the foreground.<br>Write a single Linux command to terminate the '''job #3'''.<br><br>
# Create a '''table''' listing each Linux command, useful options  and command purpose for the following Linux commands: '''ln''' , '''ps''' , '''top''' , '''fg''' , '''bg''' , '''jobs''' , '''kill'''
+
# 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:<br>'''ln''' , '''ps''' , '''top''' , '''fg''' , '''bg''' , '''jobs''' , '''kill''' , '''alias''' , '''unalias''' , '''history'''
  
  
 
[[Category:ULI101]]
 
[[Category:ULI101]]

Revision as of 10:53, 2 September 2020

LINKING FILES / MANAGING PROCESSES


Main Objectives of this Practice Tutorial

  • Understand the purpose and why links are used in Unix / Linux
  • Define the term inode as it relates to a file on Unix / Linux
  • Define the terms: Hard Link and Symbolic Link
  • Issue the ln command to create hard and symbolic links
  • Define and understand the purpose of a process in Unix / Linux
  • Run and terminate processes in the foreground and background
  • Display and manipulate background and foreground processes
  • Manage aliases and Unix/Linux command history


Tutorial Reference Material

Course Notes
Concepts / Commands
YouTube Videos
Course Notes:


Links

Managing Processes

  • Process Information
  • Manipulating Processes
  • Running commands / programs in background with &
Linux Commands Brauer Instructional Videos:

KEY CONCEPTS

Linking Files

Links are powerful and add flexibility to Linux filesystems because everything is a file.

There are two types of Linux filesystem links: hard and soft. The difference between the two types of links is significant, but both types are used to solve similar problems. They both provide multiple directory entries (or references) to a single file, but they do it quite differently.


Reference: https://opensource.com/article/17/6/linking-linux-filesystem


inode (index) Number of a File:

Each inode stores the attributes and disk block locations of the object's data.
(Image licensed under cc)
The inode number is like a finger-print, and is considered to be unique for each file on the Unix / Linux file system.

The inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attributes may include metadata (times of last change, access, modification), as well as owner and permission data.

Reference: https://en.wikipedia.org/wiki/Inode


The inode 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 inode number for each file. You can see that each file
(whether it is a directory or regular file) has its own unique inode number.


Hard Links:

(Image licensed under cc)
Image manipulated by author]
Hard links share the same inode number. Hard links are very good for backup purposes.

A Hard link is a reference to the physical data on a file system.

Advantages of hard links are that if one hard link remains (even if original file has been removed), the data in that hard linked file is NOT lost, as well as hard linked files will automatically be updated to that of the original file since they share the same i-node number.

Disadvantages of hard links are that they 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:

touch myfile.txt
ln myfile.txt myfile1.hard.lnk
ln myfile.txt myfile2.hard.lnk
ln myfile.txt ~/backups/myfile.hard.lnk


Symbolic Links:

(Image licensed under cc)
Symbolic links do NOT share the same i-node number. Symbolic links are very good for short-cuts to other files (including directories) but NOT for backup purposes.

A Symbolic Link is an indirect pointer to a file and are also known as soft link or symlink.

Advantages of symbolic links are that they 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 of symbolic links are that they are NOT good for backup purposes
since a symbolic link can point to a nonexistent file (referred to as a "broken link").


Examples:

touch otherfile.txt
ln -s otherfile.txt otherfile1.sym.lnk
ln -s otherfile.txt otherfile2.sym.lnk
ln -s otherfile.txt ~/backups/otherfile.sym.lnk


Managing Processes

(Image licensed under cc)

All programs that are running on a Unix/Linux computer system are referred to as processes:

  • Each process has an owner
  • Each process has a unique ID (PID)
  • Processes in UNIX can run in the foreground or background
  • Programs / Commands can be run in the background by placing an ampersand & after the command
    (eg. program-name &)

UNIX processes are hierarchical:

  • The process structure has a root, parents, and children, etc.
  • Creation of a new process is called forking or spawning
  • The Parent process can spawn a child process, and in turn spawn their own child processes
  • Processes keep their PID for their entire life
  • Usually a parent sleeps when a child is executing
    (the exception is when the child process is running in the background)

Users can learn to manage processes to become more productive while working in the Unix / Linux Command-line environment.


Below are common Linux commands / keyboard shortcuts to manage processes:

Linux Command / Key CombinationPurpose
psThe ps (process status) command displays snapshot information about processes. By default, the ps command displays information only about the current terminal (ps -l provides a detailed listing, ps -U username shows all)

Examples: ps , ps -l , ps -ef , ps -u , ps aux
topThe top command provides a realtime status of running processes.
NOTE: You can press ctrl-c to exit
fgThe fg (foreground) command moves a background job from the current environment into the foreground. The fg command issued without arguments will place the most recent process in the background to the foreground. Example: fg %job-number
<ctrl><c>Terminates a process running in the foreground
<ctrl><z>Sends a process running in the foreground into the background.
bgThe bg utility resumes suspended jobs from the current environment. The bg command issued without arguments will run the most recent process that was placed into the background.
Example: bg %job-number
jobsThe jobs utility displays the status of jobs that were started in the current shell environment
Examples:
jobs
[1]+ Stopped vim a   <-- Job #1 (+ means most recent process sent to background)
[2]  Running sleep 200 &  <-- Job #2
[3]  Running sleep 300 &  <-- Job #3
[4]- Running sleep 400 &  <-- Job #4 (- means second recent process sent to background)

killThe 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

Alias:

Assigns a new name to an existing utility

Example:

alias (Alias command without an argument will display all the aliases currently set)

Other Examples:

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

<up> or <down> Move to previous command or next command within Bash shell prompt
fc -l display last 16 commands
history | moredisplay all stored commands
!numre-execute an issued command number by command number (determined from history command)
!xxxre-execute an issued command beginning with string "xxx"


INVESTIGATION 1: LINKING FILES


In this section, you will learn how to create hard links and symbolic links on your Matrix account.


Perform the Following Steps:

  1. Login to your matrix account.

  2. Issue a command to confirm you are located in your home directory.

  3. Issue the following Linux command to create a directory called ~/links:
    mkdir ~/links

  4. Issue the ls command to confirm that the directory called ~/links exists.

  5. Use a text editor to create a file called ~/links/data-file.txt

  6. Enter the following text displayed below:

    This is line 1
    This is line 2
    This is line 3

  7. Save your editing session and exit your text editor.

  8. 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?

  9. 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

  10. Issue the ls -li command for the ~/links directory.

    What do you notice about both of these file's i-node numbers?

  11. Use a text editor to edit the file ~/links/data-file.txt and add some additional lines to this file.

  12. Save your editing session and exit your text editor.

  13. Issue the following Linux command:
    cat ~/links/data-file.hard.lnk

    What do you noticed what happened to this linked file?

  14. Use a text editor to edit the hard-linked file ~/links/data-file.txt.hard.lnk and add some additional lines to this file.

  15. Save your editing session and exit your text editor.

  16. Issue the following Linux command:
    cat ~/links/data-file.hard.lnk

    What do you noticed what happened to this original file?

  17. Issue the following Linux command to create a hard-linked file in your home directory:
    ln ~/links/data-file.txt ~/data-file.hard.lnk

  18. Issue the ls -i command to determine the i-node number for the file called ~/data-file.hard.lnk

    What do you notice about this file's i-node number?

  19. Make certain you are currently located in your home directory.

  20. Issue the following Linux command to remove the ~/links directory and its contents:
    rm -rf ~/links

  21. Issue a Linux command to confirm that the ~/links directory has been removed.

  22. 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?

  23. Issue the following Linux command to create a directory called ~/links2:
    mkdir ~/links2

  24. Issue the ls command to confirm that the directory called ~/links2 exists.

  25. Use a text editor to create a file called ~/links2/text-file.txt

  26. Enter the following text displayed below:

    This is line one
    This is line two
    This is line three

  27. Save your editing session and exit your text editor.

  28. 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

  29. Issue the ls -li command for the ~/links2 directory.

    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 it represents?

  30. Change to your home directory.

  31. 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

  32. Issue the ls -l command for the ~/text-file.txt.sym.lnk file.

    What do you notice? What is the file size?
    What pathname do you think this file contains?

  33. Issue the following Linux command:
    cat ~/text-file.txt.sym.lnk

    What did you notice?

  34. Issue the following Linux command to remove the ~/links2 directory:
    rm -rf ~/links2

  35. Re-issue the Linux command you performed in step #31.

    What happened? Why?

  36. Issue the following Linux command:
    ls -l ~/text-file.txt.sym.lnk

    What do you notice?

  37. Issue the following Linux command: ln -s ~uli101 linux

  38. Issue the following Linux command: linux/assign1

    What happened? Why?


In the next investigation, you will learn how to manage processes on your Matrix server.

INVESTIGATION 2: MANAGING PROCESSES

In this section, you will learn how to manage processes on a Unix / Linux server.


Perform the Following Steps:

  1. Make certain that you are logged into your Matrix account.

  2. Issue the following Linux command: sleep 700

    NOTE: The sleep command waits for a specified number of seconds before returning to the shell prompt. It is useful in order to force a pause in a sequence of commands.

  3. 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 process.

    The Unix/Linux system is designed to allow users to send preemptive signals to manage those processes.

  4. Press the key combination: <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) to terminate a process that is running.

  5. Reissue the Linux command: sleep 700

  6. Press the key combination: <ctrl><z>

  7. 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 commands.

  8. Clear your bash shell terminal screen.

  9. 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.

  10. Issue the following Linux command: bg

    NOTE: You can use the bg command WITHOUT arguments to specify the most recent process
    that was placed into the background. From the jobs command, the process that has a plus sign "+"
    indicates the most recent process placed into the background.

  11. 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
    (denoted by the ampersand character "&"). Now the command is counting-down to zero.

  12. Issue the following Linux command: fg

    You should notice that the sleep command is now running in the foreground.

    You can run commands with ampersand "&" to run processes in the background.

  13. Press the key combination: <ctrl><c> to terminate the process running in the foreground.

  14. Issue the following Linux command: sleep 500 & sleep 600 & sleep 700 &

  15. Issue the jobs command. What do you notice?

  16. Issue the following Linux command: kill %1

    NOTE: You can specify job number preceded by percent % with the
    kill, bg, and fg commands to specify the processes' job number.

  17. Issue the jobs command. What do you notice?

  18. Issue the following Linux commands:
    kill %2
    kill %3

  19. Issue the jobs command. What do you notice?

    You can also manipulate processes by their PID (process ID).

  20. Let's use grouping to run several commands in sequence within a single process.

  21. Issue the following Linux command: (sleep 400; sleep 500; sleep 600) &

  22. Issue the jobs command. What do you notice?

  23. Issue the following Linux command: ps

    What is the PID of the previously issued Linux command in the background?

  24. Issue the following Linux command (using the PID number instead of "PID"):
    kill PID

  25. Issue the jobs command. Is the sleep process still running?

    NOTE: You need to issue the kill -9 PID command if the previous commands did not work.

  26. Issue the following Linux command (using the PID number instead of "PID"):
    kill -9 PID

  27. Issue the jobs and ps commands to confirm there are no processes running in the background.

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 section, you will learn how to manage aliases and Linux command history on your Matrix account.


Perform the Following Steps:

  1. Make certain that you are logged into your Matrix account.

  2. Issue the following Linux command: alias

    Take a few moments to view existing aliases that have previously been declared.
    Take a few moments to run those aliases to see what happens.

  3. Issue the following to create an alias: alias lal='ls -al'

  4. Run the alias lal to see what it does.

  5. Issue the following to create another alias (lowercase l and h):
    alias lh='ls --human-readable --size -1 -S --classify'

  6. Issue the alias command without any arguments to confirm that it is stored in memory.

  7. Run the lh command to see what happens. What do you think this command does?

  8. Logout of your Matrix account, than log into your Matrix account.

  9. Reissue the lh alias. What happened?

  10. Issue the alias command without any arguments to see if it is stored in memory.

  11. Reissue the command to create the lh alias in step #5.

  12. Run the lh alias to confirm that it is properly set in memory.

  13. Issue the following Linux command to edit your ~/.bashrc startup file:
    nano ~/.bashrc

  14. Add the following line at the bottom of this file:
    alias lh='ls --human-readable --size -1 -S --classify'

  15. Save your editing changes and exit your text editor.

  16. Logout of your Matrix account, than log into your Matrix account.

  17. Reissue the lh alias. What happened?

  18. Issue the following Linux command: unalias lh

  19. Run the lh command to see what happens.

  20. Logout of your Matrix account, than log into your Matrix account.

  21. Reissue the lh alias. What happened? Why?

  22. Issue the following Linux command: history

    What do you notice?

  23. Press an exclamation mark ! immediately followed by the number by one of those commands listed in the history list and press ENTER

    What happened?

  24. Type the following: !unalias and press ENTER

    What happened?

  25. Issue the following Linux command: history | grep ln

    What happened?

  26. After you complete the Review Questions sections to get additional practice, then work on your
    online assignment 2 and complete section4 labelled: Linking files and directories.

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://ict.senecacollege.ca/~murray.saul/uli101/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:

  1. Write a single Linux command to create a hard link called ~/backup/myfile.txt.lnk to the existing file called ~/myfile.txt
    Write a single Linux command to display detailed information for those files above displaying their i-node numbers.

  2. Write a single Linux command to create a symbolic link called ~/shortcuts/murray.saul.lnk to the existing directory called ~murray.saul
    Write a single Linux command to display detailed information for those files above displaying their i-node numbers.

    What data is contained in the file called ~/shortcuts/murray.saul.lnk?
    What would be the size of the file called ~/shortcuts/murray.saul.lnk?

  3. Write a single Linux command to run the program called ~/clean.sh in the background.
    What command would you issue to place the previously issued program in the foreground?
    What command would you issue to confirm that this program is running in the background?
    What key-combination would you issue to send that program again into the background?

  4. Write a single Linux command to display running processes in “real-time”.
  5. Write a single Linux command to terminal a process that has the following PID: 22384
  6. 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 the job #3.

  7. Write a linux command to create an alias called ld that issues the command: ls -ld
  8. Write a linux command to unset the alias created in the previous question.
  9. issue a Linux command to list history of commands that match the pattern called touch.
  10. 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