CHAPTER 4 · 7 lessons · 91 exercises

Connect commands

Hand the output of one command to the next. Keep the result in a file. Split the errors off on their own.

This is where the command line is at its strongest. Join small tools together and build whatever you need on the spot.

The idea to hold on to is ways out and ways in. Every command has two ways out (the ordinary result, called standard output, and the errors, called standard error) and one way in (standard input).

  • > — point the way out at a file (overwriting)
  • >> — add to the end of a file
  • 2> — split the errors off on their own
  • < — pour a file into the way in
  • | — wire the way out of one command to the way in of the next
  • tee — show it on the screen and keep it in a file at the same time
  • xargs — turn the text flowing past into arguments for the next command

> overwrites in silence. Point it at a file with something in it and the original contents are gone. This chapter has you do that once on purpose, so get the habit into your fingers.

By the time you get here, work you used to do by tapping away at a screen fits on one line.

Lessons in this chapter

Point the output at a file

  1. 13Keep the result in a file (`>`)Point the output of a command at a file, so the result does not just flow past and vanish.Go to the exercises
  2. 14Add without wiping (`>>`)Use >> to add a result to the end of a file while keeping what is already there.Go to the exercises
  3. 15Split the errors off (`2>`)Errors come out of a different way out. Split them with 2>, join them with 2>&1, throw them away with /dev/null.Go to the exercises
  4. 16Pour a file in (`<`)Use < to wire a file to the standard input, so tools that only have a way in become usable.Go to the exercises

Join them with a pipe

  1. 17Join them with a pipe (`|`)The way out of one command into the way in of the next. Build things up, as many stages as you like.Go to the exercises
  2. 18Show it and keep it (`tee`)Use tee to branch a pipe, so you can watch what is happening partway and keep it.Go to the exercises
  3. 19Turn a stream into arguments (`xargs`)Use xargs to hand what came down a pipe to the next command as its arguments.Go to the exercises

Next up is “Reshape text”. Sort it, drop duplicates, pull out a column, swap one thing for another. The tools that work on a stream of text.