Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Guide to Stream Redirections in Linux
As a Linux user, it's essential to understand the concept of stream redirections. Stream redirections allow us to manipulate and control input and output streams of Linux commands. This means we can control where standard input, output, and error streams are going to and coming from. In this article, we'll guide you through the basics of stream redirections and show you how to use them with examples.
Introduction to Stream Redirections
In Linux, there are three types of streams
Standard Input (stdin) This is the input stream that allows us to send data into a command. It has file descriptor 0.
Standard Output (stdout) This is the output stream that displays the output of a command. It has file descriptor 1.
Standard Error (stderr) This is the stream that displays error messages if a command fails to execute correctly. It has file descriptor 2.
Stream redirections allow us to redirect these streams to and from files or other commands, giving us control over the input and output of a command. Stream redirections use special characters to indicate where streams should be redirected. The most common redirection operators are
| Operator | Description | Example |
|---|---|---|
> |
Redirects stdout to a file (overwrites) | ls > output.txt |
< |
Redirects stdin from a file | sort < data.txt |
>> |
Appends stdout to a file | echo "text" >> log.txt |
2> |
Redirects stderr to a file | command 2> error.log |
2>> |
Appends stderr to a file | command 2>> error.log |
| |
Pipes stdout to another command | ls | grep "file" |
Redirecting Standard Output
Let's start with a simple example of how to redirect standard output. Suppose we want to redirect the output of the ls command to a file named "test.txt". We can do this using the > operator as follows
ls > test.txt
In this example, the > operator redirects the output of the ls command to the "test.txt" file. When we run this command, the standard output of ls will be written to "test.txt" instead of being displayed on the screen.
If we want to append output to the end of a file, we can use the >> operator instead of > as follows
ls >> test.txt
In this example, the >> operator appends the output of the ls command to the end of the "test.txt" file.
Redirecting Standard Input
Now let's look at how to redirect standard input. Suppose we have a file named "input.txt" that contains some data, and we want to redirect this data as input to a command. We can do this using the < operator as follows
sort < input.txt
In this example, the < operator redirects input from the "input.txt" file to the sort command. When we run this command, sort will sort the data from "input.txt" instead of waiting for input from the keyboard.
We can also use the | (pipe) operator to redirect the output of one command as input to another command. For example
ls -l | grep "test"
In this example, the | operator redirects the output of ls -l as input to the grep command. The grep command will search for the word "test" in the output of ls -l and display matching lines.
Redirecting Standard Error
Finally, let's look at how to redirect standard error. Suppose we have a command that generates error messages, and we want to redirect these error messages to a file. We can do this using the 2> operator as follows
command 2> error.txt
In this example, the 2> operator redirects the standard error output of "command" to the "error.txt" file. When we run this command, any error messages generated by "command" will be written to "error.txt" instead of being displayed on the screen.
If we want to append error output to the end of a file, we can use the 2>> operator instead of 2> as follows
command 2>> error.txt
Combining Stream Redirections
We can also combine stream redirections to redirect multiple streams at once. For example, suppose we want to redirect standard output and error output of a command to different files. We can do this as follows
command > output.txt 2> error.txt
To redirect both stdout and stderr to the same file, we can use
command > logfile.txt 2>&1
The 2>&1 means "redirect stderr to wherever stdout is going".
Advanced Techniques
Here are some useful advanced redirection techniques
Using the tee Command
The tee command allows you to display output on screen and redirect it to a file simultaneously
command | tee output.txt
Discarding Output
Use the null device /dev/null to discard unwanted output
command > /dev/null 2>&1
Here Documents
You can provide multi-line input using here documents
cat << EOF This is line 1 This is line 2 EOF
Common Use Cases
| Task | Command |
|---|---|
| Save command output to file | ls -la > directory_listing.txt |
| Append to log file | echo "$(date): Process started" >> app.log |
| Hide error messages | command 2> /dev/null |
| Count lines in a file | wc -l < textfile.txt |
| Filter and save results | ps aux | grep "apache" > apache_processes.txt |
Key Points
Be careful when using
>with existing files it overwrites the file contents. Use>>to append instead.File descriptor numbers: 0 (stdin), 1 (stdout), 2 (stderr)
Use
man bashto learn more about redirection operatorsThe
&>operator redirects both stdout and stderr to the same location
Conclusion
Stream redirections are a fundamental feature of Linux command-line usage that provide powerful control over input and output streams. By mastering these redirection techniques, you can efficiently manage data flow between commands, files, and processes, making your Linux workflows more effective and automated.
