Linux Redirections: >, >>, |, 2>, 2>&1 - Complete Guide with Examples
An important concept in Linux is redirection. Since everything in Linux is a file, it became necessary for commands and processes to have the ability to handle data input and output with great ease.
Before we detail Linux's redirection capability, we need to define what standard input, standard output, and error output are.
Standard input stdin -> 0
Standard Input (stdin) is the input of a text stream. Examples include the keyboard, mouse, USB drive, etc. They all feed information to the computer. It can be represented by the number 0.
Standard output stdout -> 1
Standard Output (stdout) is the output of a text stream under normal conditions. Examples include the monitor, printer, floppy disk, a file, etc. They all receive information from the computer. It can be represented by the number 1.
Error output stderr -> 2
Error Output (stderr) is the output of a text stream under error or failure conditions in a given processing. The error output can be directed to the monitor or to a LOG file. It can be represented by the number 2.
We can redirect the standard input or the standard output of a data stream from a given process to another process or file very easily.
Redirecting to files with > or >>
To redirect an output result to another output, we use the greater than sign (>) or (>>).
The > sign simply overwrites the output file; The >> sign appends the output at the end of the file, without deleting the content.
Redirecting from files with < or
And to direct an input to another input, we use the less than sign (<).
Pipes... conduct something from one process to another ( | or pipe)
We can also redirect an output to an input using a special vertical bar, the English signal called "pipe" (|) or duct.
$ ls > saida.txt
In this example we list the files in a directory, saving the result to the saida.txt file.
Redirection is mainly used by programs or when the result of a processing will not be observed directly by the user, but sent to another program, to a file, or to a device.
$ mail [email protected] < e-mail.txt
Here the mail program is receiving the email address as an argument, and instead of using the keyboard as standard input, we are redirecting the text file e-mail.txt.
$ ./folha_pagamento > arquivo1
Or
$ ./folha_pagamento 1 > arquivo1
We are sending all output from the folha_pagamento program to arquivo1
$ ./folha_pagamento 2 > arquivo_erro.log
We are sending all error output to the arquivo_erro.log file.
$ ./folha_pagamento > arquivo1 2>&1
We are sending all standard output and errors to the arquivo1 file.
$ ./folha_pagamento > arquivo_sucesso.log 2> arquivo_erro.log
We are sending the result with normal output to arquivo_sucesso.log and error output to arquivo_erro.log.
$ ./folha_pagamento | imprime_boletos 2> erros.log
We are sending the result of the folha_pagamento process to the imprime_boletos process and the error output of imprime_boletos to the erro.log file.
Wildcard Symbols
Note that you can also use wildcard symbols to group files by name or parts of names.
See the following table
TABLE – Wildcard Symbols
| Symbol | Description |
| * | Means "matches anything" and can replace one or more characters in a name. For example "Certifi*" can mean "Certificate", "Certification", or any other combination of filenames that start with "Certifi". |
| ? | Means it can replace only one character in a name. For example: "?ertificate" can replace "Certificate", "certificate", or any other combination of filenames that start with any character and end with "certificate". |
| {text1, text2...} | Replaces the part inside the braces with text1, then text2, and so on. For example: "part_{a,b,c}" will result in "part_a", "part_b", and "part_c". |
When working with files and directories in the shell, it's very common to need to work with several files at once.
To make this type of task simpler, Linux offers the wildcard feature for filenames and directory names.
Like in a deck of cards, wildcards are symbols that can mean many things. In Linux they allow you to use them as part of filenames and directory names. See table 6, wildcards:
Examples:
$ ls *.txt
Lists all files with the .txt suffix
$ cat doc??? >> documentos
Concatenates all files starting with "doc" that are 6 characters long into the documentos file.
Now get to know a special command that allows the output of a command to be simultaneously saved to a file and sent to another command.
The tee command
Usage:
$ tee [options] files
The tee command reads data from standard input, writes what it received to a file, and also sends it to its standard output.
It is used to write the standard output of a command to a file and also send this output to another command.
The option is:
- -a: Appends what was received to the end of the file instead of overwriting.
Example:
$ folha_pagamento | tee folha.txt | imprime_boleto | tee boleto.txt | lpr 2> erros.log
The result of the folha_pagamento program is written by tee to the file folha.txt and sent to the imprime_boleto program.
In turn, the imprime_boleto program redirects its output to the second tee which writes the result to the file boleto.txt which is also sent to the lpr program that prints the tickets.
If there is a printing error, this error is written to the erros.log file.
There is another special command that passes what it received as input as arguments to a given command.
xargs command
Usage:
$ xargs command [options] initialargument
xargs executes the command and passes what was received as standard input as arguments to that command.
xargs will execute the command multiple times according to the number of lines received as standard input.
The most common options are:
- -p: Ask the user whether to execute the command before doing it
- -r: Do not execute the command when receiving empty lines
- -t: Show the command on the screen before executing it
Examples:
$ cat LEIAME.TXT | xargs echo
In this example each line of the LEIAME.TXT file is passed as argument to the echo command.
The echo command prints the received arguments to standard output.
$ cat lista-de-usuários.txt | xargs mkdir
In this example the xargs command will create a directory by executing mkdir with the name of each user contained in the lista-de-usuários.txt file.
$ ls -1 *.gz > lista-de-arquivo-zipado.txt
$ cat lista-de-arquivo-zipado.txt | guzip
In this example the first line lists all files with .gz suffix and sends them to the lista-de-arquivo-zipado.txt file.
On the second line xargs will execute "gunzip" with each file from the list.
Watch the video below and there will be no doubts left:
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.