Difference between revisions of "Tutorial5: Redirection"

From CDOT Wiki
Jump to: navigation, search
(INVESTIGATION 2: REDIRECTION USING PIPES)
 
(447 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/tutorial5<br />'''Andrew's students please go here:''' http://wiki.littlesvr.ca/wiki/OPS145_Lab_5}}
 
=REDIRECTION: STANDARD INPUT / STANDARD OUTPUT / STANDARD ERROR=
 
=REDIRECTION: STANDARD INPUT / STANDARD OUTPUT / STANDARD ERROR=
 
<br>
 
<br>
 
===Main Objectives of this Practice Tutorial===
 
===Main Objectives of this Practice Tutorial===
  
:* x
+
:* Understand and use the '''cut''', '''tr''', and '''wc''' Linux commands
  
:* x
+
:* Define the terms '''Standard Input''' (''stdin''), '''Standard Output''' (''stdout''), and '''Standard Error''' (''stderr'')
  
:* x
+
:* Understand and use the '''>''', '''>>''', '''2>''', '''2>>''' symbols with Linux commands
  
:* x
+
:* Understand the purpose of the the '''/dev/null''' file and the '''Here Document'''
  
:* x
+
:* Define the term '''pipeline command''' and explain how a pipeline command functions
  
:* x
+
:* Define the term '''filter''' and how it relates to pipeline commands
  
:* x
+
:* Use the '''semicolon''' ";" and '''grouping''' "( )" symbols to issue multiple Unix / Linux commands on a single line
 +
 
 +
:* Use the '''backslash''' "\" symbol to spread-out long Unix/Linux commands over multiple lines
 +
<br>
  
 
===Tutorial Reference Material===
 
===Tutorial Reference Material===
Line 25: Line 29:
 
|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;"|Course Notes<br>
  
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
+
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command / Shortcut Reference<br>
  
 
|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|YouTube Videos<br>
 
|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|YouTube Videos<br>
Line 31: Line 35:
 
|- valign="top" style="padding-left:15px;"
 
|- valign="top" style="padding-left:15px;"
  
|colspan="2" |Course Notes:<ul><li>[https://ict.senecacollege.ca/~murray.saul/uli101/ULI101-Week5.pdf PDF] | [https://ict.senecacollege.ca/~murray.saul/uli101/ULI101-Week5.pptx PPTX]</li></ul>
+
|colspan="2" |'''Slides:'''<ul><li>Week 5 Lecture 1 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-5.1.pdf PDF] | https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-5.1.pptx PPTX]</li><li>Week 5 Lecture 2 Notes:<br> [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-5.2.pdf PDF] | [https://wiki.cdot.senecacollege.ca/uli101/slides/ULI101-5.2.pptx PPTX] <br></li></ul>
 +
 
 +
 
 +
|  style="padding-left:15px;" |'''Redirection:'''
 +
* [http://www.linfo.org/standard_input.html Standard Input (stdin)]
 +
* [http://www.linfo.org/standard_output.html Standard Output (stdout)]
 +
* [http://www.linfo.org/standard_error.html Standard Error (stderr)]
 +
* [http://www.linfo.org/pipe.html Pipeline Commands]<br><br>
 +
'''Multiple Commands:'''
 +
* [https://www.javatpoint.com/linux-semicolon Semicolon]<br>
 +
* [https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html Grouping ( )]<br><br>
 +
|  style="padding-left:15px;"|'''Redirection Filters:'''
 +
* [http://man7.org/linux/man-pages/man1/more.1.html more] , [http://man7.org/linux/man-pages/man1/less.1.html less]<br>
 +
* [http://man7.org/linux/man-pages/man1/head.1.html head] , [http://man7.org/linux/man-pages/man1/tail.1.html tail]
 +
* [http://man7.org/linux/man-pages/man1/sort.1.html sort]<br>
 +
* [http://man7.org/linux/man-pages/man1/uniq.1.html uniq]<br>
 +
* [http://linuxcommand.org/lc3_man_pages/grep1.html grep]<br>
 +
* [http://man7.org/linux/man-pages/man1/cut.1.html cut]<br>
 +
* [http://linuxcommand.org/lc3_man_pages/tr1.html tr]<br>
 +
* [http://man7.org/linux/man-pages/man1/wc.1.html wc]<br>
 +
* [http://man7.org/linux/man-pages/man1/tee.1.html tee]<br>
 +
 
 +
|colspan="1" style="padding-left:15px;" width="30%"|'''Brauer Instructional Videos:'''<ul><li>[https://www.youtube.com/watch?v=ocU34PcYn2U&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=4 Reading/Writing to Files<br>(echo, stdin, stdout, stderr, >, >>, 2>, cat, more, less, man, date, diff, diff -y, find, wc])<br></li></ul>
 +
|}
 +
 
 +
= KEY CONCEPTS =
 +
 
 +
===Additional File Manipulation Commands===
 +
 
 +
Before proceeding, let's look at some additional commands used to manipulate content of text files.
 +
 
 +
Refer to the table below regarding these text file manipulation commands:
 +
<br><br>
 +
[[Image:manipulation-commands.png|left|700px|]]
 +
<br><br><br><br><br><br><br><br><br>
 +
 
 +
===Redirection (Standard Input, Standard Output, Standard Error)===
 +
 
 +
<i>'''Redirection''' can be defined as changing the way from where commands read input to where commands sends output.<br>You can redirect input and output of a command.</i>
 +
 
 +
Reference: https://www.javatpoint.com/linux-input-output-redirection
 +
 
 +
[[Image:stdin-symbol.png|thumb|right|250px|The '''standard input''' ('''stdin''') symbol that describes where a Unix/Linux command receives '''input''']]
 +
'''Standard input''' ('''stdin''') is a term which describes from where a command receives '''input'''.<br>
 +
This would apply only to Unix/Linux commands that accept stdin input<br>(like ''cat'', ''more'', ''less'', ''sort'', ''grep'', ''head'', ''tail'', ''tr'', ''cut'', ''wc'', etc.).<br>
 +
 
 +
''Examples:''
 +
 
 +
<span style="font-family:courier;font-weight:bold">tr 'a-z' 'A-Z' < words.txt<br>cat < abc.txt<br>sort < xyz.txt</span>
 +
 
 +
 
 +
<table align="right"><tr><td>[[Image:stdout-symbol-1.png|thumb|right|250px|The '''standard out''' ('''stdout''') symbol with one greater than sign '''overwrites''' existing file content with command output]]</td><td>[[Image:stdout-symbol-2.png|thumb|right|250px|The '''standard output''' ('''stdout''') symbol  with two greater than signs '''add''' command's output to '''bottom''' of existing file's contents.]]</td></tr></table>
 +
'''Standard output''' ('''stdout''') describes where a command sends its '''output'''.<br>In the examples below, output from a command is sent to the '''monitor''', unless it is sent to a '''text file'''.
 +
 
 +
 
 +
''Examples:''
 +
 
 +
<span style="font-family:courier;font-weight:bold">ls -l<br>ls -l > detailed-listing.txt<br>ls /bin >> output.txt</span>
 +
 
 +
 
 +
<table align="right"><tr><td>[[Image:stderr-symbol-1.png|thumb|right|250px|The '''standard error''' ('''sterr''') symbol with one greater than sign '''overwrites''' existing file content with command's '''error message'''.]]</td><td>[[Image:stderr-symbol-2.png|thumb|right|250px|The '''standard error''' ('''stderr''') symbol  with two greater than signs '''add''' command's error message to '''bottom''' of existing file's contents.]]</td></tr></table>
 +
'''Standard Error''' ('''stderr''') describes where a command sends it's error messages. In the examples below we issue the pwd in capitals on purpose to generate an error message, which can be redirected to a '''text file'''.
 +
 
  
 +
''Examples:''
  
style="padding-left:15px;" |Redirection
+
<span style="font-family:courier;font-weight:bold">PWD<br>PWD 2> error-message.txt<br>PWD 2 >> error-messages.txt<br>PWD 2> /dev/null</span>
* Standard Input (stdin)
+
<br><br>
* Standard Output (stdout)
 
* Standard Error (stderr)
 
* Piping (pipes)<br><br>
 
Multiple Commands
 
* Semicolon
 
* Grouping ( )
 
  
 +
====The /dev/null File====
  
|  style="padding-left:15px;"|Redirection Filters
 
* head
 
* tail
 
* sort
 
* grep
 
* cut
 
* tr
 
* wc<br><br>
 
  
 +
The '''/dev/null''' file (sometimes called the '''bit bucket''' or  '''black hole''') is a special system file<br>that '''discard''' all  data written into it. This is useful to discard unwanted command output.
  
  
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=ocU34PcYn2U&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=4 Reading/Writing to Files<br>(echo, stdin, stdout, stderr, >, >>, 2>, cat, more, less, man, date, diff, diff -y, find, wc])<br><br></li></ul>
+
''Examples:''
  
|}
+
<span style="font-family:courier;font-weight:bold;">LS 2> /dev/null<br>ls > /dev/null<br>find / -name "tempfile" 2> /dev/null</span>
 +
<br><br>
  
= KEY CONCEPTS =
+
====The Here Document====
  
  
===Purpose of Having a Matrix Account===
+
[[Image:Here Document.png|thumb|right|175px|The '''Here Document''' allows a user to redirect stdin from <u>within</u> the command itself.]]
 +
In Linux, the '''Here Document''' allows a user to redirect stdin from within the command itself.
  
While attending Seneca College, you will be using many different computer systems to perform various operations.
+
''Example:''
Some of these servers include:
 
  
{|width="100%" cellspacing="0"
+
<span style="font-family:courier;font-weight:bold">cat <<+<br>Line 1<br>Line 2<br>Line 3<br>+<br><br>
  
|-
+
===Pipeline Commands===
| style="padding-left:0px;width:10%" |'''my.senecacollege.ca'''
 
| style="padding-left:15px;width:65%" |Learning Content Management System (Student Grades / Notes / Online Quizzes)
 
  
|-
+
[[Image:pipe-diagram-1.png|thumb|right|450px|A '''pipeline command''' sends a command's '''standard output''' directly to '''standard input''' of other command(s) without having to create temporary files.]]
| style="padding-left:0px;width:10%" |'''ict.senecacollege.ca'''
+
'''Pipeline Command:''' Having commands send their '''standard  output''' <u>directly</u> to '''standard input''' of other  commands WITHOUT having to use '''temporary''' files.
| style="padding-left:15px;width:65%" |Main ICT Webserver
 
  
|-
+
Pipes that are used in a '''pipeline command''' are represented by the '''pipe''' "|" symbol.<br>
| style="padding-left:0px;width:10%" |'''wiki.cdot.senecacollege.ca'''
 
| style="padding-left:15px;width:65%" |Course WIKIs for Seneca College Students
 
  
|-
+
A few simple commands can be '''combined''' to form a more <u>powerful</u> command line.<br>
| style="padding-left:0px;width:10%" |'''matrix.senecacollege.ca'''
 
| style="padding-left:15px;width:65%" |Linux Account for Student Practice and Assignment submission
 
  
|}
 
  
 +
Commands to the '''right''' of the pipe symbol are referred to as '''filters'''. They are referred to as ''filters'' since those commands are used to '''modify''' the stdout of the <u>previous command</u>. Many commands can be "piped" together, but these commands (filters) must be chained in a specific order, depending on what you wish to accomplish
  
[[Image:cli-prompt.png|thumb|right|120px|A '''shell''' is an interface / interpreter to allow a user to communicate with the Linux computer system. ]]
 
Although you need to study concepts throughout this course, you will also need to learn to '''issue and memorize simple as well as more advanced Linux commands'''. This requires that you practice issuing Linux commands on a frequent basis to become more comfortable when working the Unix and '''Linux command-line environments'''.
 
  
Just like with other operating systems like MS Windows, it is important to learn how to '''open a command prompt in order to issue commands to perform common tasks'''. In Unix/Linux, a '''shell''' is simply an interface / interpreter to allow a user to communicate with the Linux computer system (server). Linux shells have evolved (improved) over a period of time. You will be using the '''Bash Shell''' which is considered to be user-friendly. By understanding how to issue Linux commands, you can create a file consisting of Linux commands and programming techniques called a '''shell script''' (learned later in this course) to automate tasks to make you a more productive user and administrator.
+
''Examples:''<br>
 +
<span style="font-family:courier;font-weight:bold">ls -al | more<br>ls | sort -r<br>ls | sort | more<br>ls -l | cut -d" " -f2 | tr 'a-z' 'A-z"<br>ls | grep Linux | head -5<br>head -7 filename | tail -2</span>
  
===Layout of the Matrix Server===
+
====The tee Command====
[[Image:matrix-layout.png|thumb|right|350px|The Matrix server consists of several Virtual Computers all connected together to form a '''cluster'''. A cluster is a '''cost effective'''  alternative to larger servers. ]]
 
  
The Matrix server consists of '''several Virtual Computers connected together''' to form a '''cluster'''. A cluster is a '''cost effective''' alternative to buying larger servers.
 
  
All registered students in this course should have an account on the Matrix server.
+
[[Image:tee-diagram-1.png|thumb|right|250px|The '''tee''' utility can be used to '''split''' the flow of information. For example to save in a file as well as display on a screen. <br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
You will be using this account for the following reasons:
+
The '''tee''' utility can be used to <u>split</u> the flow of '''standard output'''<br>between a '''text file''' and the '''terminal screen'''.<br><br>
 +
The '''tee''' option '''-a''' can be used to add content to the '''bottom''' of an existing file<br>as opposed to ''overwriting'' the file's previous contents.
  
:* Issuing '''Linux commands'''
+
The reason for the name "'''tee'''" is that the splitting of the flow of information resembles a capital T.
:* Becoming productive using the Linux '''command prompt''' ('''shell''')
 
:* Performing '''Linux Practice Tutorials'''
 
:* Performing '''Linux Assignments''' ('''3''')
 
:* Practice Issuing '''Linux Command Review Questions'''
 
 
  
In the next investigation, you will learn how to '''connect''' to and '''login''' to your Matrix server account both at a '''college lab workstation''' and from a remote computer such as your '''home computer''' or your '''laptop computer'''.<br><br>
 
  
=INVESTIGATION 1: REDIRECTION INVOLVING REGULAR FILES=
+
''Examples:''
 +
<span style="font-family:courier;font-weight:bold">ls | tee unsorted.txt | sort<br>ls | grep Linux | tee matched.txt | more<br>ls | head -5 | tee -a listing.txt</span>
 +
<br><br>
  
<br>
+
===Multiple Commands Using Semicolon, Grouping, and Backquotes===
In this section, you will learn how to access your Matrix Linux account by two different methods:
+
 +
Besides piping, there are other ways that multiple commands may be placed in one line:<br>commands may be separated by '''semi-colons'''.<br>
  
:* From a <u>workstation</u> '''within''' a '''Seneca College lab'''
 
:* From your <u>own</u> computer '''within''' or '''outside''' '''Seneca College''' <br><br>
 
  
===Part 1: Access your Matrix Account from a Seneca College Lab ===
+
''Example:''
[[Image:apps-anywhere-main-window.png|thumb|right|250px|'''MyApps''' ('''AppsAnywhere''') Main Window ]]
 
You will learn how to connect to your Matrix Linux account from a workstation in your computer labs at Seneca college.
 
  
This would be useful if you do not have a notebook computer and are required to use one of the workstations available in your Seneca computer lab.
+
<span style="font-family:courier;font-weight:bold">sleep 5; ls</span> 
  
'''MyApps''' ('''AppsAnywhere''') is an application streaming service that lets you install software on any Seneca PC on demand. Any software application on the MyApps menu can be used on computers in classrooms, labs, and the Computing Commons.
 
  
Reference: https://inside.senecacollege.ca/its/software/myapps/
+
Multiple commands can also be '''grouped''' by using parentheses.
  
  
You will now use MyApps on your Windows workstation in your Seneca lab room to launch an application<br>to allow you to connect and login to your Matrix server account.
+
''Example:''
[[Image:apps-anywhere.png|thumb|right|250px|Several SSH applications will appear. We will use the application called '''SSH Client''' for this practice tutorial. ]]
 
  
 +
<span style="font-family:courier;font-weight:bold">(echo "Who is on:"; w) > whoson</span><br>('''''Note:''' <u>all</u> command output is sent to a file'')
  
'''Perform the Following Steps:'''
 
  
# Start your workstation in your lab and login to your Seneca Windows account.<br><br>
+
Commands may also be '''spread-out over multiple lines''', making it easier (for humans) to interpret a long command.<br><br>
# Make certain that the '''MyApps''' window is open. This window should have opened shortly after you logged into your Windows workstation. If the application windows is not open, click on the '''MyApps''' icon on the desktop to launch).<br><br>[[Image:ssh-main-window.png|thumb|right|150px|SSH Client Application Window ]]
+
The '''\''' symbol “''quotes-out''the meaning of the '''ENTER''' key as <u>text</u><br> (i.e. ''new-line'' as instead of ''running'' the command).
# Click on the '''Search Apps''' area located in the top right corner of the MyApps window and type the word: <span style="color:blue;font-weight:bold">ssh</span><br><br>
 
# Several SSH applications will appear. <br><br>'''NOTE:''' All of these applications allow you to connect to your Matrix account.<br>We will use the application called '''SSH Secure Shell Client''' for this practice tutorial.<br><br>
 
# Launch the '''SSH Secure Shell Client''' application icon to launch this program.<br><br>[[Image:quick-connect-window.png|thumb|right|150px|Quick Connect Dialog Box ]]'''NOTE:''' Prior to launching the application, you can add this to your '''favourites''' which will make it faster<br>to access this application in the future.<br><br>  
 
# The main SSH Client window will appear. Click on the '''Quick Connect''' button.<br><br>'''NOTE:''' The Connect dialog box allows the user to specify the '''server name''' and your '''account name'''<br>to allow you to connect to the server.<br><br>
 
# Click on the textbox labelled Hostnanme and enter the text: <span style="color:blue;font-weight:bold">matrix</span><br><br>'''NOTE:''' You can use the hostname '''matrix''' since you are located <u>inside</u> Seneca's network.<br>If you where located <u>outside</u> Seneca's network, then you would need to enter the full domain name: '''matrix.senecac.on.ca'''<br><br>[[Image:share-public-key.png|thumb|right|150px|Dialog Box to Share Public Key on Remote Server ]]
 
# When connecting securely for the '''first time''', a dialog box will appear to share a "public key" with your Matrix account in order to make your interaction between your workstation and the remote Linux server secure within the network (i.e. encrypted to prevent unauthorized access by other users)<br><br>'''FYI:''' To make communications over a network secure, a computer generates '''two keys''': a '''private key''' that is kept on the computer, and a '''public key''', which can be shared with other computers. Transmissions from your computer uses the private key to encrypt (scramble) transmission to the remote computer, which in turn, uses the public key to decrypt (unscramble) the transmission. Likewise, the remote computer uses the public key to encrypt (scramble) transmissions and your computer uses the private key to decrypt (unscramble) those transmissions.<br><br>[[Image:ssh-password-prompt.png|thumb|right|150px|Password Dialog Box ]]
 
# Click the '''Yes''' button to share your public key.<br><br>
 
# Next, a dialog box will appear prompting you for your Matrix account password in order to gain entry to your Matrix account. Your Matrix password is identical to your Seneca password. As you type the password, the text will be blocked-out or hidden to prevent others from viewing the screen to obtain your password[[Image:ssh-authentication-response.png|thumb|right|150px|Authentication Response Dialog Box ]]<br><br>
 
# Enter your password and click '''OK''' or press '''ENTER'''<br><br>[[Image:ssh-login-error.png|thumb|right|150px|SSH Login Error ]]
 
# Click '''OK''' or press '''ENTER''' at the '''Authentication Response''' dialog box.<br><br>'''NOTE:''' If you encounter an '''error message''', this can occur for several reasons:<ol><li>You mis-spelled the '''name of the server'''</li><li>You mis-spelled your Matrix '''username''' (same username that you connect to my.senecacollege.ca)</li><li>You have '''CAPS LOCK''' on by mistake (your username should be '''lowercase''' only).</li><li>You mis-spelled your '''password''' (same password that you connect to my.senecacollege.ca)</li></ol><br>If you encountered an error logging in, issue the same steps to connect to your Matrix account,<br>but carefully check your server name, username and your password. If you continue to experience the same problems,<br>you can go to the computer help desk in the learning commons to get help.<br><br>[[Image:ssh-shell-window.png|thumb|right|150px|Matrix Shell Command Line Interface ]]
 
#If you entered your password correctly, the main SSH Linux shell should appear where you enter Linux commands.<br><br>The '''Matrix''' server has been configured to allow users to '''only interact with the Linux OS by issuing commands'''. There are various reasons for this, but the main 2 reasons are to '''force students to learn how to issue Linux commands''' as well as '''limitations to remote access to a graphical Linux server''' due to large number of users (students).<br><br>Later in the course, your instructor may demonstrate other ways of accessing '''graphical versions of Linux''', and how to launch a shell terminal.<br><br>[[Image:ssh-settings-window.png|thumb|right|150px|SSH Settings Dialog Box ]]
 
  
You can '''configure''' the SSH application to adjust elements such as the '''window size''' and '''text font type''' and text '''font size'''. We will be showing you how to configure your SSH Secure Shell Client, but the other applications can allow you to configure their settings.<br><br>
 
  
'''Perform the Following Steps:'''
+
''Example:''
  
# Click on the '''Edit''' menu item, and then select '''Settings''' in the Edit drop-down menu.<br><br>
+
<span style="font-family:courier;font-weight:bold">echo "This will be split over multiple \<br>lines.  Note that the shell will realize \<br>that a pipe requires another command, so \<br>it will automatically go to the next line" |tr '[a-z]' '[A-Z]'</span>
# To change the size of your shell window, under the ''Global Settings'' section, click '''Appearance'''<br><br>
+
<br><br>
# Change the ''window size'' both horizontal and vertical to larger values.<br><br>
 
# To change the font size, under the ''Global Settings'' section, click '''Font'''<br><br>
 
# Select a larger ''font size'' and click '''OK'''<br><br>
 
# You now should notice that you have customized your SSH shell terminal. You can use the settings screen to also change your background and text colours if you are interested in doing so.<br><br>
 
# To exit your Matrix session, type the following command and press ENTER: <span style="color:blue;font-weight:bold">exit</span><br><br>'''NOTE:''' You should notice that the colour of the text changes slightly when disconnected and you are unable to issue Linux commands from that window.<br><br>
 
# After you have disconnected from your account, close the SSH Secure Shell Client application window.<br><br>
 
# It is recommended to repeat these steps with connecting to your Matrix account from a Seneca lab workstation until you become comfortable with the process.<br><br>
 
  
In the next section, you will learn how to connect to your Matrix server account from a different computer<br>(for example: your laptop computer at Seneca College or your computer at home).<br><br>
+
=INVESTIGATION 1: BASICS OF REDIRECTION=
  
===Part 2: Access your Matrix Account From your own computer within or outside the Seneca College Network ===
+
<span style="color:red;">'''ATTENTION''': This online tutorial will be required to be completed by '''Friday in week 6 by midnight''' to obtain a grade of '''2%''' towards this course</span><br><br>
  
You will learn how to connect to your Matrix Linux account from your own computer whether it be a '''laptop''' computer or your '''home computer'''.
+
In this investigation, you will learn how to redirect '''standard input''', '''standard output''' and '''standard error''' when issuing Unix / Linux commands.
  
This would be useful if you want to access your Matrix Linux account on your own computer to practice Linux commands or perform your assignment outside of the scheduled class-time.
 
  
We will be learning to use a different SSH application called "'''Putty'''". In this way, you will be exposed to a few SSH applications to help you  decide which SSH application you are most comfortable.
+
'''Perform the Following Steps:'''
  
:'''Perform the following steps:'''
+
# '''Login''' to your matrix account and issue a command to '''confirm''' you are located in your '''home''' directory.<br><br>
# Click on the following link to download the Putty executable file:<br>http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe[[Image:putty-application-icon.png|thumb|right|70px|Putty Application Icon ]]<br><br>'''NOTE:''' If you are interested in downloading and installing other SSH applications (including the<br>SSH Secure Shell Client application used in part 1), refer to week 1 links in the ULI101 Weekly Schedule.<br><br>
+
# Issue the following Linux command to create the following directory: <span style="color:blue;font-weight:bold;font-family:courier;">mkdir ~/redirect</span><br><br>
# The putty executable file does <u>not</u> install the Putty program: Instead, run this program, by opening a file manager, moving to the '''Downloads''' directory and double-clicking on the '''putty.exe''' file.<br><br>[[Image:putty-configuration-window.png|thumb|right|250px|Putty Configuration Window ]]
+
# Change to the '''~/redirect''' directory and confirm that you changed to that directory.<br><br>
# The Putty configuration window will appear.<br><br>You can '''configure''' the SSH application to adjust elements such as the '''window size''' and '''text font type''' and text '''font size'''. We will be showing you how to configure your SSH Secure Shell Client, but the other applications can allow you to configure their settings.<br><br>
+
# Use a text editor to create a file in your current directory called '''data.txt''' and 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</span><br><br>
# To change the size of your shell window, click '''Window'''<br><br>
+
# '''Save''' editing changes and '''exit''' the text editor.<br><br>
# Change the '''rows''' and '''columns''' to larger values.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' < data.txt</span><br><br>What does this command do?<br><br>
# To change the font size, under the ''Window'' section, click '''Appearance'''<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' < data.txt > output.txt</span><br><br>What does this command do? What are the contents of the file ''output.txt''?<br><br>
# In the left window under '''Font Settings''', click the '''Change''' button.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' > output.txt < data.txt</span><br><br>What does this command do? Is there any difference in terms of this command and the previous command issued?<br><br>
# Select a larger ''font size'' and click '''Apply'''<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tr 'a-z' 'A-Z' >> output.txt < data.txt</span><br><br>What happens to the content of the '''output.txt''' file? Why?<br><br>
# You will return to the Putty Configuration window.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tail -2 < data.txt > output.txt</span><br><br>What does this command do? Check the contents of the '''output.txt''' file to confirm.<br><br>
# To login to your Matrix Linux account, click on '''Sessions'''<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">tail -2 > output2.txt < data.txt </span><br><br>Why does this command render the same results as the previous command?<br>Try explaining how the command works in terms of '''stdin''' and then '''stdout'''.<br><br>
# In the Host Name sectionand enter the text: <span style="color:blue;font-weight:bold">matrix</span><br><br>'''NOTE:''' You can use the hostname '''matrix''' since you are located <u>inside</u> Seneca's network.<br>If you where located <u>outside</u> Seneca's network, then you would need to enter the full domain name: '''matrix.senecac.on.ca'''<br><br>[[Image:putty-login-window.png|thumb|right|350px|Login to Matrix Server ]]
+
# Issue the following Linux command to create a file: <span style="color:blue;font-weight:bold;font-family:courier;">cat > output3.txt </span><br><br>
# Click on the '''Open''' button and the Putty session window should appear.<br><br>
+
# Enter the follow text displayed below:<br><br><span style="font-family:courier;font-weight:bold;">This is the file output3.txt</span><br><br>
# Enter your '''username''' when prompted.<br><br>
+
# Press <span style="color:blue;font-weight:bold;font-family:courier;">ctrl-d</span> to exit the command.<br><br>
# Enter your '''password''' when prompted.<br><br>[[Image:putty-public-key.png|thumb|right|250px|Dialog Box to Share Public Key on Remote Server ]]
+
# Issue the '''cat''' command to view the contents of the file: '''output3.txt'''<br><br>
# When connecting securely from the putty application for the '''first time''', a dialog box will appear to share a "public key" with your Matrix account in order to make your interaction between your workstation and the remote Linux server secure within the network (i.e. encrypted to prevent unauthorized access by other users)<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp ~jason.carman/uli101/cars .</span><br><br>
# Click the '''Yes''' button to share your public key.<br><br>'''NOTE:''' If you encounter an '''error message''', this can occur for several reasons:<ol><li>You mis-spelled the '''name of the server'''</li><li>You mis-spelled your Matrix '''username''' (same username that you connect to my.senecacollege.ca)</li><li>You have '''CAPS LOCK''' on by mistake (your username should be '''lowercase''' only).</li><li>You mis-spelled your '''password''' (same password that you connect to my.senecacollege.ca)</li></ol><br>If you encountered an error logging in, issue the same steps to connect to your Matrix account,<br>but carefully check your server name, username and your password. If you continue to experience the same problems,<br>you can go to the computer help desk in the learning commons to get help.<br><br>[[Image:putty-shell-window.png|thumb|right|250px|Putty Shell Command Line Interface ]]
+
# Issue the '''cat''' command to view the contents of the '''cars''' file.<br><br>
# If you followed the login sequence correctly, the Putty Shell window will appear where you can issue Linux commands.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -c1-10 cars</span><br><br>What did this command do?<br><br>
# To exit your Matrix session, type the following command and press ENTER: <span style="color:blue;font-weight:bold">exit</span><br><br>'''NOTE:''' You should notice that the Putty application window will disappear.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f5 cars > field5.txt</span><br><br>What did this command do?<br>Check the contents in the file '''field5.txt''' to see what happened.<br><br>
# It is recommended to repeat these steps with connecting to your Matrix account from a Seneca lab workstation until you become comfortable with the process.<br><br>
+
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1-3 cars > field123.txt</span><br><br>What did this command do? (check file contents)<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1,5 cars > field15.txt</span><br><br>What did this command do? (check file contents)<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc cars > count.txt</span><br><br>What information does the '''count.txt''' file contain?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc -l cars > count1.txt</span><br><br>What information does the '''count1.txt''' file contain?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc -w cars > count2.txt</span><br><br>What information does the '''count2.txt''' file contain?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls -l > listing.txt</span><br><br>What information does the '''listing.txt''' file contain?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">pwd > listing.txt</span><br><br>What happenned to the original contents of the file called '''listing.txt'''? Why?<br><br>
 +
# Issue the following Linux command (use 2 greater-than signs): <span style="color:blue;font-weight:bold;font-family:courier;">date >> listing.txt</span><br><br>What information does the '''listing.txt''' file contain? Why?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars > combined.txt</span><br><br>What information does the '''combined.txt''' file contain? Why?<br><br>'''NOTE''': The '''cat''' command stands for "'''concatenate'''" which means to '''combine''' contents of multiple files into a single file.<br>This is why the command is called "''cat''".<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray 2> result.txt</span><br><br>What is displayed on the monitor? What information does the '''result.txt''' file contain? Why?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray > myoutput.txt 2> /dev/null</span><br><br>What is displayed on the monitor? What happened to the error message?<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cat listing.txt cars murray > myoutput.txt 2> result.txt</span><br><br>What is displayed on the monitor? what do those files contain? Why?<br><br>The '''Here Document''' allows you to redirect stdin from with the Linux command itself. Let's get some practice using the Here Document.<br><br>
 +
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">cat <<+<br>line 1<br>line 2<br>line 3<br>+</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;">grep 2 <<+<br>line 1<br>line 2<br>line 3<br>+</span><br><br>What do you notice? How does this differ from the previous command? Why?<br><br>
 +
# Issue the following Linux command:<br><span style="color:blue;font-weight:bold;font-family:courier;">grep 2 > line2.txt <<+<br>line 1<br>line 2<br>line 3<br>+</span><br><br>What do you notice? What is contained in the file '''line2.txt'''? Why?<br><br>'''NOTE:''' You will now run a shell script to confirm that you properly issued Linux commands using redirection.<br><br>
 +
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week5-check-1</span><br><br>
 +
# If you encounter errors, make corrections and '''re-run''' the checking script until you receive a congratulations message, then you can proceed.<br><br>
 +
# Issue the '''ls''' command to see all of the '''temporary files''' that were created as a result of redirection.<br><br>The problem with using these redirection symbols is that you create '''temporary text files''' that take up '''space''' on your file system.<br><br>
 +
# Issue a Linux command (using '''Filename Expansion''') to '''remove''' those temporary text files in the current directory.<br><br>
 +
# Issue the following Linux command to check that you removed ALL of those temporary text files:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week5-check-2</span><br><br>
 +
# If you encounter errors, make corrections and '''re-run''' the checking script until you receive a congratulations message, then you can proceed.<br><br>
  
In the next investigation, you will learn how to issue Linux commands and how to setup and perform the first two sections of the online ULI101 assignment #1.<br><br>
+
:In the next investigation, you will be learning how to issue '''pipeline Linux commands''' which can<br>accomplish tasks <u>without</u> creating temporary files.<br><br>
  
=INVESTIGATION 2: REDIRECTION USING PIPES =
+
=INVESTIGATION 2: REDIRECTION USING PIPELINE COMMANDS =
  
<br>
+
In this investigation, you will learn to issue '''pipeline commands''' to to accomplish tasks <u>without</u> having to generate temporary files.
In this section, you will learn how to ...
 
  
  
Line 205: Line 242:
 
'''Perform the Following Steps:'''
 
'''Perform the Following Steps:'''
  
# x<br><br>
+
# Confirm that you are still located in the '''~/redirect''' directory.<br><br>The '''problem''' with creating temporary files, is that they take up space on your server,<br>and should be removed. You actually did that in the previous investigation.<br><br>You will be issuing a '''pipeline command''' which will use the pipe symbol "|"<br>that will send the stdout from a command as stdin into another command<br><u>without</u> having to create temporary files.<br><br>
 +
# Issue the follow Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin | more</span><br><br>What happened? Press '''q''' to exit display.<br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin | who</span><br><br>What happened? Although this pipeline command provides output,<br>it '''does <u>not</u> work''' properly as a pipeline command since the '''who''' command is<br>'''NOT''' designed to accept standard input.[[Image:pipe-diagram-1.png|thumb|right|350px|]]<br><br>'''NOTE:''' When issuing pipeline commands, commands to the right of the pipe symbol must be designed to <u>accept</u> '''standard input'''. Since the ''who'' command does not, you did NOT see the contents of the '''/bin''' directory but only information relating to the ''who'' command. Therefore, the '''order''' of which you build your pipeline command and the '''type of command''' that is used as a ''filter'' is extremely important!<br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? > listing.txt</span><br><br>
 +
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort  listing.txt</span><br><br>
 +
# Issue the following Linux command to remove the listing file: <span style="color:blue;font-weight:bold;font-family:courier;">rm listing.txt</span><br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort </span><br><br>You should notice that the output from this pipeline command is the same output<br>from the command you issued in '''step #5'''.<br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort | more</span><br><br>What is difference with this pipeline command as opposed to the <u>previous</u> pipeline command? Press '''q''' to exit display.<br><br>
 +
# Issue the '''ls''' command.<br><br>You should notice that '''no files have been created'''.<br>Let's get practice issuing more pipeline commands using commands<br>(previously learned or new) to be used as '''filters'''.<br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort | head -5</span><br><br>What did you notice?<br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | sort | grep r | tail -2</span><br><br>What did you notice? Could you predict the output prior to issuing this pipeline command?<br><br>
 +
# Issue the following Linux '''pipeline command''': <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | sort | grep r | cut -c1-6</span><br><br>Try to explain step-by-step each process in the pipeline command (including ''filters'')<br>to explain the final output from this pipeine command.<br><br>
 +
# Confirm that you are still located in the '''~/redirect''' directory.<br><br>
 +
# Issue the following Linux '''pipeline command''':<br><span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | tee unsort.txt | sort | tee sort.txt | grep r | tee match.txt | head </span><br><br>
 +
# Issue the '''ls''' command to view the contents of this redirectory.<br><br>What did you notice?<br><br>
 +
# View the <u>contents</u> of the '''text files''' that were created to see how the '''tee''' command<br>was used in the previous pipeline command.<br><br>What was the purpose of using the '''tee''' command for this pipeline command?<br><br>You will now run a shell script to confirm that you properly issued that Linux pipeline command<br>using the '''tee''' command and redirection.<br><br>
 +
# Issue the following Linux command to run a checking script:<br><span style="color:blue;font-weight:bold;font-family:courier;">~uli101/week5-check-3</span><br><br>If you encounter errors, make corrections and '''re-run''' the checking script until you receive<br>a congratulations message, then you can proceed.<br><br>
 +
# Change to <u>your</u> '''home''' directory.<br><br>
 +
# Remove the '''~/redirect''' directory and its contents.<br><br>
 +
 
 +
:In the next investigation, you will learn various techniques to issue '''multiple Linux commands'''<br>on the same line, or issue a '''single Linux command over multiple lines'''.
 +
<br><br>
  
 +
=INVESTIGATION 3: ISSUING MULTIPLE UNIX/LINUX COMMANDS=
  
===Part 2: Performing Online Assignments ===
+
In this investigation, you will learn how to issue multiple Unix / Linux commands in a single line or over multiple lines.
  
You are required to perform '''3 online assignments''' during this course. This online assignment is used to teach and reinforce Linux commands are techniques as well as using Linux commands to perform tasks and tests students on their knowledge of issuing Linux commands.
 
  
In this section, you will learn to '''select the correct section''' for your online assignments, how to perform the '''first two sections''' of assignment #1, how to '''verify''' that you successfully completed both sections, and how to '''exit''' your online assignment #1.
+
'''Perform the Following Steps:'''
  
<br>
+
# Confirm you are located in your '''home''' directory in your Matrix account.<br><br>
'''Perform the following steps:'''
+
# Issue the following Linux commands (using the ''semicolon'' character "''';'''" to separate <u>each</u> Linux command):<br><span style="color:blue;font-weight:bold;font-family:courier;">cal;pwd;date</span><br><br>Note the output as well as the <u>order</u> of what each Linux command results.<br><br>
[[Image:assign-initial-window.png|thumb|right|250px|Initial Assignment Window to Allow Student to Select Correct Course Section. ]]
+
# Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">(cal;pwd;date)</span><br><br>Was there any difference in the output of this command as opposed to the previous command?<br><br>Let's see how grouping affects working with redirection.<br><br>
# In your Matrix account, issue the following command:<br><span style="color:blue;font-weight:bold;font-family:courier">~uli101/assign1</span><br><br>
+
# Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">cal;pwd;date > output.txt</span><br><br>What happened? Where is the output for the '''date''' command?<br>Why isn't the output for the '''cal''' and '''pwd''' commands are NOT contained in that file?<br><br>
# A screen similar to the one displayed on the right will appear.<br><br><span style="color:red;">'''WARNING''': You need to select the CORRECT section for the course which you belong to.<br>If you do NOT select your correct section, your assignment may not be recorded for marks!</span><br><br>
+
# Issue a Linux command to view the contents of the file called '''output.txt'''<br><br>What do you notice?<br><br>Let's use '''grouping''' to make modification to the previous command<br><br>
# Select the '''letter(s)''' corresponding to the <u>correct</u> '''ULI101 section''' and '''professor''' and press <span style="color:blue;font-weight:bold">ENTER</span><br><br>
+
# Issue the following Linux commands: <span style="color:blue;font-weight:bold;font-family:courier;">(cal;pwd;date) > output.txt</span><br><br>What did you notice?<br><br>
# After you have selected your course section, There will be a screen that provides several important notes before proceeding.<br>Please take a few moments to read those notes and press <span style="color:blue;font-weight:bold">ENTER</span> to proceed.<br><br>[[Image:assn-section-completion.png|thumb|right|250px|Main Assignment Window to View Which Sections Need to be Completed. ]]
+
# Issue a Linux command to view the contents of the file called '''output.txt'''<br><br>What does ''grouping'' do when issuing multiple Linux commands (separated by a semi-colon ";") that uses redirection?<br><br>
# The assignment main menu will then be displayed.<br><br>'''NOTES:'''<ul><li>Near the bottom of the window, you will see '''"You are currently registered to"''' followed by the '''section letter''' and '''instructor name'''. Double-check your course timetable to confirm that this the correct section letter. If you have selected the WRONG section, type  <span style="color:blue;font-weight:bold">C</span> in the menu selection area and press <span style="color:blue;font-weight:bold">ENTER</span>. You will return back to the original window to select your correct course section.<br><br></li><li>Near the top of the window displays the sections to complete in the assignment. You are NOT required to complete all sections at the same time. You can check the assignment #1 link on the ULI101 main WIKI page to note the due date for assignment #1. Sections that are NOT completed will be displayed in '''reverse text with a blue background'''. On the other hand, when you compete a section, then the section will appear as regular text<br>(i.e not in reverse text).<br><br>[[Image:assn-completed-window.png|thumb|right|250px|Main Assignment Window to Confirm Which Sections Have or Have Not Been Completed. ]]</li><li>If you want to verify that you have completed sections for the assignment, look for the text '''"Marks earned so far for ULI101 Assignment:"''' and it will show how many sections have been completed. When it shows all sections completed, then your assignment is finished and you should receive full marks provided that you have selected your correct course section.<br><br></li></ul>
+
# Issue the following Linux pipeline command (using \ at the end of most lines):<br><span style="color:blue;font-family:courier;font-weight:bold">echo "This will be split over multiple \<br>lines.  Note that the shell will realize \<br>that a pipe requires another command, so \<br>it will automatically go to the next line" |tr '[a-z]' '[A-Z]'</span><br><br>Did the command work? What is the purpose of issuing a Linux command in this way?<br><br>
# At the menu assignment #1 window, enter <span style="color:blue;font-weight:bold">1</span> in the menu selection area to begin the first section called '''"Introduction to Unix Commands"'''.<br><br>
+
# Complete the Review Questions sections to get additional practice.
# When you have completed this section, you will be returned to the main window.<br>View the contents of this window to confirm that you completed this section.<br><br>
+
<br><br>
# When you have time this week, enter <span style="color:blue;font-weight:bold">2</span> in the menu selection area to begin the first section called '''"Basic Unix Commands"'''.<br><br>
 
# When you have completed this section, you will be returned to the main window.<br>View the contents of this window to confirm that you completed this section.<br><br>'''NOTE:''' Although you are NOT allowed to use reference sheets for course evaluations, it is recommended to create one for your personal use, and write down Linux commands, their arguments and options, so your can successfully perform quizzes, midterm exam, and final exam.<br><br>
 
# Proceed to the next section to perform additional practice. Linux is about "doing"! The more practice questions you perform, the better mark you will achieve for this course and have a better foundation of issuing Linux commands to be more productive in your future courses.<br><br>
 
  
 
= LINUX PRACTICE QUESTIONS =
 
= LINUX PRACTICE QUESTIONS =
Line 235: Line 290:
 
simulate a quiz:
 
simulate a quiz:
  
https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week5_practice.docx
+
https://wiki.cdot.senecacollege.ca/uli101/files/uli101_week5_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).
 +
 +
When answering Linux command questions, refer to the following Inverted Tree Diagram. The linux directory is contained in your home directory. Assume that you just logged into your Matrix account. Directories are <u>underlined</u>.
 +
 +
[[Image:week5-dir.png|thumb|left|300px|]]
 +
 +
<br><br><br><br><br><br><br><br><br>
  
  
 
'''Review Questions:'''
 
'''Review Questions:'''
  
# x
+
# Write a single Linux command to provide a detailed listing of all files in the '''/etc''' directory, sending the output to a file called listing.txt in the “'''projects'''” directory (append output to existing file and use a relative pathname)
# x
+
# Write a single Linux command to redirect the stderr from the command:<br>'''cat a.txt b.txt c.txt''' to a file called '''error.txt''' contained in the “'''assignments'''” directory. (overwrite previous file’s contents and use only relative pathnames)
# x
+
# Write a single Linux command: '''cat ~/a.txt ~/b.txt ~/c.txt''' and redirect stdout to a file called “good.txt” to the “tests” directory and stderr to a file called “'''bad.txt'''” to the “'''tests'''” directory. (overwrite previous contents for both files and use only relative-to-home pathnames).
# x
+
# Write a single Linux command to redirect the stdout from the command:<br>'''cat a.txt b.txt c.txt''' to a file called wrong.txt contained in the “'''projects'''” directory and throw-out any standard error messages so they don’t appear on the screen (append output to existing file and use only relative pathnames).<br><br>
# x
+
# Write a single Linux '''pipeline command''' to display a detailed listing of the '''projects''' directory but pause one screen at a time to view and navigate through all of the directory contents. Use a relative-to-home pathname.
# x
+
# Write a single Linux '''pipeline command''' to display the sorted contents (in reverse alphabetical order) of the “'''linux'''” directory. Use a relative pathname.
# x
+
# Assume that the text file called “'''.answers.txt'''” contains 10 lines. Write a single Linux pipeline command to only displays lines 5 through 8 for this file. Use only relative pathnames.
# x
+
# Write a single Linux '''pipeline command''' to only display the contents of the “'''assignments'''” directory whose filenames match the pattern “'''murray'''” (both upper or lowercase). Use an absolute pathname.
# Create a '''table''' listing each Linux command, useful options that were mentioned in the online assignment #1 and command purpose for the following Linux commands: '''x''' , '''x''' , '''x''' , '''x''' , '''x''' , '''x''' , '''x''' , '''x''' , '''x''' , '''x'''
+
# Write a single Linux '''pipeline command''' to display the number of characters contained in the file called “'''.answers.txt'''”. Use a relative-to-home pathname.
 +
# Write a single Linux '''pipeline command''' to display the number of lines contained in the file called “'''questions.txt'''”. Use a relative pathname.
 +
# Write a single Linux '''pipeline command''' to display only the first 10 characters of each filename contained in your current directory. Also, there is will be a lot of output, so also pause at each screenful so you can navigate throughout the display contents. Use a relative pathname.
 +
# Create a '''table''' listing each Linux command, useful options that were mentioned in this tutorial for the following Linux commands: '''cut''' , '''tr''' , '''wc''' , and '''tee'''.
 +
 
 +
 
 +
 
 +
_________________________________________________________________________________
 +
 
 +
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

Stop (medium size).png
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/tutorial5
Andrew's students please go here: http://wiki.littlesvr.ca/wiki/OPS145_Lab_5

REDIRECTION: STANDARD INPUT / STANDARD OUTPUT / STANDARD ERROR


Main Objectives of this Practice Tutorial

  • Understand and use the cut, tr, and wc Linux commands
  • Define the terms Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr)
  • Understand and use the >, >>, 2>, 2>> symbols with Linux commands
  • Understand the purpose of the the /dev/null file and the Here Document
  • Define the term pipeline command and explain how a pipeline command functions
  • Define the term filter and how it relates to pipeline commands
  • Use the semicolon ";" and grouping "( )" symbols to issue multiple Unix / Linux commands on a single line
  • Use the backslash "\" symbol to spread-out long Unix/Linux commands over multiple lines


Tutorial Reference Material

Course Notes
Linux Command / Shortcut Reference
YouTube Videos
Slides:


Redirection:

Multiple Commands:

Redirection Filters: Brauer Instructional Videos:

KEY CONCEPTS

Additional File Manipulation Commands

Before proceeding, let's look at some additional commands used to manipulate content of text files.

Refer to the table below regarding these text file manipulation commands:

Manipulation-commands.png










Redirection (Standard Input, Standard Output, Standard Error)

Redirection can be defined as changing the way from where commands read input to where commands sends output.
You can redirect input and output of a command.

Reference: https://www.javatpoint.com/linux-input-output-redirection

The standard input (stdin) symbol that describes where a Unix/Linux command receives input

Standard input (stdin) is a term which describes from where a command receives input.
This would apply only to Unix/Linux commands that accept stdin input
(like cat, more, less, sort, grep, head, tail, tr, cut, wc, etc.).

Examples:

tr 'a-z' 'A-Z' < words.txt
cat < abc.txt
sort < xyz.txt


The standard out (stdout) symbol with one greater than sign overwrites existing file content with command output
The standard output (stdout) symbol with two greater than signs add command's output to bottom of existing file's contents.

Standard output (stdout) describes where a command sends its output.
In the examples below, output from a command is sent to the monitor, unless it is sent to a text file.


Examples:

ls -l
ls -l > detailed-listing.txt
ls /bin >> output.txt


The standard error (sterr) symbol with one greater than sign overwrites existing file content with command's error message.
The standard error (stderr) symbol with two greater than signs add command's error message to bottom of existing file's contents.

Standard Error (stderr) describes where a command sends it's error messages. In the examples below we issue the pwd in capitals on purpose to generate an error message, which can be redirected to a text file.


Examples:

PWD
PWD 2> error-message.txt
PWD 2 >> error-messages.txt
PWD 2> /dev/null


The /dev/null File

The /dev/null file (sometimes called the bit bucket or black hole) is a special system file
that discard all data written into it. This is useful to discard unwanted command output.


Examples:

LS 2> /dev/null
ls > /dev/null
find / -name "tempfile" 2> /dev/null


The Here Document

The Here Document allows a user to redirect stdin from within the command itself.

In Linux, the Here Document allows a user to redirect stdin from within the command itself.

Example:

cat <<+
Line 1
Line 2
Line 3
+

Pipeline Commands

A pipeline command sends a command's standard output directly to standard input of other command(s) without having to create temporary files.

Pipeline Command: Having commands send their standard output directly to standard input of other commands WITHOUT having to use temporary files.

Pipes that are used in a pipeline command are represented by the pipe "|" symbol.

A few simple commands can be combined to form a more powerful command line.


Commands to the right of the pipe symbol are referred to as filters. They are referred to as filters since those commands are used to modify the stdout of the previous command. Many commands can be "piped" together, but these commands (filters) must be chained in a specific order, depending on what you wish to accomplish


Examples:
ls -al | more
ls | sort -r
ls | sort | more
ls -l | cut -d" " -f2 | tr 'a-z' 'A-z"
ls | grep Linux | head -5
head -7 filename | tail -2

The tee Command

The tee utility can be used to split the flow of information. For example to save in a file as well as display on a screen.
(Image licensed under cc)

The tee utility can be used to split the flow of standard output
between a text file and the terminal screen.

The tee option -a can be used to add content to the bottom of an existing file
as opposed to overwriting the file's previous contents.

The reason for the name "tee" is that the splitting of the flow of information resembles a capital T.


Examples: ls | tee unsorted.txt | sort
ls | grep Linux | tee matched.txt | more
ls | head -5 | tee -a listing.txt


Multiple Commands Using Semicolon, Grouping, and Backquotes

Besides piping, there are other ways that multiple commands may be placed in one line:
commands may be separated by semi-colons.


Example:

sleep 5; ls


Multiple commands can also be grouped by using parentheses.


Example:

(echo "Who is on:"; w) > whoson
(Note: all command output is sent to a file)


Commands may also be spread-out over multiple lines, making it easier (for humans) to interpret a long command.

The \ symbol “quotes-out” the meaning of the ENTER key as text
(i.e. new-line as instead of running the command).


Example:

echo "This will be split over multiple \
lines. Note that the shell will realize \
that a pipe requires another command, so \
it will automatically go to the next line" |tr '[a-z]' '[A-Z]'


INVESTIGATION 1: BASICS OF REDIRECTION

ATTENTION: This online tutorial will be required to be completed by Friday in week 6 by midnight to obtain a grade of 2% towards this course

In this investigation, you will learn how to redirect standard input, standard output and standard error when issuing Unix / Linux commands.


Perform the Following Steps:

  1. Login to your matrix account and issue a command to confirm you are located in your home directory.

  2. Issue the following Linux command to create the following directory: mkdir ~/redirect

  3. Change to the ~/redirect directory and confirm that you changed to that directory.

  4. Use a text editor to create a file in your current directory called data.txt and enter the following text displayed below:

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


  5. Save editing changes and exit the text editor.

  6. Issue the following Linux command: tr 'a-z' 'A-Z' < data.txt

    What does this command do?

  7. Issue the following Linux command: tr 'a-z' 'A-Z' < data.txt > output.txt

    What does this command do? What are the contents of the file output.txt?

  8. Issue the following Linux command: tr 'a-z' 'A-Z' > output.txt < data.txt

    What does this command do? Is there any difference in terms of this command and the previous command issued?

  9. Issue the following Linux command: tr 'a-z' 'A-Z' >> output.txt < data.txt

    What happens to the content of the output.txt file? Why?

  10. Issue the following Linux command: tail -2 < data.txt > output.txt

    What does this command do? Check the contents of the output.txt file to confirm.

  11. Issue the following Linux command: tail -2 > output2.txt < data.txt

    Why does this command render the same results as the previous command?
    Try explaining how the command works in terms of stdin and then stdout.

  12. Issue the following Linux command to create a file: cat > output3.txt

  13. Enter the follow text displayed below:

    This is the file output3.txt

  14. Press ctrl-d to exit the command.

  15. Issue the cat command to view the contents of the file: output3.txt

  16. Issue the following Linux command: cp ~jason.carman/uli101/cars .

  17. Issue the cat command to view the contents of the cars file.

  18. Issue the following Linux command: cut -c1-10 cars

    What did this command do?

  19. Issue the following Linux command: cut -f5 cars > field5.txt

    What did this command do?
    Check the contents in the file field5.txt to see what happened.

  20. Issue the following Linux command: cut -f1-3 cars > field123.txt

    What did this command do? (check file contents)

  21. Issue the following Linux command: cut -f1,5 cars > field15.txt

    What did this command do? (check file contents)

  22. Issue the following Linux command: wc cars > count.txt

    What information does the count.txt file contain?

  23. Issue the following Linux command: wc -l cars > count1.txt

    What information does the count1.txt file contain?

  24. Issue the following Linux command: wc -w cars > count2.txt

    What information does the count2.txt file contain?

  25. Issue the following Linux command: ls -l > listing.txt

    What information does the listing.txt file contain?

  26. Issue the following Linux command: pwd > listing.txt

    What happenned to the original contents of the file called listing.txt? Why?

  27. Issue the following Linux command (use 2 greater-than signs): date >> listing.txt

    What information does the listing.txt file contain? Why?

  28. Issue the following Linux command: cat listing.txt cars > combined.txt

    What information does the combined.txt file contain? Why?

    NOTE: The cat command stands for "concatenate" which means to combine contents of multiple files into a single file.
    This is why the command is called "cat".

  29. Issue the following Linux command: cat listing.txt cars murray 2> result.txt

    What is displayed on the monitor? What information does the result.txt file contain? Why?

  30. Issue the following Linux command: cat listing.txt cars murray > myoutput.txt 2> /dev/null

    What is displayed on the monitor? What happened to the error message?

  31. Issue the following Linux command: cat listing.txt cars murray > myoutput.txt 2> result.txt

    What is displayed on the monitor? what do those files contain? Why?

    The Here Document allows you to redirect stdin from with the Linux command itself. Let's get some practice using the Here Document.

  32. Issue the following Linux command:
    cat <<+
    line 1
    line 2
    line 3
    +


    What do you notice?

  33. Issue the following Linux command:
    grep 2 <<+
    line 1
    line 2
    line 3
    +


    What do you notice? How does this differ from the previous command? Why?

  34. Issue the following Linux command:
    grep 2 > line2.txt <<+
    line 1
    line 2
    line 3
    +


    What do you notice? What is contained in the file line2.txt? Why?

    NOTE: You will now run a shell script to confirm that you properly issued Linux commands using redirection.

  35. Issue the following Linux command to run a checking script:
    ~uli101/week5-check-1

  36. If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message, then you can proceed.

  37. Issue the ls command to see all of the temporary files that were created as a result of redirection.

    The problem with using these redirection symbols is that you create temporary text files that take up space on your file system.

  38. Issue a Linux command (using Filename Expansion) to remove those temporary text files in the current directory.

  39. Issue the following Linux command to check that you removed ALL of those temporary text files:
    ~uli101/week5-check-2

  40. If you encounter errors, make corrections and re-run the checking script until you receive a congratulations message, then you can proceed.

In the next investigation, you will be learning how to issue pipeline Linux commands which can
accomplish tasks without creating temporary files.

INVESTIGATION 2: REDIRECTION USING PIPELINE COMMANDS

In this investigation, you will learn to issue pipeline commands to to accomplish tasks without having to generate temporary files.


Perform the Following Steps:

  1. Confirm that you are still located in the ~/redirect directory.

    The problem with creating temporary files, is that they take up space on your server,
    and should be removed. You actually did that in the previous investigation.

    You will be issuing a pipeline command which will use the pipe symbol "|"
    that will send the stdout from a command as stdin into another command
    without having to create temporary files.

  2. Issue the follow Linux pipeline command: ls /bin | more

    What happened? Press q to exit display.

  3. Issue the following Linux pipeline command: ls /bin | who

    What happened? Although this pipeline command provides output,
    it does not work properly as a pipeline command since the who command is
    NOT designed to accept standard input.
    Pipe-diagram-1.png


    NOTE: When issuing pipeline commands, commands to the right of the pipe symbol must be designed to accept standard input. Since the who command does not, you did NOT see the contents of the /bin directory but only information relating to the who command. Therefore, the order of which you build your pipeline command and the type of command that is used as a filter is extremely important!

  4. Issue the following Linux command: ls /bin/?? > listing.txt

  5. Issue the following Linux command: sort listing.txt

  6. Issue the following Linux command to remove the listing file: rm listing.txt

  7. Issue the following Linux pipeline command: ls /bin/?? | sort

    You should notice that the output from this pipeline command is the same output
    from the command you issued in step #5.

  8. Issue the following Linux pipeline command: ls /bin/?? | sort | more

    What is difference with this pipeline command as opposed to the previous pipeline command? Press q to exit display.

  9. Issue the ls command.

    You should notice that no files have been created.
    Let's get practice issuing more pipeline commands using commands
    (previously learned or new) to be used as filters.

  10. Issue the following Linux pipeline command: ls /bin/?? | sort | head -5

    What did you notice?

  11. Issue the following Linux pipeline command: ls /bin/???? | sort | grep r | tail -2

    What did you notice? Could you predict the output prior to issuing this pipeline command?

  12. Issue the following Linux pipeline command: ls /bin/???? | sort | grep r | cut -c1-6

    Try to explain step-by-step each process in the pipeline command (including filters)
    to explain the final output from this pipeine command.

  13. Confirm that you are still located in the ~/redirect directory.

  14. Issue the following Linux pipeline command:
    ls /bin/???? | tee unsort.txt | sort | tee sort.txt | grep r | tee match.txt | head

  15. Issue the ls command to view the contents of this redirectory.

    What did you notice?

  16. View the contents of the text files that were created to see how the tee command
    was used in the previous pipeline command.

    What was the purpose of using the tee command for this pipeline command?

    You will now run a shell script to confirm that you properly issued that Linux pipeline command
    using the tee command and redirection.

  17. Issue the following Linux command to run a checking script:
    ~uli101/week5-check-3

    If you encounter errors, make corrections and re-run the checking script until you receive
    a congratulations message, then you can proceed.

  18. Change to your home directory.

  19. Remove the ~/redirect directory and its contents.

In the next investigation, you will learn various techniques to issue multiple Linux commands
on the same line, or issue a single Linux command over multiple lines.



INVESTIGATION 3: ISSUING MULTIPLE UNIX/LINUX COMMANDS

In this investigation, you will learn how to issue multiple Unix / Linux commands in a single line or over multiple lines.


Perform the Following Steps:

  1. Confirm you are located in your home directory in your Matrix account.

  2. Issue the following Linux commands (using the semicolon character ";" to separate each Linux command):
    cal;pwd;date

    Note the output as well as the order of what each Linux command results.

  3. Issue the following Linux commands: (cal;pwd;date)

    Was there any difference in the output of this command as opposed to the previous command?

    Let's see how grouping affects working with redirection.

  4. Issue the following Linux commands: cal;pwd;date > output.txt

    What happened? Where is the output for the date command?
    Why isn't the output for the cal and pwd commands are NOT contained in that file?

  5. Issue a Linux command to view the contents of the file called output.txt

    What do you notice?

    Let's use grouping to make modification to the previous command

  6. Issue the following Linux commands: (cal;pwd;date) > output.txt

    What did you notice?

  7. Issue a Linux command to view the contents of the file called output.txt

    What does grouping do when issuing multiple Linux commands (separated by a semi-colon ";") that uses redirection?

  8. Issue the following Linux pipeline command (using \ at the end of most lines):
    echo "This will be split over multiple \
    lines. Note that the shell will realize \
    that a pipe requires another command, so \
    it will automatically go to the next line" |tr '[a-z]' '[A-Z]'


    Did the command work? What is the purpose of issuing a Linux command in this way?

  9. Complete the Review Questions sections to get additional practice.



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_week5_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).

When answering Linux command questions, refer to the following Inverted Tree Diagram. The linux directory is contained in your home directory. Assume that you just logged into your Matrix account. Directories are underlined.

Week5-dir.png











Review Questions:

  1. Write a single Linux command to provide a detailed listing of all files in the /etc directory, sending the output to a file called listing.txt in the “projects” directory (append output to existing file and use a relative pathname)
  2. Write a single Linux command to redirect the stderr from the command:
    cat a.txt b.txt c.txt to a file called error.txt contained in the “assignments” directory. (overwrite previous file’s contents and use only relative pathnames)
  3. Write a single Linux command: cat ~/a.txt ~/b.txt ~/c.txt and redirect stdout to a file called “good.txt” to the “tests” directory and stderr to a file called “bad.txt” to the “tests” directory. (overwrite previous contents for both files and use only relative-to-home pathnames).
  4. Write a single Linux command to redirect the stdout from the command:
    cat a.txt b.txt c.txt to a file called wrong.txt contained in the “projects” directory and throw-out any standard error messages so they don’t appear on the screen (append output to existing file and use only relative pathnames).

  5. Write a single Linux pipeline command to display a detailed listing of the projects directory but pause one screen at a time to view and navigate through all of the directory contents. Use a relative-to-home pathname.
  6. Write a single Linux pipeline command to display the sorted contents (in reverse alphabetical order) of the “linux” directory. Use a relative pathname.
  7. Assume that the text file called “.answers.txt” contains 10 lines. Write a single Linux pipeline command to only displays lines 5 through 8 for this file. Use only relative pathnames.
  8. Write a single Linux pipeline command to only display the contents of the “assignments” directory whose filenames match the pattern “murray” (both upper or lowercase). Use an absolute pathname.
  9. Write a single Linux pipeline command to display the number of characters contained in the file called “.answers.txt”. Use a relative-to-home pathname.
  10. Write a single Linux pipeline command to display the number of lines contained in the file called “questions.txt”. Use a relative pathname.
  11. Write a single Linux pipeline command to display only the first 10 characters of each filename contained in your current directory. Also, there is will be a lot of output, so also pause at each screenful so you can navigate throughout the display contents. Use a relative pathname.
  12. Create a table listing each Linux command, useful options that were mentioned in this tutorial for the following Linux commands: cut , tr , wc , and tee.


_________________________________________________________________________________

Author: Murray Saul

License: LGPL version 3 Link: https://www.gnu.org/licenses/lgpl.html

_________________________________________________________________________________