Guide to Stream Redirections in Linux


As a Linux user, it's essential to understand concept of stream redirections. Stream redirections allow us to manipulate and control input and output streams of Linux commands. It means that we can control where standard input, output, and error streams are going to and coming from. In this article, we'll guide you through 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 input stream that allows us to send data into a command.

  • Standard Output (stdout) − This is output stream that displays output of a command.

  • Standard Error (stderr) − This is stream that displays error messages if a command fails to execute correctly.

Stream redirections allow us to redirect these streams to and from files or other commands, giving us control over input and output of a command. Stream redirections use special characters to indicate where streams should be redirected. most common redirection operators are −

  • (greater than) − redirects stdout to a file or command.

  • < (less than) − redirects stdin from a file or command.

  • (double greater than) − appends stdout to a file or command.

  • 2> (2 followed by greater than) − redirects stderr to a file or command.

  • 2>> (2 followed by double greater than) − appends stderr to a file or command.

Now that we have covered basics let's get started with examples.

Redirecting Standard Output

Let's start with a simple example of how to redirect standard output. Suppose we have a file named "test.txt," and we want to redirect output of "ls" command to this file. We can do this using ">" operator as follows −

$ ls > test.txt

In this example, ">" operator redirects output of "ls" command to "test.txt" file. When we run this command, standard output of "ls" command will be written to "test.txt" file instead of being displayed on screen.

If we want to append output to end of file, we can use ">>" operator instead of ">" as follows −

$ ls >> test.txt

In this example, ">>" operator appends output of "ls" command to end of "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 "<" operator as follows −

$ sort < input.txt

In this example, "<" operator redirects input from "input.txt" file to "sort" command. When we run this command, "sort" command will sort data from "input.txt" file instead of waiting for input from keyboard.

We can also use "|" (pipe) operator to redirect output of one command as input to another command. For example −

$ ls -l | grep "test"

In this example, "|" operator redirects output of "ls -l" command as input to "grep" command. "grep" command will search for word "test" in output of "ls -l" command 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 "2 Redirecting Standard Error (cont.) ">" operator followed by "2" as follows −

$ command 2> error.txt

In this example, "2>" operator redirects standard error output of "command" to "error.txt" file. When we run this command, any error messages generated by "command" will be written to "error.txt" file instead of being displayed on screen.

If we want to append error output to end of file, we can use "2>>" operator instead of "2>" as follows −

$ command 2>> error.txt

In this example, "2>>" operator appends standard error output of "command" to end of "error.txt" file.

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

In this example, ">" operator redirects standard output of "command" to "output.txt" file, while "2>" operator redirects standard error output of "command" to "error.txt" file.

Tips for Working with Stream Redirections

Here are some tips to help you work with stream redirections more effectively −

  • Use "man" command to check syntax of stream redirection operators and to learn more about how to use them.

  • Use "-h" or "--help" option with commands to check if they support stream redirections.

  • Use "tee" command to display output of a command on screen and redirect it to a file at same time. For example −

$ command | tee output.txt

In this example, "tee" command will display output of "command" on screen and write it to "output.txt" file.

  • Be careful when using ">" operator with existing files. If you use ">" operator with an existing file, it will overwrite contents of file. To avoid this, use ">>" operator instead to append to file.

  • Use "null" device to discard output. If you want to discard output of a command, you can redirect it to "null" device as follows −

$ command > /dev/null

In this example, standard output of "command" will be discarded.

Conclusion

In conclusion, stream redirections are an essential part of Linux command-line usage. They allow us to control input and output of commands, redirecting them to or from files or other commands. In this article, we have covered basics of stream redirections and shown you how to use them with examples. By mastering stream redirections, you will be able to manipulate and control output of Linux commands, making your work more efficient and effective.

Updated on: 03-Mar-2023

387 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements