CSC231 Bash Tutorial 4

From dftwiki3
Revision as of 14:36, 22 February 2017 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 14:13, 22 February 2017 (EST)


<showafterdate after="20170224 13:00" before="20170601 00:00">

Using the Vi Editor

Redirection


  • We have seen in the previous lab how to use pipes to take the output of a program and feed it to the input of another.
  • Redirection is similar, except that the input or the output is fed from, or fed to a file, respectively.


Example 1: storing the output of a command into a file


  • Try this:
last

this will list the last login times of all the users on Aurora. If you want to target a particular user, say 231b, you can try this:
last | grep "231b "               (make sure there's a space after the b)
  • Assume you want to store these login time into a file, so that you can share it with somebody else, or send as an attachment. You simply use the > sign to store the output of the last command into a file:
last | grep "231b "  >  logins231b.txt

  • You won't see any output, as it is being redirected. Look at the files in your directory and see that you have a new file called logins231b.txt.
  • Display the contents of this file with the cat or with the less command.


You now have a new way of storing the output of a program into a file.


Challenge #1:

QuestionMark1.jpg


Create a text file called "231Students.txt" that contains their last login information, but make sure the information from User 231b (your instructor) is not stored in the file.





Side-step: Sorting text


Linux sports a command to sort text. It's called "sort." To see how it works, try this:

last | sort

  • If you had wanted the output in reverse order, you could have typed:
last | sort -r

  • Sometimes the information returned by a linux command contains numbers at the beginning of the first line. du, which returns the "disk usage" of a directory, i.e. the amount of disk space taken by each file, is such a command. Let's try it:
du /etc

You get a list of all the files in the /etc directory, with their size at the beginning of each line.
  • Try to sort this list:
du /etc  | sort

  • See the problem? Sorting numbers alphabetically does not yield what we expect. Instead we can tell sort to sort numerically:
du /etc  | sort -n


Challenge #2:

QuestionMark2.jpg


Create a text file called "231Students.txt" using redirection, and make it contain the login information for 231 students, sorted in alphabetical order, but make sure the information from User 231b (your instructor) is not stored in the file.







Challenge #3:

QuestionMark3.jpg


Create a text file using redirection that contains only the 10 largest files found in the /etc/ directory. The file should be called to10etcFiles.txt.









</showafterdate>






<showafterdate after="2017024 11:30" before="20170601 00:00">

Challenge 1
last | grep "231b" | grep -v "231b "  > 231students.txt

Challenge 2
last | grep "231b" | grep -v "231b " | sort > 231students.txt
Challenge 3
du /etc | sort -n | tail -n 11 | head -n 10 > top10filesInEtc.txt

</showafterdate>