Changes

Jump to: navigation, search

Tutorial5: Redirection

1,764 bytes added, 09:33, 4 February 2020
INVESTIGATION 1: BASICS OF REDIRECTION
These commands are displayed in the table below:
<table cellpadding="5" width="5055%"><tr><th style="border-bottom: 1px solid black;text-align:left;">Linux Command</th><th style="border-bottom: 1px solid black;text-align:left;">Purpose</th></tr><tr valign="top"><td>'''cut'''</td><td>Used to extract fields and characters from records. The option '''-c''' option is used to cut by a character or a range of characters. The '''-f''' option indicates the field number or field range to display (this may require using the '''-d''' option to indicate the field separator (delimiter) which is tab by default.<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">cut -f2 filename</span> - extract 2nd field from all records in file<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f2,5 filename</span> - extract 2nd and 5th field<br><span style="font-family:courier;font-weight:bold;">cut -d' ' -f1-3,5 filename</span> - extract 1st to 3rd and 5th fields<br><span style="font-family:courier;font-weight:bold;">cut -c3-5 filename</span> - extract 3rd to 5th characters</td></tr><tr valign="top"><td>'''tr'''</td><td>Used to translate characters to different characters.<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">tr "[a-z]" "[A-Z]" < filename</span> - translate lowercase "a" through "z" lower to uppercaseupper case<br><span style="font-family:courier;font-weight:bold;">tr "a-z" "A-Z" < filename</span> - translate lowercase "a" through "z" to uppercase, different syntax same as above (non-System Vservers)<br><span style="font-family:courier;font-weight:bold;">tr ':' ' ' < filename</span> - translate all colons to spaces<br><span style="font-family:courier;font-weight:bold;">tr ' ' '\n' < filename</span> - translate all spaces to newline characters<br><span style="font-family:courier;font-weight:bold;">tr 'abc' 'A' < filename</span> - translate 'a', 'b', and 'c' to 'A', the last character in the "to" string repeats<br></td></tr><tr valign="top"><td><span style="font-family:courier;font-weight:bold;">wc</span></td><td>'''Displays various counts of the contents of a file.'''<br><br>''Examples:''<br><span style="font-family:courier;font-weight:bold;">wc -l filename</span> - displays number of lines in file<br><span style="font-family:courier;font-weight:bold;">wc -c filename</span> - displays number of characters in file<br><span style="font-family:courier;font-weight:bold;">wc -w filename</span> - displays number of words in fil<br></td></tr></table>
<br><br>
[[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.]]
'''Pipeline Command:''' Having commands send their s'''tandard output''' <u>directly</u> to '''standard input''' of other commands without WITHOUT having to use '''temporary ''' files.
A few simple commands can be '''combined ''' to form a more <u>powerful </u> command line.<br>
Pipes that are used in a '''pipeline command ''' are represented by the '''pipe''' "| " symbol. 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 stdin that was sent from 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
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 stdin that was sent from 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:''<br>
The '''tee''' utility can be used to split the flow of information. 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:''
Commands may also be '''split spread-out over multiple lines''', making it easier (for humans) to interpret a long command.<br>You can add a '''quote''' or backslash'''symbol "escape\" the newline character''' at the end of a line, to get rid of the special meaning<br>of newline (to end a command line)
# Save editing changes and exit the text editor.<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 this command do?<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 this command do? What are the contents of the file output.txt?<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 this command do? Is there any difference in terms of this command and the previous command issued?<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># 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>
# 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>
# 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>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cp ~murray.saul/uli101/cars .</span><br><br>
# Issue the '''cat''' command to view the contents of the '''cars''' file.<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -c1-10 uli101/cars</span><br><br>What did this command do?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f5 uli101/cars > field5.txt</span><br><br>What did this command do?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1-3 uli101/cars > field123.txt</span><br><br>What did this command do?<br><br># Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">cut -f1,5 uli101/cars > field15.txt</span><br><br>What did this command do?<br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">wc 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 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;">pwd > listing.txt</span><br><br>What happenned to the original contents of the file called '''listing.txt'''?<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?<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?<br><br>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".<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 information does the '''result.txt''' file contain?<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?<br><br>
 
:The problem with using redirection to create files, you have these files taking up space, which requires you remove them. In the next investigation, you will be learning how to issue pipeline commands which can provide information by issuing several Linux commands without creating temporary files.<br><br>
'''Perform the Following Steps:'''
# Confirm Change to your '''home''' directory and confirm that you are located now in your home directory.<br><br># Issue the '''ls''' command to view the contents of your '''~/redirect''' directory.<br><br>These are all temporary files that you created in your previous investigation.<br>The '''problem''' with creating temporary files, is that they take up space on your server,<br>and should be removed.<br><br># Issue the following Linux command to remove all files in your ''redirect'' directory: <span style="color:blue;font-weight:bold;font-family:courier;">rm * -r ~/redirect</span><br>and confirm that you have removed this directory and its contents.<br><br>'''NOTE: ''' 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><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?<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? Why is the result different than antipated?[[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 accept stdin. Since the who command does not, an error message was displayedyou 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;">cp /bin/?? > listing.txt</span><br><br>
# Issue the following Linux command: <span style="color:blue;font-weight:bold;font-family:courier;">sort -r 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-r</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 #6'''<br><br># Issue the following Linux pipeline command: <span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/?? | sort -r | more</span><br><br>What is different with this pipeline command as opposed to the previous pipeline command?<br><br># Issue the '''ls''' command.<br><br>You should notice that no files have been created. Let's get practice issuing more pipeline commands <br>using commands (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 -r | 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 -r | grep r | tail -2</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 -r | grep r | cut -1c1-3</span><br><br># Issue the following Linux pipeline command: <br><span style="color:blue;font-weight:bold;font-family:courier;">ls /bin/???? | tee unsorted.txt | sort -r | tee sorted.txt | tee unmatched.txt | grep r | tail -2</span><br><br>What did you notice?<br><br>
# Check the files that were created to see how the '''tee''' command was used in the previous pipeline command.<br><br>
# Change to your home directory.<br><br>
# Issue the following Linux '''rm''' command to remove the ~/redirect directory and its contents: <span style="color:blue;font-weight:bold;font-family:courier;"u>rm -r ~/redirectonly</spanu>remove the files '''unsorted.txt''' , '''sorted.txt''' , and '''unmatched.txt'''<br><br> 
:In the next investigation, you will learn various techniques to issue multiple Linux commands on the same line, or long Linux commands over multiple lines.
<br><br>
=INVESTIGATION 3: ISSUING MULTIPLE UNIX/LINUX COMMANDS=
13,420
edits

Navigation menu