Input/Output Redirection in Linux



The standard streams for input, output, and error. In computing, redirection is a form of inter process communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.

Input/output types:

I/O                          ABBERVITAION    FILE DESCRIPTOR

Standard input           stdin                              0
Standard output         stdout                            1
Standard error           stderr                             2

stdin(0):  it’s connected to the keyboard, most programs read input from this file

stdout(1):  it’s attached to the screen, and all programs send their results to this file

stderr(2): programs send status/error messages to this file which is also attached to the screen

Redirection:

·        >    Redirect standard output to a file.
       Overwrites (truncating) existing contents.

·        <   Redirect standard input from a file to a command.
             Overwrites (truncating) existing contents.

·        >>   Redirect standard output to a file.
        Appends to any existing content.

·        <<   Redirect standard output from a file to a command.
       Appends to any existing content

·        2>   Redirect standard error to a file

·        &    used with redirection to signal that a file descriptor is being used.

·        2>&1   Combine standard error  and standard output

·        >/dev/null     Redirect output to null (trash)

Redirect Standard Output to File:
We can redirect standard output using  “>”  as shown  in below example:-

ls –l /root/Desktop/ >ls.txt

For appending standard output to a existing file, we can use “>>”

ls  /root/Desktop/ >>ls.txt

 Redirect Standard Input to File:
To redirect standard input from a file, we can use “<”

cat   </root/Desktop/file.txt
Redirect Standard Error to File:
To redirect standard error of a command, we can use “2>”

ls dir  2>error.txt

Redirect Standard Input/Output to File:
We can use standard input and standard output redirection at the same time

sort  </root/Desktop/file.txt  >sorted_file.txt


Redirect Standard Output/Error to File:
We can redirect standard output and standard error to different files at the same time

ls ebd.txt  dir  1>out.txt  2>err.txt

Redirect Standard Output/Error to One File:
We can redirect standard output and standard error to same file

ls ebd.txt dir  >out_err.txt   2>&1

Use Input/output Redirection Using Pipes:
We can use pipes to redirect output of a command as input of another command 

cat billy-wiki.list | less

Sending Standard Error to Null Device:
/dev/null is a special file that is used to trash any data that is redirected to it. It is used to discard standard error that is not needed

ls –l ebd.txt dir dir2  2>/dev/null

Sending Standard Error and Standard output to Null Device:
/dev/null is a special file that is used to trash any data that is redirected to it. It is used to discard standard output or error or both that is not needed. Any output that is sent to /dev/null is discarded

ls –l ebd.txt dir dir2  >/dev/null  2>&1

Input/Output Redirection in Linux Input/Output Redirection in Linux Reviewed by vijay pratap singh on March 26, 2017 Rating: 5

No comments:

Powered by Blogger.