π 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 commandtee
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 file2>
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 :