Pipes and redirection

Video 107 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:35, with English and Hindi audio.

Commands print text, and the shell can send that text somewhere else. You can write it into a file, add it to the end of a file, or pass it straight into another command. These small moves let simple commands combine into a useful job, so you rarely need a new program. This article explains the three symbols, shows what each one does with real output, and points out where the mistakes happen.

What you will learn

Three ways to move the text a command prints

Output is text, and the shell can send it to a file or to another program. Three symbols do that job, and each one suits a different case.

Writing to a file with one sign or two

Write one line into note.txt, replacing its old contents
echo hello > note.txt
Add one line to the end of note.txt
echo world >> note.txt

The file now holds both lines. Reading it with cat shows hello and then world.

Note: One sign is dangerous with an existing file, because it replaces the old contents at once. Two signs are safer for logs, because they keep what was there.

Passing output into another command with a pipe

A pipe carries the lines from one program straight into another program, with no file in between. Here the wc command counts the lines it receives, and prints a single number.

Count the lines that cat prints
cat note.txt | wc -l

Piping into sort with the r option puts the lines in reverse order, so world now comes before hello.

Sort the lines in reverse order
cat note.txt | sort -r

Sending error messages to their own file

Errors travel on their own channel, separate from normal output. Redirect them on their own, so the screen and the file each get what they need. Two and a greater-than sign send the error messages into a file.

Send the error message into errors.txt
cat missing.txt 2> errors.txt

Note: The error message goes into errors.txt, not onto the screen, so the message is kept for reading.

Try it yourself

You need a Linux terminal and a scratch folder. Run the commands in order, and check where each output ends up: the screen or a file.

  1. Write one line into note.txt. One greater-than sign replaces what was in the file.

    echo hello > note.txt
  2. Add a second line to the end of the file. Two greater-than signs keep the first line.

    echo world >> note.txt
  3. Show the file, to check that both lines are there.

    cat note.txt

    What you should see: The shell prints hello, then world.

  4. Count the lines. The output of cat goes into wc through a pipe.

    cat note.txt | wc -l

    What you should see: The shell prints 2.

  5. Sort the lines in reverse order, passing them through a pipe into sort.

    cat note.txt | sort -r

    What you should see: The shell prints world, then hello.

  6. Try to read a file that does not exist, and send the error message into a file.

    cat missing.txt 2> errors.txt

    What you should see: Nothing appears on the screen. The error message is written into errors.txt.

Terms

Redirection
Sending a command's output to a file instead of the screen, with a greater-than sign.
Pipe
The vertical bar that sends one command's output straight into another command as its input.

Check yourself

What is the difference between one greater-than sign and two?

One sign replaces the file's old contents with the new output. Two signs add the new output to the end and keep what was there.

What does a pipe do?

It sends the output of one command straight into another command, with no file in between.

How can you keep error messages off the screen?

Redirect them with 2 and a greater-than sign. The error messages then go into their own file.

Previous: Finding files and programs. Next: Environment variables and PATH.