DEV Community

1suleyman
1suleyman

Posted on

🧰 Redirection in the Command Line: The Toolbox I Wish I Knew About Earlier

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
Enter fullscreen mode Exit fullscreen mode

βœ… Creates a file with your message.

echo "Welcome back!" >> hello.txt
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

πŸ—£οΈ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

βœ… 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
Enter fullscreen mode Exit fullscreen mode

This shows you the result, but the file stays unchanged.

To update the file in-place:

sed -i 's/snow/rain/g' weather.txt
Enter fullscreen mode Exit fullscreen mode

πŸ“ 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)