3.2a Pipes, Redirection, and Basic Regular Expressions
Key Takeaways
A pipe (|) sends stdout of the left command into stdin of the right command
overwrites (or creates) a file with stdout; >> appends stdout to a file
< feeds a file into a command as stdin; 2> redirects stderr; 2>&1 merges stderr into stdout
Basic regex atoms on Essentials: . any one character, [ ] character class, * zero-or-more of the previous atom, ? optional (ERE) / shell glob awareness
Quote patterns with spaces or metacharacters so the shell does not expand them before grep or similar tools see them
Connecting commands and shaping I/O (objective 3.2)
Objective 3.2 is about searching and extracting data. Before the individual tools (grep, sort, cut, and friends), you need the plumbing: pipes, redirection, and a little regular expression vocabulary so patterns mean what you intend.
Pipes: |
A pipe connects two programs. The standard output (stdout) of the left command becomes the standard input (stdin) of the right command — no temporary file required.
ps aux | grep nginx
dmesg | grep -i usb
cat access.log | grep " 404 "
Read pipelines left to right: “generate text, then filter or transform it.” You can chain several stages:
grep "ERROR" app.log | sort | uniq -c
That pattern — produce → filter → refine — is the heart of day-to-day Linux text work and a frequent exam theme.
Pipes carry stdout, not stderr, unless you merge streams first (see 2>&1 below).
Pipe versus redirect: grep error app.log | wc -l counts on the fly; grep error app.log > hits.txt saves matches to a file. Pick | for another command, >/>> for a file.
Redirection operators
Every process has three standard streams:
| Stream | Number | Typical use |
|---|---|---|
| stdin | 0 | Input (keyboard or a file/|) |
| stdout | 1 | Normal output |
| stderr | 2 | Error messages |
Writing and appending: > and >>
| Operator | Effect |
|---|---|
> | Redirect stdout to a file — overwrite (create if missing) |
>> | Redirect stdout — append to the file |
echo "first" > notes.txt # creates or truncates notes.txt
echo "second" >> notes.txt # adds a second line
ls /etc > listing.txt # save directory listing
Danger: > truncates immediately. command > important.txt with a typo can wipe important.txt before you notice. Prefer >> when you mean “add to the log.”
Reading input: <
sort < names.txt
wc -l < app.log
< feeds a file into stdin. Many commands also accept filenames as arguments (sort names.txt).
Errors: 2> and merging with 2>&1
| Operator | Effect |
|---|---|
2> | Redirect stderr to a file (overwrite that error file) |
2>> | Append stderr to a file |
2>&1 | Send stderr to the same place as stdout |
ls /exists /missing > out.txt 2> err.txt
# out.txt gets the successful listing; err.txt gets the error for /missing
ls /missing > all.txt 2>&1
# both stdout and stderr land in all.txt
Order tip: write > file 2>&1 (redirect stdout to file, then point stderr at wherever stdout now goes). The reverse order 2>&1 > file first merges stderr to the terminal’s stdout, then only redirects stdout — a classic fill-in trap.
Worked silence-and-pipe: find /var/log -name "*.log" 2>/dev/null | head discards permission errors on stderr so only successful path names flow into head. /dev/null is the “bit bucket.”
Basic regular expressions
Linux Essentials expects awareness of basic regex atoms used with tools like grep. A regular expression is a pattern, not always a literal string.
| Atom | Meaning | Example idea |
|---|---|---|
. | Any one character | err.r matches error, errar |
[ ] | Character class — one char from the set | [Ee]rror matches Error or error |
* | Zero or more of the previous atom | fail* → fai plus zero or more l |
? | In extended regex (grep -E), previous atom optional; also a shell glob for “one char” — know the context |
grep "err.r" app.log
grep "[0-9][0-9][0-9]" codes.txt # three digits in a row (BRE style)
grep -E "colou?r" words.txt # color or colour with ERE ?
Shell globs vs regex: In the shell, * means “any string” in filenames (ls *.log). Inside grep’s default basic regular expressions, * repeats the previous piece. Quoting protects your intent:
grep "file.txt" notes # . means any character — matches fileXtxt too
grep "file\.txt" notes # escaped dot = literal period
When a pattern has spaces or shell-special characters, quote it so the shell does not expand globs or split words before grep runs:
grep "connection refused" /var/log/syslog
Tiny practice patterns
| Goal | Pattern sketch |
|---|---|
Lines with Error or error | [Ee]rror or use grep -i error |
Any character between a and c | a.c |
| Digit | [0-9] |
Literal dot in v1.2 | v1\.2 |
You do not need advanced Perl-style regex for 010-160. Own ., [ ], *, and when ? is optional-atom (ERE) versus a shell wildcard.
Fill-in traps: wrong merge order (2>&1 > file vs > file 2>&1); using > when the story says append; unquoted * so the shell expands names before grep.
Putting it together
cut -d: -f1 /etc/passwd | sort | uniq > users.txt
cmd_that_warns 2>&1 | grep -i warn
Exam mindset
|connects stdout → stdin between commands.>overwrites;>>appends;<supplies stdin from a file.2>is stderr;2>&1merges stderr into stdout (watch operator order).- Regex
.[]*(and?in the right mode) change matching — quote and escape when you mean literals.
With that plumbing solid, the extract tools in the next section become short, readable pipelines instead of one-off memorization.
What is the difference between > and >> when redirecting stdout?
appends; >> overwrites
Both always append; only 2> can overwrite
overwrites (or creates) the target file; >> appends to it
redirects stderr; >> redirects stdout
In a basic regular expression used by default grep, what does the pattern a.c match?
The letter a, then any one character, then the letter c
Only the exact three-character string a.c with a literal dot
Any line that contains a or c
Zero or more a characters followed by c
Which command sends both stdout and stderr of ls /missing into the file all.txt?
ls /missing 2> all.txt
ls /missing > all.txt 2>&1
ls /missing < all.txt
ls /missing >> all.txt 2>
What does the pipe in the command ps aux | grep ssh accomplish?
It archives the ps output into a file named grep
It runs grep first, then starts ps only if grep succeeds
It sends the stdout of ps aux into the stdin of grep so only matching lines remain
It redirects stderr from ps into grep while leaving stdout on the terminal
Sections you finish are checked off in the contents.