Hardware

Linux pipes and redirects

Table of contents:

Anonim

Many already know that in Linux, the terminal provides us with valuable tools to refine and optimize the results we seek. In this post we take advantage of teaching you how to use redirects and pipes. And it is that the terminal turns out as a box with incredible utilities. It puts at our disposal countless commands and tools that help us carry out our daily tasks on a day-to-day basis.

Index of contents

Redirects and pipes in Linux

Basic concepts

Before going on to explain what pipes are and their importance (the fun part), we must be clear about three fundamental concepts in Linux: standard input, standard output and standard error.

The standard input: represents the data that is necessary for the correct operation of an application. An example of them can be a file with structured data or information entered from the terminal. In the terminal it is represented as type 0.

The standard output: is the means that an application uses to display information on its processes and / or results, these can be simple messages, notices regarding progress or files with structured data such as a process resolution (a report, for example). In the terminal it is represented as type 1.

The standard error: it is the way in which the applications inform us about the problems that can occur at the moment of their execution. It is represented as type 2 in the terminal.

All types are represented as physical files in the system, because as you must have read in a previous post, in Linux, everything is a file.

Redirects

Now what is a redirect?

Redirects consist of moving information from one type to another (the types mentioned above), for example, from standard error to standard output or from standard output to standard input. Through the terminal, we accomplish that by using the> symbol.

Redirect output and standard error

For example, to redirect the output of a command and send it to a file; we just need to execute:

ls -la ~> (file name)

However, if we execute in this way, the content of our file will be replaced, each time, by the command output. If what we want is for this output to be added to the file, then the execution would be as follows:

ls -la ~ >> (file name)

What is interesting is that we can redirect the standard outputs, errors and inputs. It is here where the numbers I mentioned at the beginning make sense. For example, to force a program to show us the errors that are generated during an execution, we redirect the standard error to the standard output during its execution:

application 2 >> & 1

Where 2 represents the standard error and & 1 represents the standard output.

We can also discard the standard error in a certain process, something common in systems administration. For this we execute:

application 2> / dev / null

Even discard the standard output:

application> / dev / null

Since in Linux, the / dev / null file is a special file where the information is sent to be discarded.

Redirect input

In the same way that we redirect standard outputs and errors, we can do it with standard inputs from a file and for this we use the operator <.

WE RECOMMEND YOU The best little-known Linux browsers

This is useful in commands or programs where the arguments are entered by keyboard, in such a way that we can replace them with a file, for example:

echo "Hello world"> greeting cat <greeting Hello world

Take a look at: Linux Commands: Know and manipulate the system

Pipelines

After understanding the operation of redirects, the concept of pipes will be quite simple. Among the principles of the Unix philosophy, we have the fact of having small applications that are in charge of carrying out very specific tasks and that together carry out complex tasks. Following this principle, there must be a way for a set of applications to interact with each other. This is where the so-called pipes arise.

Pipelines are a special type of redirection that allow you to send the standard output of one command as the standard input of another. The way to represent it is with the symbol | (pipe). Its main usefulness is that it offers us the possibility of concatenating commands, enriching programming.

A simple and very useful example is to see the processes that are running on the system with ps and redirect their output to sort to sort them by PID:

ps -a | sort

As you can see, redirects and pipes are fundamental Linux concepts and that we should definitely handle. In this way you will feel more and more comfortable in the terminal.

Tell us in the comments, what would you use or use redirects and pipes for in the terminal?

Hardware

Editor's choice

Back to top button