Input and Output Redirection

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Input and Output Redirection

Input and Output Redirection

Module 9

Input and Output Redirection

Introduction One important thing you have to know to understand I/O redirection is file descriptors. There are three types of I/O, called a file descriptor. They are standard input, standard output, standard error. Topics Input Redirection Output Redirection Error Redirection Filter Pipes

Input and Output Redirection

Input Redirection
The standard input (stdin) by default is any text entered from the

keyboard.
Standard input can be redirected through the use of command line

argument and redirection operators (like <) so that it becomes a source other than the keyboard argument.
The input redirection is generally useful if you have a data file and a

command that expects input from standard input.

Contd

Input and Output Redirection

Input Redirection
Example of input redirection

Input and Output Redirection

Output Redirection
The default standard output (stdout) is the display screen.

The command line programs automatically send their output to the

screen.
Standard output can be redirected through the use of command line

argument and redirection operators (like >).


The standard output can be redirected from the screen to any file or to

the printer.

Contd

Input and Output Redirection

Output Redirection
Example of output redirection # ls > list.file

Syntax # command > (file name

Input and Output Redirection

Error Redirection
By default the standard error (stderr) displays on the terminal and can

be redirected into a file, printer and any other program.

Standard error can be redirected with the standard error redirection operator (i.e 2>, 2>>).

The error messages can be redirected to any file, so that the error

messages will not appear on the screen and it is written to the file.

Contd

Input and Output Redirection

Error Redirection
Example of error redirection # ll myfile 2>error_message

Syntax command (filename) 2> (filename)

Input and Output Redirection

Common Redirection Operators


There are two common redirection operators

> >>
> operator sends the input of command or file to the other file as standard input, overwriting the existing file. > > operator sends the input of command or file to the other file as standard input, appending text to an existing file, instead of overwriting the file.

Contd

Input and Output Redirection

Common Redirection Operators


Usage of > operator

Syntax # cat > (filename)

Contd

Input and Output Redirection

Common Redirection Operators


Usage of the >> operator

Contd

Input and Output Redirection

Filters
Filters take standard input and perform an operation upon it and

send the results to standard output.

Example for filters: grep root /etc/passwd The grep command is used to filter the root user from the /etc/passwd file.

Input and Output Redirection

Pipes
A pipe is a mechanism for interprocess communication.

The data written to the pipe by one process can be read by another

process. It is handled in a first-in, first-out (FIFO) order.


Pipes ( | ) let you redirected output from one command to become input

to another command.

Example: simple pipe with more ls -l | more

Contd

Input and Output Redirection

Pipes
The tee command

The tee command copies the standard input to standard output and also to any files given with an argument.
This is useful when you want not only to send some data down a pipe, but also to save a copy. If a file being written which does not already exist, it is created.

If a file being written which already exists, the data contained is overwritten unless the -a option (appends the standard input to the given files rather than overwriting)
Contd

Input and Output Redirection

Pipes
Example of tee command

Syntax tee <option> <file name>

Input and Output Redirection

Lab Exercise
Filter and display the word blue from the file my_sortfile

Sort and redirect the data from one file to another

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy