<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lucas Salustiano</title>
    <description>The latest articles on DEV Community by Lucas Salustiano (@lucasalustiano).</description>
    <link>https://dev.to/lucasalustiano</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F294245%2F6b5ac8d7-0509-4696-a049-787b25eece31.jpg</url>
      <title>DEV Community: Lucas Salustiano</title>
      <link>https://dev.to/lucasalustiano</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lucasalustiano"/>
    <language>en</language>
    <item>
      <title>A brief introduction in redirections, pipes, and the tee command</title>
      <dc:creator>Lucas Salustiano</dc:creator>
      <pubDate>Sun, 04 Oct 2020 19:12:22 +0000</pubDate>
      <link>https://dev.to/lucasalustiano/a-brief-introduction-in-redirections-pipes-and-the-tee-command-1plm</link>
      <guid>https://dev.to/lucasalustiano/a-brief-introduction-in-redirections-pipes-and-the-tee-command-1plm</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Redirection is the most basic form of I/O manipulation in bash. It's used to change the data source or destination of a program's file descriptor. &lt;/p&gt;

&lt;p&gt;On the other hand, pipes a good way to connect the output from program A to the input of program B.&lt;/p&gt;

&lt;p&gt;Good knowledge about redirection and pipes is fundamental to work effortlessly in the terminal, so let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  File Descriptors
&lt;/h2&gt;

&lt;p&gt;First of all, before we talk about redirections, we need to understand file descriptors. File descriptors are kind of pointers to sources of data or places it can be written. There are three types of file descriptors:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fui7sdltw18q7drix4l3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fui7sdltw18q7drix4l3h.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Standard Input  [stdin]  -&amp;gt; file descriptor number 0&lt;/li&gt;
&lt;li&gt;Standard Output [stdout] -&amp;gt; file descriptor number 1&lt;/li&gt;
&lt;li&gt;Standard Error  [stderr] -&amp;gt; file descriptor number 2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Standard input refers to the way how bash sees the characters you type. Standard output refers to the way/location where the output should be redirected/displayed. And standard error refers to where the program sends its error messages.&lt;/p&gt;

&lt;p&gt;Now you know about file descriptors, let's see how to apply it to redirect data in our commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  File redirections
&lt;/h2&gt;

&lt;p&gt;How was said in the beginning, redirections are the most basic form of I/O manipulation in bash, so we can do, for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffnxxw5y48dz6aqhdh83j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffnxxw5y48dz6aqhdh83j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Somethings are happening here. First, we invoked the command &lt;em&gt;echo&lt;/em&gt; that prints some data in the terminal, by default. We are giving a string as an argument to the echo command, after this, we are redirecting the output of the echo command to the file called &lt;em&gt;file.txt&lt;/em&gt;. The symbol &lt;code&gt;1&amp;gt;&lt;/code&gt; denotes the redirection of the standard output (attention to the number 1) from the previous command, in this case, the echo command, into a file. Both symbols &lt;code&gt;1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; work the same. Note: Because it's a write operation, both symbols &lt;code&gt;1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; overrides the content of the file that was redirected to, so if you want to append the current output to the previous content of the file, you must use the symbol &lt;code&gt;1&amp;gt;&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also give an argument to a command from a file, for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl2cgzw98f1udt78sgzd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl2cgzw98f1udt78sgzd3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, we invoked the echo command but stead pass directly a string as an argument, we redirected the content of the &lt;em&gt;file.txt&lt;/em&gt; to the standard input of command echo (attention to the number 0). The symbol &lt;code&gt;&amp;lt;&lt;/code&gt; without the &lt;em&gt;0&lt;/em&gt; works the same.&lt;/p&gt;

&lt;p&gt;We can redirect the errors from our commands as well, for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz68p84n87rvtsqvzxt34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz68p84n87rvtsqvzxt34.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the first command, we are creating three folders in the current directory, in this case, folder3 inside folder2 inside folder1. &lt;/p&gt;

&lt;p&gt;In the second command, we are removing the folder1 and redirect the output error to the &lt;em&gt;log.txt&lt;/em&gt; file. This command will not work and will raise an error because we don't use the &lt;code&gt;-r&lt;/code&gt; flag to indicate the recursive remove, so we can catch the error message. Note that the error will not be printed in our terminal, but will be redirected to the log.txt file. Just like the standard output, the symbol &lt;code&gt;2&amp;gt;&lt;/code&gt; will overwrite the content of the log.txt, if it exists. If you want to append the current output error message, you must use the &lt;code&gt;2&amp;gt;&amp;gt;&lt;/code&gt; symbol. Note that number 2 is mandatory in the symbol &lt;code&gt;2&amp;gt;&lt;/code&gt; or &lt;code&gt;2&amp;gt;&amp;gt;&lt;/code&gt; to indicate the standard error message.&lt;/p&gt;

