Input Output Redirection in Linux/Unix Examples

What is Redirection?

Output Redirection, is a function in Linux such that when executing a command, you can exchange the same old enter/output devices.

The fundamental workflow of any Linux command is that it takes an enter and supply an output.

  • The trendy input (stdin) device is the keyboard.
  • The general output (stdout) device is the display.

With redirection, the above standard enter/output can be modified.

In this tutorial, we will learn-

  • Output Redirection
  • Input redirection
  • File Descriptors (FD)
  • Error Redirection
  • Why Error Redirection?
  • Examples

Output Redirection

The ‘>‘ symbol is used for output (STDOUT) redirection.

Example:

ls -al > listings

Here the output of command ls -al is re-directed to file “listings” instead of your screen.

Note: Use the perfect report name whilst redirecting command output to a record. If there is an existing document with the equal name. The redirected command will delete the contents of that report after which it could be overwritten.”

If you do now not need a file to be overwritten however want to add extra content material to an current report, then you must use ‘>>’ operator.

You can redirect standard output, to not just files, but also devices!

$ cat music.mp3 > /dev/audio

The cat command reads the record track.Mp3 and sends the output to /dev/audio that’s the audio tool.

If the sound configurations on your PC are correct, this command will play the document tune.Mp3

Input Redirection

The ‘<‘ symbol is used for input(STDIN) redirection

Example: The mail application in Linux permit you to ship emails from the Terminal.

You can type the contents of the email using the same old device keyboard. But in case you need to connect a File to e mail you may use the enter re-route operator in the following layout.

Mail -s "Subject" to-address < Filename

This would connect the report with the e-mail, and it would be sent to the recipient.

The above examples were simple. Let’s examine some develop re-course techniques which employ File Descriptors.

File Descriptors (FD)

In Linux/Unix, the entirety is a document. Regular record, Directories, or even Devices are files.

Every File has an related number referred to as File Descriptor (FD).

Your display additionally has a File Descriptor.

When a program is achieved the output is sent to File Descriptor of the display screen, and also you see program output for your display.

If the output is sent to File Descriptor of the printer, the program output might have been printed.

Output Redirection, Error Redirection

Whenever you execute a application/command at the terminal, 3 documents are usually open, viz., trendy input, widespread output, wellknown mistakes.

These files are constantly gift every time a application is run.

As defined in advance than a record descriptor, is associated with every of those files.

File File Descriptor
Standard Input STDIN 0
Standard Output STDOUT 1
Standard Error STDERR 2

By default, errors stream is displayed on the display. Error redirection is routing the errors to a file other than the display screen.

Output Redirection, Why Error Redirection?

Error re-route is one of the very popular features of Unix/Linux.

Frequent UNIX users will reckon that many commands give you massive quantities of errors.

  • For example, even as trying to find files, one commonly gets permission denied errors. These errors typically do no longer assist the man or woman trying to find a selected record.
  • While executing shell scripts, you frequently do NOT need blunders messages cluttering up the ordinary software output.

The solution is to re-direct the mistake messages to a file.

Example 1

$ myprogram 2>errorsfile

Above we’re executing a program names myprogram.

The record descriptor for fashionable errors is 2.

Using “2>” we re-direct the error output to a record named “errorfile”

Thus, application output isn’t cluttered with mistakes.

Example 2

Here is another instance which uses locate assertion –

find . -name 'my*' 2>error.log

Using the “find” command, we are searching the “.” current directory for a file with “name” starting with “my”

Example 3 Let’s see a extra complicated instance.

Server Administrators frequently, list directories and preserve each mistakes and cutting-edge-day output proper into a file, which can be processed later. Here is the command.

ls Documents ABC> dirlist 2>&1

Here,

  • which writes the output from one record to the enter of every other record. 2>&1 way that STDERR redirects to the target of STDOUT (that’s the record dirlist)
  • We are redirecting errors output to conventional output which in turn is being re-directed to file dirlist. Hence, both the output is written to document dirlist

Output Redirection, Summary

  • Each record in Linux has a corresponding File Descriptor related to it
  • The keyboard is the usual input device at the identical time as your display is the identical vintage output tool
  • “>” is the output redirection operator. “>>” appends output to an existing file
  • “<” is the input redirection operator “>&”re-directs output of one report to every other.
  • You can re-direct mistakes using its corresponding File Descriptor 2.