DEV Community

Cover image for 🧠 Linux Redirection & Pipes: The Magic of `>`, `>>`, `|`, and File Descriptors 🎯
SAHIL
SAHIL

Posted on

🧠 Linux Redirection & Pipes: The Magic of `>`, `>>`, `|`, and File Descriptors 🎯

Ever wondered how Linux commands talk to each other? Let's dive into the power-packed symbols and streams that drive automation in the terminal.

From STDOUT to 2>&1, this is your one-stop cheat sheet 🧾


🎬 What Are STDIN, STDOUT & STDERR?

Every process in Linux interacts with 3 default streams:

Stream Meaning File Descriptor Role
STDIN Standard Input 0 Takes input (usually keyboard)
STDOUT Standard Output 1 Shows successful command output
STDERR Standard Error Output 2 Displays error messages

➑️ STDOUT is for successful output, while STDERR is where errors are sent β€” they are separate!


πŸ”’ What’s a File Descriptor?

File Descriptors are just numbers assigned to files or streams opened by a process.

They help the shell identify where to read/write.

Standard mapping:

0 - STDIN   (input)
1 - STDOUT  (normal output)
2 - STDERR  (error output)
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Shell Symbols Cheat Sheet

Symbol Name What It Does
; Command Separator Runs commands one after another, regardless of success.
| Pipe Sends output of one command to input of another.
> Redirect Output Sends STDOUT to a file (overwrite).
>> Redirect Output Sends STDOUT to a file (append).
2> Redirect STDERR Sends errors to a file.
2>&1 Merge Outputs Merges STDERR into STDOUT.

πŸš€ Real-Life Examples

🟩 ; β€” Run Multiple Commands

echo "Start"; ls; echo "End"
Enter fullscreen mode Exit fullscreen mode

🟦 | β€” Pipe Output into Another Command

ps aux | grep apache
Enter fullscreen mode Exit fullscreen mode

πŸŸ₯ > β€” Redirect STDOUT (Overwrite)

echo "Hello World" > file.txt
Enter fullscreen mode Exit fullscreen mode

🟨 >> β€” Redirect STDOUT (Append)

echo "New Line" >> file.txt
Enter fullscreen mode Exit fullscreen mode

🟧 2> β€” Redirect STDERR

ls notafile 2> error.log
Enter fullscreen mode Exit fullscreen mode

πŸŸͺ 2>&1 β€” Combine STDOUT + STDERR

make > output.log 2>&1
Enter fullscreen mode Exit fullscreen mode

🧠 Bonus: Redirect STDIN (Input)

sort < names.txt
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Quick Reference Table

Operator Meaning Example
> STDOUT to file (overwrite) echo hi > file.txt
>> STDOUT to file (append) echo again >> file.txt
2> STDERR to file cmd 2> errors.txt
&> STDOUT + STDERR to file cmd &> alloutput.txt (Bash only)
2>&1 Merge STDERR to STDOUT cmd > out.txt 2>&1
< Redirect file as input sort < names.txt
; Run commands in sequence date; cal

πŸ”₯ Bash Scripting Overview

These redirection symbols and streams are heavily used in bash scripting to handle outputs, store logs, and avoid error clutter. For example:

#!/bin/bash
ls /etc > out.txt 2> err.txt
Enter fullscreen mode Exit fullscreen mode

In upcoming posts, we’ll dive deeper into bash scripting, but this gives you a quick taste of what’s possible πŸ”§.


πŸ“Œ Coming Soon: Quotes in Shell Scripts

We’ll soon talk about the differences between single quotes '...', double quotes "...", and backticks ...\ β€” and how they change how commands behave in shell scripting.

Stay tuned for the next post! πŸ“š


🏁 Wrap-Up

These symbols may look tiny, but they are mighty! Mastering them helps you script smarter, debug easier, and automate faster.

πŸ’¬ Got a trick or shortcut you love? Share it in the comments!


#Linux #Bash #ShellScripting #CommandLine #DevOps #Redirection #CheatSheet

Top comments (0)