&lt;p&gt;In the third command, we are using the cat command to see what is the content of the file log.txt.&lt;/p&gt;

&lt;p&gt;Now we have a good understanding of how redirection works, let's take a look at pipes and how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pipes
&lt;/h2&gt;

&lt;p&gt;Pipes is another way to create a flow between commands. With redirections, we &lt;em&gt;redirect streams to/from files&lt;/em&gt;. With pipes, we &lt;em&gt;redirect/connect streams from one process (one command) to another process (another command)&lt;/em&gt;. In this way, we can combine a bunch of commands to perform more complex tasks. A pipe is denoted by the symbol &lt;code&gt;|&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;here are some examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F81mz7ahh6bw063hxxo0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F81mz7ahh6bw063hxxo0k.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The firsts two commands will work well, however, the third command will not work completely. That's because when we redirect output to a file, the data 'ends' there, so what can we do? We can use the tee command.&lt;/p&gt;

&lt;h2&gt;
  
  
  The &lt;em&gt;tee&lt;/em&gt; command
&lt;/h2&gt;

&lt;p&gt;The tee command is a command to kind take a snapshot of the data you are working with, so you can send the data to a file &lt;em&gt;and&lt;/em&gt; pipe the data into another command and keep the flow of the pipeline. Let's see how  we can fix the third command to run correctly:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx3o6xgezhxy70upaz5d5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx3o6xgezhxy70upaz5d5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some explanations about what is happening. First, we have our &lt;code&gt;ls&lt;/code&gt; command with some options and the folder we are interested in is &lt;code&gt;/etc&lt;/code&gt;. Till now, we have all the output from the &lt;code&gt;ls&lt;/code&gt; command and now we pipe all this output into the &lt;code&gt;tee&lt;/code&gt; command. The &lt;code&gt;tee&lt;/code&gt; command will take a snapshot (you can imagine its as a copy of the data) of all the output from the ls command, will insert into the &lt;code&gt;etc_content.txt&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; will also pass the same data to the &lt;code&gt;sort&lt;/code&gt; command. Now, the &lt;code&gt;sort&lt;/code&gt; command will process that data and will redirect the output to the &lt;code&gt;sorted.txt&lt;/code&gt;. We can also use multiples tee commands to redirect outputs into files and keep the flow of the pipeline.&lt;/p&gt;

&lt;p&gt;Now let's take an overview of all that we have seen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;command 1&amp;gt; file&lt;/code&gt; or &lt;code&gt;command &amp;gt; file&lt;/code&gt; will redirect the standard output from the &lt;em&gt;command&lt;/em&gt; to the &lt;em&gt;file&lt;/em&gt; and both of then will override the previous content from the file. To append new data into file, we must use &lt;code&gt;command 1&amp;gt;&amp;gt; file&lt;/code&gt; or &lt;code&gt;command &amp;gt;&amp;gt; file&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;command 2&amp;gt; file&lt;/code&gt; will redirect the standard error from the &lt;em&gt;command&lt;/em&gt; to the &lt;em&gt;file&lt;/em&gt; and it will override the previous content from the file. To append new data into the file, we must use &lt;code&gt;command 2&amp;gt;&amp;gt; file&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;command 0&amp;lt; file&lt;/code&gt; or &lt;code&gt;command &amp;lt; file&lt;/code&gt; will redirect the data from &lt;em&gt;file&lt;/em&gt; to the standard input of the &lt;em&gt;command&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;command1 | command2 | command3 | ..&lt;/code&gt; will pipe the standard output from the first command into the standard input of the second command and so on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;command1 | command2 &amp;gt;&amp;gt; file&lt;/code&gt; will pipe the standard output from the first command into the standard input of the second command, and the second command will redirect its standard output into a file. Note that the &lt;em&gt;redirection is at the end of the pipeline&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To make a &lt;em&gt;redirection before the end of the pipeline&lt;/em&gt;, we must use the tee command. It will allow us to do both, pipe and redirect the data we are working with.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understand the command line, its inputs, and outputs to perform complex flows and tasks for us, is an essential skill. Once we have a deep understanding of how it works, the other topics will be easier to learn (and to teach).&lt;/p&gt;

</description>
      <category>linux</category>
      <category>tutorial</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
