Control where a command reads from and writes to: redirect stdout and stderr, append to files, discard output, chain commands with pipes, and capture or stream output with command and process substitution.
Why: every command has three channels — standard input (0), standard output (1), and standard error (2). Normal results go to stdout; error messages go to stderr. Keeping them separate lets you save output while still seeing errors, or the reverse.
# stdin (0) — where a command reads input
# stdout (1) — normal output
# stderr (2) — error and diagnostic output
ls /etc /does-not-exist
# /etc listing -> stdout
# "No such file" error -> stderrWhy: > sends stdout to a file, replacing its contents. >> appends instead of overwriting. To redirect errors, target stream 2 with 2>. To send both to one place, use &> (or 2>&1).
Overwrite a file with the output
echo "first line" > log.txtAppend another line
echo "second line" >> log.txtSend errors to their own file
ls /nope 2> errors.txtSend BOTH stdout and stderr to one file
ls /etc /nope &> all.txtWhy: /dev/null is a "black hole" — anything written to it disappears. Redirect there when you only care whether a command succeeded, not what it printed. 2>/dev/null hides errors; >/dev/null 2>&1 hides everything.
Silence normal output, keep errors visible
command --version > /dev/nullSilence errors only
grep "x" file.txt 2> /dev/nullSilence everything — just check the exit code
ping -c1 example.com > /dev/null 2>&1Why: a pipe | feeds one command's stdout into the next command's stdin, so you build a pipeline that transforms data step by step. Note: the text tools in these pipelines (grep, sort, uniq, wc, cut) are covered in the Linux course lesson on pipes & text processing.
Count how many lines contain "error"
cat app.log | grep "error" | wc -lTop 3 most common values in a column
cut -d',' -f2 data.csv | sort | uniq -c | sort -rn | head -3Why: <(command) makes a command's output look like a temporary file, so you can feed two command outputs into a tool that expects file arguments — like diff comparing two live listings. This is a Bash-specific feature plain sh does not have.
Compare the output of two commands without temp files
diff <(ls dir-a) <(ls dir-b)Feed a command's output where a file path is expected
while read -r line; doecho "got: $line"done < <(grep "WARN" app.log)