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)