πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 22 views

πŸ” Linux/Unix: I/O Redirection – >, <, >>, 2> Explained with Examples

🧲 Introduction – Why Learn I/O Redirection in Linux/Unix?

I/O redirection is a core concept in Linux/Unix that lets you control how input and output are handled during command execution. Instead of reading input from the keyboard or printing output to the terminal, you can redirect it to files, logs, or other commands. This is crucial for automation, scripting, and error handling.

🎯 In this guide, you’ll learn:

  • The purpose of redirection operators: >, <, >>, 2>
  • How to separate output and errors
  • How to redirect input/output efficiently in real-world scenarios

🧭 What Is I/O Redirection?

There are three main streams in Linux:

Stream NameFile DescriptorDescription
stdin0Standard Input
stdout1Standard Output
stderr2Standard Error Output

Redirection lets you control where these streams go.


πŸ“€ > – Redirect Standard Output (Overwrite)

βœ… Syntax:

command > file.txt

πŸ“Œ Description:

Redirects stdout to a file, overwriting existing content.

πŸ§ͺ Example:

echo "Hello World" > hello.txt

βœ… Creates or replaces hello.txt with the text Hello World.


πŸ“₯ < – Redirect Standard Input

βœ… Syntax:

command < input.txt

πŸ“Œ Description:

Feeds input from a file instead of the keyboard (stdin).

πŸ§ͺ Example:

wc -l < hello.txt

βœ… Counts lines in hello.txt by taking input from the file.


βž• >> – Append Standard Output

βœ… Syntax:

command >> file.txt

πŸ“Œ Description:

Redirects stdout to a file but appends instead of overwriting.

πŸ§ͺ Example:

echo "Another line" >> hello.txt

βœ… Adds a new line to the end of hello.txt without deleting existing content.


❌ 2> – Redirect Standard Error

βœ… Syntax:

command 2> error.log

πŸ“Œ Description:

Redirects stderr (errors) to a separate file.

πŸ§ͺ Example:

ls nofile.txt 2> error.txt

βœ… Stores the error message in error.txt.


πŸ” Combine Redirection Examples

πŸ“„ Redirect stdout and stderr separately:

command > output.log 2> error.log

πŸ“„ Redirect both to the same file:

command &> combined.log   # Bash only

Or:

command > all.log 2>&1

βœ… 2>&1 sends stderr to where stdout is going.


πŸ”ƒ Chaining Redirection with Piping

grep "error" logfile.txt | tee filtered.log > all.txt
  • | passes output to next command
  • tee shows output and writes to file

πŸ“Œ Summary – Recap & Next Steps

Redirection helps store results, suppress output, and capture errors in a structured, script-friendly manner. Whether you’re monitoring logs, automating reports, or debugging scriptsβ€”mastering these operators is essential.

πŸ” Key Takeaways:

  • > redirects stdout (overwrite), >> appends
  • < takes input from a file
  • 2> redirects errors (stderr)
  • Combine outputs using 2>&1 or &> file.log

❓ FAQs

❓ What’s the difference between > and >>?
βœ… > overwrites the file; >> appends to it.

❓ How do I redirect both output and error to the same file?
βœ… Use:

command > output.txt 2>&1

❓ Can I discard output completely?
βœ… Yes, redirect to /dev/null:

command > /dev/null 2>&1

❓ How do I read input from a file instead of typing it?
βœ… Use:

command < input.txt

❓ What happens if the file doesn’t exist when redirecting?
βœ… It gets created automatically (for > and >>).


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: I/O Redirection (>, <, >>, 2>)

Or Copy Link

CONTENTS
Scroll to Top