πŸ“‘Linux/Unix: File Viewing, Redirection & Filters
Estimated reading: 3 minutes 269 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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top