Module 58 min

Pipes and Redirection

Chaining commands with | > >>

This is where the shell becomes powerful. Pipes and redirection let you combine simple commands into exactly the tool you need, which is the real reason engineers love the command line.

Redirection: sending output to a file

bash
ls -l > files.txt          # write output to a file (overwrites)
grep VIOLATED t.rpt >> bad.txt   # append to a file (keeps old content)
make 2> errors.txt          # send error messages to a file
make > out.txt 2>&1         # send both normal output and errors to one file

The pipe: feeding one command into the next

The pipe symbol | takes the output of the command on its left and feeds it as input to the command on its right. You chain small tools into a custom one.

bash
# How many violated paths are in the report?
grep "VIOLATED" timing.rpt | wc -l

# Show the unique cell types used, sorted
grep "dfxtp\|dfrtp" netlist.v | sort | uniq -c

# Find the 5 biggest files in a run directory
ls -lS runs/ | head -6

Why this matters

Instead of one giant program for every task, Linux gives you small commands that each do one thing well, and lets you pipe them together. grep finds lines, sort orders them, uniq counts duplicates, wc counts them. Combined, they answer almost any "how many" or "which ones" question about a report instantly.

Pro tip

the phrase to remember: grep to filter, sort to order, uniq -c to count, wc -l to total. That little pipeline answers a huge fraction of the questions you will have about tool reports, and it impresses interviewers who ask how you would find something in a log.