Redirecting Output, Input, and Errors

Redirecting output can be useful if the output from a command or script is too large to be displayed, so we direct it to a file where we can view all of the contents.

Step-by-step guide

The main method is to push the output over to a file. There you can grep it, use head, tail or cat to view the contents of the file.

  1. First type the following into your command like
    1. This example will be Standard Output”. For this example, we’re going to use the echo command. and it will display what ever we want it to.
      1. In your terminal session type; echo “Hello World” and see what happens
      2. Standard Output writes data that is generated by a program, and displays the text to the terminal.
    2. Standard Input is where we the user enter data into a program. So, user to program. Most often, we use a keyboard for standard input.
    3. Standard Error is writes errors generated by a program that has failed. Like Standard Output, it is displayed in the user’s terminal.

      1. As you can see above, there was an error. The error, was displayed in my terminal session must like standard output.
  2. Redirecting Output, Input and Error to files
    1. The following commands will write the output, input or error to a file. If the file doesn’t exist, it will be created prior to writing it.
      1. if a file do not exist, then using a > or >> will cause the file to be created and then written to.
      2. Commands with a single bracket overwrite the destination’s existing contents.

        Overwrite

        • > – standard output
        • < – standard input
        • 2> – standard error

        Commands with a double bracket do not overwrite the destination’s existing contents.

        Append

        • >> – standard output
        • << – standard input
        • 2>> – standard error

      3. When you use cat to view the file, you can view the contents you has redirected to it.
  1. Filters
    1. Pipelines are often use to preform complex operations on data. This allows you to use multiple commands together, separated by “|”.
      • sort – Sort the printed output until its redirected
      • uniq – Report or Omit repeated lines
      • wc – Print Line, Word and Byte Counts
      • grep – Print lines matching a pattern
      • tee – read from stdin and output from stdout and files
    2. Example:
      1. ls % 2> error.txt | sort | uniq | wc
        1. So, command would redirect the error message to “error.txt”, then sort the error based on the output, delete any repeating lines and finally give you how man worst and bytes have been used.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s