Now, to understand what redirections are, let's learn how to handle inputs and outputs through special operators.
What are terminal inputs and outputs
In the console we generate an input when we type and an output almost every time we execute a command.
Inputs are typically called Standard Input and outputs Standard Output and are abbreviated as stdin and stdout respectively.
What are file descriptors
File descriptors are numbers that identify a resource. It works by associating a number with an action, file or program, in the case of the shell we have 3 file descriptors:

2 is Standard Error.
How to use the redirection operator (>)
Sometimes we want to save the information of an output because we may be interested in storing what that output contains. Let's see the following example, if you use the command:
ls -l

What happens here is that you gave a Standard Input (the command) and got a Standard Output (the list of files).
If you want the Standard Output to go not to the console but to a file, then you can use the > operator followed by the name of the file where you want to save the output.
ls -l > output.txt

How to concatenate (>>)
Assuming that you already have the output.txt file and now you also want to save the information in the document folder, then you can not run again:ls -l > output.txt
This will rewrite the contents of the document, what you need is to concatenate the contents of the document with the output, for that you run:ls -l >> output.txt

As you can see, the output of the command ls -l
is concatenated with the output of the command ls -l ./StateSecrets
. You can tell because the word total is repeated twice.
By the way, that word total is the total size of the folder in kilobytes and it says that the SecretsState folder weighs 0, because empty files and folders do not take up space.
Error redirection (2>|2>&1)
The default redirection operator only redirects file descriptor 1 (i.e., the Standard Output). But what if we want to redirect an error? Well, we have to specify that we want the Standard Error, which has file descriptor 2.
We are going to generate an error by executing a command that will go wrong to redirect it to a file called "error.txt".

In this case the "ñ" option does not exist, so it produces an error.
We can also specify that no matter what happens if it gives me a Standar Ouput or a Standar Error, it still has to save the output to a file. We do this like this:
ls -l > output.txt 2>&1
The command 2>&1
means that it should redirect file descriptor 2 and file descriptor 1.

On the first execution of the command, it executes correctly and saves the Standar Output, but on the second execution, the command fails and saves the Standar Error.
Table of operators
Operator | Function | | | --- --- --- | | | | | | | Redirects the output. By default redirects the Standar Output | | | >> | | Concatenates the output with whatever the file already has where the output is being redirected to | | | 2> | | Redirects the file descriptor 2 (In this case Standar Error) | | | 2>&1 | Redirects the file descriptor 2 and 1 | | |
Contribution created with contributions from: Miguel Gonzalez.
Want to see more contributions, questions and answers from the community?