Handling terminal outputs with Redirection

6 Sep 2020 ⏱️ 3 min
Handling terminal outputs with Redirection

In this tutorial, you will learn how to handle the outputs and errors in terminal. Using this you can easily watch and navigate through logs, outputs, errors, etc in your favourite editor.

We will be using the redirection and pipe operators to get this done! Before proceeding, we’ll do a basic intro to redirection and pipe operators. Feel free to skip this, if you are already know it.

Let’s begin!


Redirection Operators

Command in linux may take input and result in some output and error. There are three files which are open -

  • Standard input (File descriptor = 0)
  • Standard output (File descriptor = 1)
  • Standard error (File descriptor = 2)

With redirection, we can basically change the standard input/output devices. By default -

  • Standard Input (stdin) - keyboard
  • Standard Output (stdout) - screen

It is of two types -

  • Overrite
    • > - standard output
    • < - standard input
    • 2> - standard error
  • Appends
    • >> - standard output
    • << - standard input
    • 2>> - standard error

Example

  • ls > output.txt

    This basically adds files in current directory (output to ls) to output.txt

  • pwd >> output.txt

    This appends current directory path at the end of the file to output.txt while the previous data still remains there.


Pipe Operator

Pipe operator is a redirection operator which is used to run two or more commands in sequence which they are provided. Pipes are unidirectional i.e data flows from left to right through the pipeline.

Example

  • ls | tail -5

This prints the last 5 lines of the output of ls command. The output of ls is sent to tail command which shows the last 5 lines.


Handling Outputs in Terminal

command > terminal_output.txt

  • Output will be redirected to the file (if the file already exists, it gets overwritten).
  • Output will not be visible in the terminal.
  • Similarly if >> if used the output gets appended to the file.

command 2> terminal_output.txt

  • Error will be redirected to the file (if the file already exists, it gets overwritten).
  • Output will not be visible in the terminal.
  • Similarly if 2>> if used the error gets appended to the file.

command &> terminal_output.txt

  • Both output and error will be redirected to the file (if the file already exists, it gets overwritten).
  • Output will not be visible in the terminal.
  • Similarly if &>> if used both output and error gets appended to the file.

command | tee terminal_output.txt

  • Output will be redirected to the file (if the file already exists, it gets overwritten).
  • Output will also be visible in the terminal.
  • Similarly if -a param is used then output gets appended to the file.

Resources

Awesome! You just learned how to handle outputs and errors in terminal and how to save it to files. This article was focussed more towards handling output but input works in same way. Do try it as an exercise!

Liked the article? Consider supporting me ☕️


I hope you learned something new. Feel free to suggest improvements ✔️

I share regular updates and resources on Twitter. Let’s connect!

Keep exploring 🔎 Keep learning 🚀

Liked the content? Do support :)

Paypal - Mohit Khare
Buy me a coffee