Objective

Control where command input and output goes using redirection operators and chain commands with pipes to build powerful one-liners.

Tools & Technologies

  • redirect
  • pipe
  • >
  • >>
  • 2>
  • tee
  • xargs

Key Commands

ls > list.txt
cmd 2>&1 | tee log.txt
find . | xargs grep pattern
cat /dev/null > clear.txt

Architecture Overview

flowchart LR CMD[Command] -->|stdout fd=1| OUT[Output] CMD -->|stderr fd=2| ERR[Error] IN[Input] -->|stdin fd=0| CMD OUT -->|>| FILE[File] OUT -->|>>| APPEND[Append] ERR -->|2>| EFILE[Error File] ERR -->|2>&1| OUT OUT -->|pipe| CMD2[Next Command] style CMD fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0 style CMD2 fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0

Step-by-Step Process

01
Standard Streams

Every process has 3 streams: stdin (0), stdout (1), stderr (2). Redirection controls where each goes.

# stdout to file (overwrite)
ls /etc > filelist.txt
# stdout append
echo 'new line' >> log.txt
# stderr to file
cmd 2> errors.txt
# both stdout and stderr
cmd > all.txt 2>&1
# discard output
cmd > /dev/null 2>&1
02
Pipes

The | operator connects stdout of one command to stdin of the next. Chain multiple commands.

ps aux | grep nginx          # filter processes
cat /etc/passwd | cut -d: -f1 | sort
du -h /var | sort -rh | head -20  # disk hogs
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
03
tee — Split Output

tee reads stdin and writes to both stdout and a file simultaneously.

make 2>&1 | tee build.log     # see + save
tcpdump -i eth0 | tee capture.txt | grep 'ERROR'
echo 'nameserver 8.8.8.8' | sudo tee -a /etc/resolv.conf
04
xargs — Build Command Arguments

xargs converts stdin lines into arguments for another command.

find . -name '*.log' | xargs rm
find . -name '*.txt' | xargs grep 'ERROR'
cat urls.txt | xargs -I{} curl -O {}
# Handle spaces in filenames:
find . -name '*.txt' -print0 | xargs -0 rm

Challenges & Solutions

  • 2>&1 order matters — must come after the > or output goes to terminal
  • xargs without -0 breaks on filenames with spaces

Key Takeaways

  • Use tee when you need both screen output and logging simultaneously
  • Process substitution <(cmd) lets you diff two command outputs