Sikau

Linux and the shell · Day 6 · about 30 min

Redirection and pipes

stdout, stderr, >, >>, <, 2>&1, | and tee.

Small tools, joined together

The Unix idea: each tool does one job well, and you connect them. Every command has three streams: input (stdin), output (stdout) and errors (stderr). You can send them into files, or pipe one command's output straight into the next.

Redirect and pipe

echo "first" > notes.txt
echo "second" >> notes.txt
cat notes.txt
wc -l < notes.txt
ls /nope 2> errors.txt
cat errors.txt
ls notes.txt /nope > all.txt 2>&1
ls /nope 2> /dev/null
cat menu.csv | grep yes | wc -l
ls /etc | tee etc-list.txt | head -3

The rest of this lesson

  • Order matters with 2>&1explain
  • Pipelines in practiceexplain
  • What's in log.txt?predict
  • Redirection meaningsmatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 16 lessons of Linux and the shell