Theory
Characters with superpowers
Some characters mean something special to the shell. When you type *, the shell does not look for a file literally named star, it expands it to match filenames. When you type >, you are not printing a greater-than sign, you are redirecting output into a file.
These special characters are called metacharacters (and operators), and they turn plain commands into powerful one-liners. This lesson covers the essential ones: wildcards, redirection, pipes, and command substitution. Master these and the command line becomes dramatically more capable.
At a glance
| Symbol | Meaning |
|---|---|
| * | Wildcard: matches any number of characters in filenames |
| ? | Wildcard: matches exactly one character |
| > file | Redirect output INTO file (overwrites it) |
| >> file | Redirect output, APPENDING to file |
| < file | Take input FROM file |
| cmd1 | cmd2 | Pipe: cmd1's output becomes cmd2's input |
| $(cmd) | Command substitution: replaced by cmd's output |
Practical
Metacharacters at work (verified)
$ ls *.txt # wildcard: every name ending in .txt
a1.txt a2.txt out.txt
$ ls ?1.* # ? = one char: names like a1.xxx, b1.xxx
a1.txt b1.log
$ echo "line1" > out.txt # > creates/overwrites out.txt
$ echo "line2" >> out.txt # >> appends a second line
$ wc -l < out.txt # < feeds the file in as input
2
$ echo "There are $(ls *.txt | wc -l) txt files" # pipe + substitution
There are 3 txt filesFormula
Redirection goes to files; pipes go to commands
Keep two ideas distinct. Redirection connects a command to a file: > and >> send output into a file, < reads input from a file. A pipe | connects a command to another command: it feeds the first's output straight into the second, with no file in between.
So sort names.txt > sorted.txt puts sorted output in a file, while sort names.txt | uniq feeds sorted output into uniq. Files on one side, commands on the other: that distinction untangles most confusion.
Quiz
You run: echo "first" > log.txt , then later echo "second" > log.txt (both with a single >). What does log.txt contain?
- Both lines: first and second
- Only 'second', because a single > overwrites the file each time
- Only 'first', because the file was already created
- Nothing; > deletes the file
Show the answer
Only 'second', because a single > overwrites the file each time
A single > OVERWRITES the file's contents every time. The first command puts 'first' in log.txt; the second command overwrites it entirely with 'second', so only 'second' remains. Option A would be correct only if the second redirect used >> (append), which adds without erasing, that is the crucial difference. Option C is backwards: the newer write replaces the older content, not the other way round. Option D is wrong: > writes new content (and creates the file if needed); it does not leave it empty here. The trap to remember: > overwrites (data loss if you meant to add), >> appends. When in doubt about keeping existing content, use >>.
Think first
Why is confusing > and >> such a dangerous mistake?
Both write to a file. Why do experienced users treat the single > with real caution? Then tap.
Show the answer
Because a single > SILENTLY DESTROYS whatever was already in the file, with no warning and no undo. Imagine you have a log file that has been collecting important results for weeks, and you mean to add one more line. If you type command >> log.txt you APPEND, and all the old results are safely kept. But if you slip and type command > log.txt with a single >, the shell first TRUNCATES the file to empty, then writes the new content, so weeks of results vanish in an instant, and there is no recycle bin to recover them (recall rm's lesson: Linux does not soften destructive actions). The danger is that the two operators look almost identical, one character apart, yet one preserves and one obliterates. That is why careful users build the habit of pausing whenever they type > to a file that matters, and default to >> whenever they intend to add rather than replace. The single > is perfect for creating fresh output, but aimed at a file you care about, it is a loaded footgun. One extra > means 'keep what is there'; forgetting it means 'erase it all'.
Summary
Key takeaways
- Metacharacters are characters the shell treats specially, turning simple commands into powerful one-liners.
- Wildcards expand filenames: * matches any characters, ? matches exactly one, [ ] matches a set.
- Redirection connects a command to a file: > overwrites, >> appends, < reads input from a file.
- A pipe | connects a command to another command, feeding output straight in with no file between.
- Command substitution $(cmd) is replaced by that command's output, dropping it into your line.
- Danger: a single > overwrites (destroying existing content) while >> appends; default to >> when adding.
- Memory hook: * many, ? one, > overwrite, >> append, | pipe to a command, $(...) insert output.