Linux on call · Chapter 8 of 30

Pipes and redirection

By the end of this chapter you can send a command's results and its errors to different places, and join commands together with pipes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Files and text
  • 5 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Arjun left a script, stock.sh, that prints what is left in Tapri's kitchen. Ravi wants the stock in one file for the kitchen, the problems in another for him, and a history log that grows by one day each morning.

You already know >, which saves a command's output in a file. Bunty tried it: the warnings stayed on his screen and never reached the file. Then he saved today's report into the history log the same way, and yesterday's lines were gone. To do what Ravi asks, you need to know where a command's output really goes.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Save only the stock in stock.txt and throw the errors away

    Save the stock in a file, and throw the warnings away.

    1. Run ./stock.sh once and look at its eight lines.

    2. Run it again. Send its normal output (stream 1) to stock.txt.

    3. In the same command, send its errors (stream 2) to /dev/null. Write it as 2>/dev/null.

    4. Run cat stock.txt to see what you saved.

    To pass: stock.txt has only the five stock lines, in the order the script prints them, and nothing else. You ran a command with 2>/dev/null in the chapter folder, and it worked.

  2. Task 2

    Save only the errors in errors.txt

    Save only the warnings and the error in a file.

    1. Run ./stock.sh and send its errors (stream 2) to errors.txt.

    2. The stock may still show on the screen. You may also send it to /dev/null.

    3. Run cat errors.txt to see what you saved.

    To pass: errors.txt has only the three problem lines (two WARNING lines and one ERROR line), in the order the script prints them, and nothing else.

  3. Task 3

    Save both streams together in all.txt

    Save all of the script's lines, stock and problems, in one file.

    1. Run ./stock.sh and send stream 1 to all.txt.

    2. In the same command, after that, send stream 2 to the same place with 2>&1.

    3. Run cat all.txt to see what you saved.

    To pass: all.txt has all eight lines, in the same order the script prints them on the screen, and nothing else.

  4. Task 4

    Add today's stock to the end of stock-history.log

    Add today's stock to the history file, and keep yesterday's lines.

    1. Run cat stock-history.log. It has yesterday's three lines.

    2. Run ./stock.sh once and add its stock to the end of stock-history.log. >> adds to the end of a file.

    3. In the same command, send the errors to /dev/null.

    4. Run cat stock-history.log again.

    To pass: stock-history.log has yesterday's three lines first, unchanged, then today's five stock lines exactly once, and nothing else. No warnings or errors in it.

  5. Task 5

    Show the low stock on screen and save it in low.txt

    Show the low stock on the screen and save it in a file, with one command.

    1. Run ./stock.sh and pass its output with | to grep.

    2. grep keeps only the lines that say LOW.

    3. Pass those lines with | to tee low.txt. tee shows them and saves them at the same time.

    4. Run cat low.txt to see what you saved.

    To pass: low.txt has the LOW lines for milk and cups, and no other stock lines. You ran a command with | tee in the chapter folder, and it worked.

What you will learn

  • Two streams: standard output for results, standard error for complaints, each sent where you choose.
  • Adding instead of replacing: >>, and /dev/null, the bin.
  • Both streams in one file: 2>&1, and why its place matters.
  • Pipes and tee: | passes output to the next command, tee saves a copy on the way.

More in Files and text

Build "Pipes and redirection" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.