Redirecting and Piping Command through the Command Line
Tags: beginners, codenewbie, 100daysofcode, tutorial
Published: 2023-08-07 18:43:04

## Standard Streams

* The three standard streams are communication channels between a computer program and its environment.
    
* They are:
    
    * Standard Input
        
    * Standard Output
        
    * Standard Error
        
    
    ## Standard Output
    
    * Standard output is a place to which a program or command can send information.
        
    * The information could go to a screen to be displayed, to a file, or even to a printer or other devices.
      
![photo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22vmmr84346ebx726dcc.png)
## Standard Error
* Commands and programs also have a destination to send error 
 messages: standard error.
* By default, the shell directs standard error information to 
the screen for us to read, but we can change that 
destination if needed!
        
    
    ## Standard Input
    
    * Standard input is where a program or command gets its input information from. By default, the shell directs standard input from the keyboard.
        
    * The input information could come from a keyboard, a file, or even from another command!
        
     
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enr2wudbel8h3zbxuwro.png)
        

## Redirection

* "redirection" describes the ways we can alter the source of standard input, and the destinations for standard output and standard error.
    
    ## Redirecting Output
    
    * The redirect output symbol (**>**) tells the shell to redirect the output of a command to a specific file instead of the screen.
        
    * By default, the `date` command will print the current date to the screen. If we instead run `date > output.txt` the output will be redirected to a file called output.txt
        
    * If the file does not exist, it will be automatically created.
        
    * **Note**: the &gt; symbol needs to occur after any options and arguments!
        
        ```bash
        command > filename
        echo "moo" > cow.js # redirects the output of echo
        ls -l > files.txt # saves the output of ls -l to a file.
        ```
        
    
    ## Appending
    
    * When we redirect output into a file using **>** any existing contents in the file are overwritten. Sometimes this is not what we want!
        
    * Instead, keep the existing contents in the file and add new content to the end of the file, use **>>** when redirecting.
        
        ```bash
        echo "hello" >> greeting.txt
        echo "world" >> greeting.txt
        cat greeting.txt
        #hello
        #world
        ```
        
    
    ## Redirecting Input
    
    * To pass the contents of a file to standard input, use the **&lt;** symbol followed by the filename.
        
    * For example, we could pass the contents of the chickens.txt file to the cat command using `cat < chickens.txt`
        
    * cat (and many other commands) are set up to accept filenames as arguments directly, but we can also redirect to standard input manually.
        
        ```bash
        command < filename
        cat < chickens.txt
        ```
        
    * We can redirect standard input and output at the same time! In this example, we are using cat to read in the contents of original.txt and then redirecting the output to a file called output.txt
        
    * In this example, we are redirecting the names.txt file to the sort command. We are also redirecting the output of sort to a file called sorted.txt
        
        ```bash
        cat < original.txt > output.txt
        sort < names.txt > sorted.txt
        ```
        
    
    ## Redirecting Standard Error
    
    * By default, error messages are output to the screen, but we can change this by redirecting standard error.
        
    * The standard error redirection operator is `2>`
        
    * If we ran a command like `cat nonexistentfile` (where the file does not exist) we would see an error printed to the screen. We can instead redirect standard error to a file with `cat nonexistentfile 2> error.txt`
        

We can use the same &gt;&gt; syntax to append when redirecting standard error.

```bash
cat nonexistentfile 2> error.txt
ls -zzz 2>> error.txt # ls with illegal option
```

## Why 2&gt; ?

* Each stream gets its own numeric file descriptor, and for standard error the number is 2.
    
* The &gt; operator actually defaults to using 1 as the file descriptor number, which is why we didn't need to specify 1&gt; to redirect standard output
    
* Similarly, the &lt; operator uses a default file descriptor number of 0, so we don't need to specify 0&lt; to redirect to standard input (though we can!)
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lds9y2rkse6wvm6azfh.png)    

## All Together now

* We can redirect multiple streams at once! In this example, we are concatenating two files, redirecting standard output to a file called insects.txt, and redirecting standard error to a file called error.txt
    
* When redirecting both standard output and standard error, make sure standard output comes FIRST. Always redirect standard error after standard output.
    
    ```bash
    cat bees.txt ants.txt > insects.txt 2> error.txt
    ```
    
  
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib79fx2oc6skqnx02w7t.png)
## Pipes

* Pipes are used to redirect a stream from one program to another program. We can take the output of one command and redirect it to the input of another.
    
* We use the pipe character ( | ) to separate two commands. The output of the first command will be passed to the standard input of the second command.
    
* This example shows the output of the date command being piped to the rev command. The result is the reverse of the current date!
    
    ```bash
    command1 | command2
    date | rev
    ```
    
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1ma41vdrqgqyg0dizlp.png)
    
* This example pipes the output of **ls** to **less**. The /usr/bin directory typically contains a bunch of stuff, so it can be nice to use less to read the results in a more manageable way.
    
    ```bash
    ls -l /usr/bin | less
    ```
    
* This example counts the number of files (non hidden files) in a directory. We pipe the output of ls to the word count command. The -l option tells wc to count the number of lines.
    
    ```bash
    ls | wc -l
    ```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bz65qfupma47d0xyzni7.png)
    
* In this example, we are calling tac with a file and then piping the output to rev. The final result is the content of file.txt printed "horizontally" and "vertically" reversed
    
    ```bash
    tac file.txt | rev
    ```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6is2paxpwqchvajjl74g.png)
    
* This example concatenates two files using cat and then sorts them alphabetically
    
    ```bash
    cat colors.txt pets.txt | sort
    ```
    

## \&gt; vs |

* Though both the &gt; character and the | character are used to redirect output, they do it in very different ways.
    
* **\&gt;** connects a command to some file.
    
* | connects a command to another command.
    
    ```bash
    ls -l /usr/bin > list.txt
    ls -l /usr/bin | less
    ```

## `tee`

* What if I wanted to create a file with the output of cat? Enter the tee command
    
* The tee program reads standard input and copies it both to standard output AND to a file. This allows us to capture information part of the way through a pipeline, without interrupting the flow.
    
* It takes some informations coming from one command and gives it to a file we specify and also passes it to the next command. Hence this allows us to take information in the middle of a pipeline and save it to a file without stopping.
    
    ```bash
    command1 | tee file.txt | command2
    cat file1 file2 file3 | tee combo.txt | wc -w
    ```
    

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erzmk77oyx9eb6tql36h.png)