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 Name | File Descriptor | Description |
|---|---|---|
stdin | 0 | Standard Input |
stdout | 1 | Standard Output |
stderr | 2 | Standard 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 commandteeshows 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 file2>redirects errors (stderr)- Combine outputs using
2>&1or&> 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 :
