Hey everyone π
When I first started learning the command line, I thought it was just a place to type a bunch of commands and watch stuff fly across the screen. But then I discovered something called redirection β and suddenly, the terminal became a toolbox, not just a text box.
Whether you're managing logs, cleaning up files, or piping data between tools β redirection is how pros turn one-liner commands into power moves.
Let me explain it the way I wish someone had explained it to me π
π Think of Redirection Like Plumbing for Text
Imagine you're in a kitchen. You've got:
- a sink (your command),
- a pipe (the redirection symbol),
- and a bucket (a file or the next command).
Normally, a command sends output to your screen (stdout
). But with redirection, you can reroute that output into a file, another command, or even pull input from a file.
βοΈ Core Redirection Symbols
Symbol | What It Does |
---|---|
> |
Sends output to a file (overwrites) |
>> |
Sends output to a file (appends) |
< |
Sends a file as input to a command |
βοΈ Example: Output to File
echo "Hello World" > hello.txt
β
Creates a file with your message.
echo "Welcome back!" >> hello.txt
β Appends new content to the existing file.
π₯ <
β Feed Commands from a File
Instead of typing everything manually, let a file do the talking:
cat < greetings.txt
π£οΈ Translation: βHey cat
, read from this file instead of my keyboard.β
π |
β The Game-Changer: Piping Commands Together
Want to clean up a list of names?
cat names.txt | sort | uniq > clean-names.txt
Each command passes its result to the next β like a conveyor belt on an assembly line.
π Combine with Power Commands
These tools pair beautifully with redirection:
-
sort
: Alphabetizes your data -
uniq
: Removes adjacent duplicates -
grep
: Searches for patterns -
sed
: Replaces or transforms text
Example: Filter and Clean Error Logs
grep -i "error" logs.txt | sort | uniq > error-summary.txt
β Now youβve got a clean, sorted list of all errors β ready for review.
π§ Pro Tip: Not All Redirection Changes Files
sed 's/snow/rain/g' weather.txt
This shows you the result, but the file stays unchanged.
To update the file in-place:
sed -i 's/snow/rain/g' weather.txt
π The -i
flag is your "make it real" switch.
π‘ Why Redirection Matters (Even If You're Not a Linux Wizard Yet)
Youβll use redirection for:
β
Cleaning up messy data
β
Logging output from a long script
β
Building automations for daily tasks
β
Learning how DevOps pipelines work behind the scenes
Redirection gives you super control over what your commands do and where your data goes.
π§© Final Thoughts
When I first saw >
and |
in tutorials, Iβd skip past them β they looked cryptic.
But once you start using them, you realize theyβre not just shortcuts β theyβre infrastructure for your workflows.
Redirection starts off small⦠but ends up everywhere.
If you're learning the terminal or scripting your first automation, Iβd love to hear how you're using redirection (or struggling with it β Iβve been there too).
π¬ Drop a comment or hit me up on LinkedIn β letβs keep building our command-line superpowers together! π»β‘
Top comments (0)