Difference between revisions of "Tutorial 8 - Links / Process Management"

From CDOT Wiki
Jump to: navigation, search
Line 1: Line 1:
 
Content under development
 
Content under development
 +
 +
=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 '''<span style="font-family:courier">ln</span>''' 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 '''<span style="font-family:courier">alias</span>''' and '''<span style="font-family:courier">history</span>''' commands in Unix/Linux
 +
 +
===Tutorial Reference Material===
 +
 +
{|width="100%" cellspacing="0" cellpadding="10"
 +
 +
|- valign="top"
 +
 +
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;"|Course Notes<br>
 +
 +
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Concepts / Commands<br>
 +
 +
|- valign="top" style="padding-left:15px;"
 +
 +
|colspan="2" |Course Notes:<ul><li>[https://matrix.senecacollege.ca/~osl640/slides/OSL640-Week8.pdf PDF] | [https://matrix.senecacollege.ca/~osl640/slides/OSL640-Week8.pptx PPTX]</li></ul>
 +
 +
 +
|  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]
 +
* [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
 +
* [https://en.wikipedia.org/wiki/Inode inode]
 +
* [https://opensource.com/article/18/9/linux-commands-process-management Manipulating Processes]
 +
 +
|  style="padding-left:15px;"|Linux Commands
 +
* [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/top.1.html top]
 +
* [http://man7.org/linux/man-pages/man1/fg.1p.html fg]
 +
* [http://man7.org/linux/man-pages/man1/bg.1p.html bg]
 +
* [http://man7.org/linux/man-pages/man1/jobs.1p.html jobs]
 +
* [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]
 +
* [https://mediatemple.net/community/products/dv/204404624/using-the-history-command history]
 +
 +
 +
|}
 +
= KEY CONCEPTS =
 +
 +
<!--
 +
 +
<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.''
 +
 +
Reference: https://en.wikipedia.org/wiki/Inode
 +
 +
-->
 +
 +
===i-node (index) ID Number of a File===
 +
 +
[[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.]]
 +
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===
 +
 +
[[Image:hard-link-1.png|thumb|right|300px|A '''Hard link''' is a file which is created that shares the '''same i-node number''' with the <u>original</u> file<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])<br>Image manipulated by author]]
 +
A '''Hard link''' is a '''reference''' to the physical data on a file  system.<br>It does this by creating a file that shares the '''same i-node number''' with the <u>original</u> 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''',<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:''
 +
 +
<span style="font-family:courier">ln myfile.txt myfile1.hard.lnk<br>ln myfile.txt ~/backups/myfile.hard.lnk<br>'''</span>
 +
 +
===Symbolic Links===
 +
 +
[[Image:symbolic-link-1.png|thumb|right|300px|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. (Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
 +
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'''<br>'''on different Unix/Linux servers''', and that you '''can create symbolic links for directories'''.
 +
 +
'''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:''
 +
 +
<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>
 +
 +
===Managing Processes===
 +
 +
All '''commands/programs''' ('''tasks''') that are '''running''' on a Unix/Linux computer system are referred to as '''processes'''.
 +
<br><br>
 +
'''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.<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;">&nbsp; &lt;-- Job #1 (+ most recent process / 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 (- 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>
 +
 +
===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:''
 +
 +
<span style="font-family:courier;font-weight:bold;>alias</span> (Alias command without an argument will display all the aliases currently set)
 +
 +
<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:'''
 +
<br><br>
 +
The filename <span style="font-family:courier;font-weight:bold;>~/.bash_history</span> stores recently executed command lines
 +
<br><br>
 +
''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-run''' a most recent previously-issued command beginning with string "xxx"</td></tr></table><br><br>
  
 
= INVESTIGATION 1: LINKING FILES =
 
= INVESTIGATION 1: LINKING FILES =

Revision as of 21:09, 25 October 2021

Content under development

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
Course Notes:


Links

Managing Processes

Linux Commands


KEY CONCEPTS

i-node (index) ID Number of a File

The i-node number is like a finger-print, and is considered to be unique for each file on the Unix / Linux file system.

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 file which is created that shares the same i-node number with the original file
(Image licensed under cc)
Image manipulated by author

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

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. (Image licensed under cc)

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
psDisplays snapshot information about processes.
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
ctrl-cTerminates a process running in the foreground
ctrl-zSends a process running in the foreground into the background.
fgMoves a background job from the current environment into the foreground.
Example: fg %job-number
bgRuns (starts) 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. 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)

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


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 | moredisplay all stored commands
!numre-execute an issued command number by command number (determined from history command)
!xxxre-run a most recent previously-issued command beginning with string "xxx"


INVESTIGATION 1: LINKING FILES

INVESTIGATION 2: MANAGING PROCESSES

INVESTIGATION 3: ALIASES / COMMAND HISTORY

LINUX PRACTICE QUESTIONS