Linux Cheatsheet

Pipes and Redirection

Use this Linux reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Redirection

OperatorWhat it does
cmd > filestdout to file (overwrite)
cmd >> filestdout to file (append)
cmd < filefile as stdin
cmd 2> filestderr to file
cmd 2>&1redirect stderr to stdout
cmd > file 2>&1both stdout and stderr to file
cmd &> fileshorthand for > file 2>&1 (Bash)
cmd >> file 2>&1append both
cmd 2>/dev/nulldiscard stderr
cmd >/dev/null 2>&1discard all output
cmd <<< "string"here-string as stdin
ls /etc > listing.txt           # save output
ls /bad 2> errors.txt           # capture errors
ls /etc /bad > out.txt 2>&1     # merge into one file
cat /dev/null > file.txt        # empty a file
echo "line" >> file.txt         # append

# here-doc: multi-line stdin
cat <<'EOF' > script.sh
#!/bin/bash
echo "hello"
EOF

# here-string
base64 <<< "hello world"

Pipes

cmd1 | cmd2             # stdout of cmd1 → stdin of cmd2
cmd1 | cmd2 | cmd3      # pipeline chain
cmd1 |& cmd2            # stdout + stderr → stdin of cmd2 (Bash)
cmd 2>&1 | cmd2         # same, POSIX-compatible

ls -l | grep "^d"                   # directories only
cat /etc/passwd | cut -d: -f1 | sort
journalctl | grep "error" | tail -20
ps aux | grep nginx | grep -v grep

tee — Branch Output

cmd | tee file.txt              # stdout AND save to file
cmd | tee -a file.txt           # append to file
cmd | tee file.txt | wc -l      # save and count
cmd |& tee all.log              # include stderr

xargs — Build and Run Commands

find . -name "*.txt" | xargs wc -l
find . -name "*.bak" | xargs rm
echo "a b c" | xargs -n1 echo          # one argument per line
echo "a b c" | xargs -n2               # two args per invocation
find . -name "*.txt" -print0 | xargs -0 grep "TODO"   # null-delimited (safe for spaces)
xargs -I{} cp {} {}.bak < list.txt     # placeholder substitution
cat hosts.txt | xargs -P4 -I{} ssh {} uptime   # 4 parallel jobs

Process Substitution

diff <(ls dir1/) <(ls dir2/)        # compare command outputs as files
cat <(date) <(uptime)               # concat outputs
tee >(gzip > out.gz) < input        # write to command as if file

Named Pipes (FIFO)

mkfifo /tmp/mypipe
# Terminal 1:
cat bigfile | gzip > /tmp/mypipe
# Terminal 2:
cat /tmp/mypipe | wc -c

Common Pipeline Patterns

# Count unique values in a column
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head

# Top 10 largest files
du -sh * | sort -rh | head -10

# Remove blank lines
grep -v "^$" file.txt

# Deduplicate while preserving order
awk '!seen[$0]++' file.txt

# Print lines between two patterns
sed -n '/START/,/END/p' file.txt

# Sum a column of numbers
awk '{s+=$1} END{print s}' nums.txt

# Monitor log in real time
tail -f /var/log/syslog | grep --line-buffered "error"

Exit Codes in Pipelines

echo $?             # exit code of last command
echo ${PIPESTATUS[@]}  # exit codes of all pipe stages (Bash)

# fail pipeline if any stage fails
set -o pipefail
Exit codeMeaning
0success
1general error
2misuse of shell built-in
126command not executable
127command not found
128+nfatal signal n