CSC231 Bash Tutorial 4

From dftwiki3
Revision as of 14:13, 22 February 2017 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <showafterdate after="20170224 13:00" before="20170601 00:00"> =Using the Vi Editor= =Redirection= <br /> * We have seen in the previous lab how to use '''pip...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

</showafterdate>