Linux Redirects and Pipes [Basic Guide]

Linux Redirects and Pipes

An important concept in Linux is redirection. Since everything in Linux is a file, it was necessary for commands and processes to have the ability to handle data inputs and outputs with great ease.

Before we detail Linux’s redirection ability we need to define what are standard inputs, standard output, and error output.

Standard input stdin -> 0

Standard Input (stdin) is the input to a text flow. Examples include the keyboard, the mouse, a pendrive, etc. All of them feed the computer with information. It can be represented by the number 0.

Standard output stdout -> 1

The Standard Output (stdout) is the output from a text stream under normal conditions. Examples include the monitor, the printer, the floppy disk, a file, etc. They all receive information from the computer. It can be represented by the number 1.

Stderr -> 2 error output

The Error Output (stderr) is the output of a text stream in error or failure conditions in a particular processing. The error output may be directed to the monitor or to a LOG file. It may be represented by the number 2.

We can redirect the input standard or the standard output of the data flow of a given process for another process or a file very easily.

Redirect to files with > or >>

To redirect a result from one output to another output we use the larger sign (>) or (>>).

The > sign simply rewrites the output file; the >> sign adds output at the end of the file without deleting the content.

Redirect from files with <

or to direct one input to another input we use the minor sign (<).

Conductors… drive something from one process to another (| or pipe)

We can still redirect an output to an input using a special vertical bar, the sign in English called “pipe” (|) or duct.

$ ls > saida.txt

In this example we list the files in a directory by saving the result in the saída.txt file.

The redirection is using mainly through programs or when the result of processing will not be observed directly by the user but sent to another program, to a file or for a device.

$ mail [email protected] < e-mail.txt

Here the mail program is receiving as an argument the address of e-mail and instead of using the keyboard as standard input, we are redirecting the text file e-mail.txt.

$. /payroll > file1

Ou

$. /payroll 1 > file1

We are sending all the output of Payroll program for file1

$. /payroll 2 > arquivo_erro.log

We are sending all error output to the file file_ erro.log.

$. /payroll > file1 2>&1

We’re sending all standard and error output to the file file:1.

$. /payroll > arquivo_sucesso.log 2> arquivo_erro.log

We are sending the result with the normal output to the file_
sucesso.log is the error output for arquivo_erro.log.

$. /payroll | print_tickets 2> erros.log

We are submitting the result of
Payroll process for the print_bill process and the error output of
print_tickets to the erro.log file.

Wildcards Symbols

Note that you can also use wild symbols to group files together by name or chunks of name.

See the following table

TABLE — Wildcards Symbols

SymbolDescription
*It means “okay”
anything” and can replace one or more characters in a name. By
Example “Certifi*” can mean “Certificate”, “Certification” or any
another combination of file names starting with “Certifi”.
?It means you can
replace a character from a name only. For example: “? Certificate”
can replace “Certificate”,
“certificate” or any other combination of file names that start
with any character and ends with “certificate”.
{text1, text2…}Replaces the part
inside the braces by text1, then by text2, and so on. By
example: “part_ {a, b, c}” will result in “part_a”, “part_b” and “part_c”.

When we are working with files and directories in the shell, it is very common to have to work with several files at once.

To make this type of task simpler, Linux offers the wild card feature for file and directory names.

Just like in the deck of cards, jokers are symbols that can
mean a lot of things. On Linux, they allow you to use them as part.
of the name of the files and directories. See table 6, of jokers:

Examples:

$ ls *.txt

Lists all files with the suffix .txt

$ cat doc??? >> documents

Concatenate all files starting with “doc” that have 6
size characters in the document file.

Now discover a special command that allows the output of a command to be recorded in a file and sent to another command at the same time.

Tee command

Use:

$ tee [options] files

The tee command takes data from a standard input, writes what it received to a file, and then sends it to its standard output.

It is used to save the standard output of a command to a file and also send this output to another command.

The option is:

  • -a: Add what was received at the end of the file instead of writing on top.

Example:

$ payroll | tee payment.txt | print_bill | tee payroll.txt | lpr 2> erros.log

The result of the payroll program is recorded by the tee in the payment.txt file and sent to the print_bill program.

In turn, the print_bill program redirects its output to the second tee, which records the result in the payroll.txt file, which is also sent to the lpr program, which prints the tickets.

If the printing error occurs, this error is written to the erros.log file.

There is another special command that passes what it received as input as arguments for a given command.

xargs command

Use:

$ xargs command [options] initial argument

xargs executes the command and passes what was received as standard input as an argument to that command.

Xargs will execute commands numerous times according to the number of lines received as standard input.

The most common options are:

  • -p: Ask the user if the command should be executed before doing so
  • -r: Don’t execute the command when you receive 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 for the echo command.

The echo command prints the arguments received in the standard output.

$ cat lista-de-usuários.txt | xargs mkdir

In this example the xargs command will create a directory running the 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 the suffix .gz and sends them to the file lista-de-arquivo-zipado.txt.

In the second line, xargs will execute “gunzip” with each file in the list.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?