If you are familiar with the basic commands of Linux, you should also study the concept of I/O redirection.

You already know how the Linux team works. Boarding and disembarking are compulsory. There are several actors here. Let me tell you this.

Stdin, stdout and stderr

When you execute the Linux command, three data streams play a role:

  • The default input (stdin) is the source of the input data. By default, stdin refers to any text entry on the keyboard. Flow identification code 0.
  • The standard output (stdout) is the result of the job. By default, it is displayed on the screen. The flow identification is 1.
  • The default error (stderr) is the error message (if any) given by the commands. By default, stderr is also displayed on the screen. This is the identification code for Stream 2.

These wires contain plain text data in the so-called buffer.

Input Output & Error Redirect in Linux

Think of it as a river of water. You need a water source, like a tap. You connect a hose to it and you can store it in a bucket (file) or water the plants (pressure). If necessary, you can also connect it to another tap. Usually they divert water.

Linux also has a rerouting concept that makes it possible to reroute stdin, stdout and stderr from a normal destination to another file or command (or even to devices such as printers).

I’ll show you how rerouting works and how to use it.

Transmission of results

Input Output & Error Redirect in Linux

The first and simplest form of redirection is output redirection, also called stdout redirection.

You already know that the output of the purchase order is displayed on the screen by default. For example, I use the ls command to list all the files, and this is the result I get

[email protected] manual:~$ ls
appstxt new.txt static-ip.txt

You can redirect the output to a file by redirecting the output. If these output files do not exist, the shell creates them.

Command > File

For example, let me save the output of the ls command to a file called output.txt :

[email protected] manual:~$ ls > output.txt

Output file pre-created

What do you think the contents of this output file should look like? Let me use the cat team to show you a surprise:

[email protected] manual: ~$ cat output.txt

Did you notice that the output.txt is there? I deliberately chose this example to show you.

The output file to which stdout is redirected is created before the expected command is executed. What for? Because it must have a finite output destination to which the output signal is sent.

Fastening instead ofClock Tower

An often overlooked problem is that when you redirect to an existing file, the shell first deletes (blocks) that file. This means that the existing content of the output file is deleted and replaced by the output of the command.

You can add the bypass syntax >> instead of overwriting it.

Command >> File

Tip: You can deactivate blocking in the current shell session by: -C to be set

Why hijack Stdaut? You can save the results for later use and analysis. This is especially useful when the control output is too large and takes up the entire screen. It’s like picking up logs.

Gas pipeline shipments

Input Output & Error Redirect in Linux

Before you can see the stdin bypass, you need to know the pipe bypass. It’s more common and you’ll probably use it often.

When you divert a line, you send a standard control output to the standard input of another control.

Team 1 | Team 2

I’ll show you a practical example. Suppose you want to calculate the number of visible files in the current directory. You can use ls -1 (that’s a number one, not an L) to display the files in the current directory:

[email protected] manual: ~$ ls -1

You probably already know that the toilet command is used to calculate the number of lines in a file. If you combine these two controls with a hose, you get exactly this:

[email protected] manual: ~$ ls -1 | toilet -l

When using the pipe, both controls have the same buffer memory. The output of the first command is stored in a buffer and then the same buffer is used as input for the next command.

You’ll see the result of the last command. This is obvious because the output of the previous command(s) is given to the next command(s) instead of going to the screen.

Laying lines is not limited to connecting only two controls. You can link multiple orders if the output of one order can be used as input for the next order.

Team_1 | Team_2 | Team_3 | Team_4

Remember that stdout/stdin is a data, not afile name.

Some new Linux users will be confused when using redirection. If the command gives a series of filenames as arguments, you cannot use those names as arguments.

For example, if you use the search command to search for all files with the .txt extension, you will not be able to route them through a channel to move the found files to the new folder, not directly:

Figure it out. -type f -name *.txt | mv destination_directory

For this reason, you will often encounter a command that is used in combination with the exec or xargs command. These special commands convert text with a number of filenames into a filename that can be passed as an argument.

Figure it out. -Type f – Name *.txt | xargs -t -I{} mv {} ../new_dir

Input forwarding

Input Output & Error Redirect in Linux

You can use stdin redirection to pass the content of a text file to a command similar to this one:


You won’t see how much Sdin is used. This is because most Linux commands accept filenames as an argument, so stdin redirection is often not necessary.

Take this, for example:


The above command can simply be main filename.txt (without <).

