SkillSprout
Chapter 1 · Lesson 8
Beginner
1 min read

Pipes, Redirection and Standard Streams

Overview

Every process normally has three standard streams: standard input, standard output and standard error. Pipes connect one process's output to another process's input. Redirection sends data to or from files.

Detailed Notes

Standard streams

StreamNumberPurpose
stdin0Input from keyboard or another process
stdout1Normal result data
stderr2Errors and diagnostics

A pipeline uses |:

bash
ps aux | grep nginx

ps aux prints process data to stdout. grep nginx reads that data from stdin and prints matching lines.

Redirection

bash
ls /tmp > listing.txt
ls /missing 2> error.txt
ls /tmp 2>> errors.txt >> listing.txt
  • > redirects stdout and replaces a file.
  • >> appends stdout.
  • 2> redirects stderr.
  • &> redirects both stdout and stderr in Bash.

Pipe output and tee

bash
ps aux | sort -k3nr | head -n 5
ps aux | tee process-list.txt | grep nginx

tee writes data to a file and sends the same data to the next command.

xargs

bash
printf 'first.txt
second.txt
' | xargs rm

xargs converts lines of input into arguments for a command. Do not run a destructive xargs command until you print the generated arguments:

bash
printf 'first.txt
second.txt
' | xargs -t echo rm

-t prints each generated command before running it.

Common Interview Questions

Finished this lesson?

0 of 25 lessons done · 0% of the course