Output Redirection and Pipes

Goal: Introduce how to pipe the output of one command into the input of another, and how to redirect the output of a command into a file.



Output Redirection and Pipes

  • Redirection of output: > vs >>

    • redirect sends a channel of output to a file.

    • A channel refers to standard input/output as well as standard error (not covered here).

    • You can redirect a file as input to a command using < and << (not covered here).

  • Using pipe “|’

    • A pipe passes standard output as the standard input to another command

  • Examples of the form: 

    • View a text file and pipe to the grep command to filter lines by looking for text.

    • Cat a list and sort by line.

    • Sort and then find unique items.

    • View folder contents and look for a specifically named filename.


Redirection of output: > vs >>

Remember we can use grep to search a file for some text.

This output is written to the command-line

[intro_to_linux]$ grep -i bayes software.csv

Redirect this output to a file:

[intro_to_linux]$ grep -i bayes software.csv > apps.txt [intro_to_linux]$ ls apps.txt clusters data software.csv [intro_to_linux]$ cat apps.txt
[intro_to_linux]$ grep -i IPA software.csv > apps.txt [intro_to_linux]$ cat apps.txt

Example: Using pipe “|” from a file


Example continued


Example continued


Example continued


Example continued


Example: Pipe from ls command


Exercises: Pipeline


Answers