It’s not that Sdin’s distraction is completely useless. Some teams are counting on him. Let’s take the tr. This command can do a lot of things, but in the example below, it converts text typed from lowercase to uppercase:

tr a-z A-Z <Filmname.txt

In fact, the use of stdin through the hose is specifically recommended to avoid unnecessary use of cat control.

For example, many people would use the above example of the cat and then apply a trance to the cat. To be honest, there’s no need to put a cat in here.

chat filename.txt | tr a-z A-Z

Combined bypass

You can combine stdin, stdout and hose redirection according to your needs.

For example, the following command lists all .txt files in the current directory, then counts those .txt files and saves the result in a new file.

ls *.txt | wc -l > count.txt

Fault transmission

Sometimes, when you execute a command or script, you will see an error message on the screen.

[email protected]:~$ ls -l fffffffffffffffffffffffffffffffffffffffffffff > output.txt
ls : kan geen toegang krijgen tot ‘fffffffffffffffffffffffffffffffffffffffffffffffff’ : Such a file or folder does not exist.

At the beginning of this article I mentioned that there are three data streams and that stderr is one of the output streams displayed on the standard screen.

You can adjust the route and the bullr. Since this is an output data stream, you can use the same > or >> redirect character as the character used to redirect stdout.

But how do you distinguish stdout from stderr when both are an output stream? By its stream identification (also called file descriptor).

Data flow River identifier
stdin 0
boldly 1
stderr 2

If you use the output redirect icon >, it means 1> by default. In other words, the data stream with ID 1 is displayed here.

If you need to redirect stderr, use his ID as 2> or 2>>. This means that the output bypass for the stderr data stream (ID 2) is done.

Examples of Stderder bypass

I’ll show you some examples. Assuming you only want to record the error, you can use something like this:

[email protected] manual:~$ ls fffffffffffffffffffff 2> error.txt
[email protected] manual:~$ cat error.txt
ls : cannot access ‘fffffffffffffffffffffffffffffffff’ : Such a file or folder does not exist.

That was easy. Let’s make things a little more complicated (and useful):

[email protected] manual:~$ ls -l new.txt ffff > output.txt 2> error.txt
[email protected] manual:~$ cat output.txt
-rw-rw-r—- 1 abhishek abhishek 0 May 5 10:25 new.txt
[email protected] manual:~$ cat error.txt
ls : no access to ‘ffff’ : Such a file or folder does not exist.

In the example above, the ls command tries to display two files. For one file it’s a success, for another it’s an error. So, what I’ve done here is redirect stdout to ouput.txt (with >) and stderr to error.txt (with 2>)

You can also forward stdout and stderr to the same file. There are ways to do this.

In the following example I first send stderr (with 2>>) to the combined.txt file in application mode. And then stdout (with >>) is sent to the same file in application mode.

[email protected] manual:~$ ls -l new.txt fff 2>> combination.txt >> combination.txt
[email protected] manual:~$ cat combined.txt
ls : cannot access ‘fff’ : No such file or directory
-rw-rw-r— 1 abhishek abhishek 0 May 5 10:25 new.txt

The other way, and this is preferable, is to use something like 2>&1. This can be roughly translated by redirecting stderr to the same address as stdout.

Take the previous example and this time use 2>&1 to redirect stdout and stderr to the same file.

[email protected] manual:~$ ls -l new.txt fff > output.txt 2>&1
[email protected] manual:~$ cat output.txt
ls : cannot access ‘fff’ : No such file or directory
-rw-rw-r— 1 abhishek abhishek 0 May 5 10:25 new.txt

Remember that you cannot use 2>>&1 if you are thinking of using it in application mode. 2>>&1 is already in the add-mode.

You can also use 2> first, then 1>&2 to redirect stdout to the same file as stderr. In fact, it is >& that redirects one output stream to another.

– There are three data streams. One input, stdin (0) and two output data streams, stdout (1) and stderr (2).

– The keypad is the standard device and the display is the standard output device.

– The transmission of results is used with > or >> (for addition mode).

– Input signal transmission is used with <.

– The steering unit can be diverted by 2> or 2>>.

– Steader and stdout can be combined with 2>&1

I hope you enjoyed this detailed guide to redirecting to Linux. If you still have doubts or suggestions to improve this article, please let me know in the comments section.

Did you like the article? Share with us and help us grow ?redirection in linux with examples,input/output redirection in c,redirection in unix geeksforgeeks,input redirection example,piping in linux,input output redirection in linux in hindi,which command use the output of a program as the input of another one?,input and output in linux operating system