0% found this document useful (0 votes)
46 views

7 - IO Redirection and Piping

The document discusses input/output redirection and piping in Unix/Linux systems. By default, commands get input from standard input and output to standard output. Redirection allows inputting from and outputting to files instead of the terminal. Piping connects the output of one command to the input of another to combine their functions. File descriptors associate programs with standard input, output, and error streams.

Uploaded by

Drashty Ranpara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

7 - IO Redirection and Piping

The document discusses input/output redirection and piping in Unix/Linux systems. By default, commands get input from standard input and output to standard output. Redirection allows inputting from and outputting to files instead of the terminal. Piping connects the output of one command to the input of another to combine their functions. File descriptors associate programs with standard input, output, and error streams.

Uploaded by

Drashty Ranpara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

IO REDIRECTION AND PIPING

 In typical Unix installations, commands are entered


at the keyboard and output resulting from these
commands is displayed on the computer screen.

 Thus, input (by default) comes from the terminal and


the resulting output (stream) is displayed on (or
directed to) the monitor.
 Commands typically get their input from a source
referred to as standard input (stdin) and typically
display their output to a destination referred to
as standard output (stdout).
 As depicted in the diagram above, input flows (by
default) as a stream of bytes from standard input
along a channel, is then manipulated (or generated)
by the command, and command output is then
directed to the standard output.
 The ls command can then be described as follows;
there is really no input (other than the command
itself) and the ls command produces output which
flows to the destination of stdout (the terminal
screen.
Concept of Redirection
 There are default files where a command reads its
input from and sends its output and error messages
to.

 In UNIX these three files are known as standard


files for the command.

 The input, output, and the error of a command can


be redirected to other files by using file redirection
facilities in UNIX.
 Whenever you run a program you get some output
at the shell prompt. In case you don't want that
output to appear in the shell window, you can
redirect it elsewhere.

 you can make the output go into a file...or maybe


go directly to the printer.. or you could make it
disappear.

 This is known as Redirection. Not only can the


output of programs be redirected, you can also
redirect the input for programs.
 This allows us to connect several commands
together to perform a complex task that can not be
performed by a single command.

 In UNIX, three files are automatically opened by


the kernel for every command to read input, send
output and error messages.

 These files are known as


 standard input –stdin
 standard output – stdout
 standard error -stderr-.
File Descriptors
 One important thing you have to know to understand
Redirection is file descriptors.

 Similarly even your screen on which your programs


display their output are files for Unix. These have
file descriptors associated with it.

 So when a program actually executes it sends its


output to this file descriptor and since this particular
file descriptor happens to be pointing to the screen,
the output gets displayed on the screen.
 Had it been the file descriptor of the printer, the
output would have been printed by the printer.

File Descriptor Description


0 Standard Input (Generally Keyboard)
1 Standard Output (Generally Display / Screen)
2 Standard Error Output (Generally Display / Screen)
Standard Stream
 Whenever any program is executed it is associated
with three important files.

 They are standard input, standard output and


standard error.

 Each command takes its input from the stream


known as standard input.
 The command that creates output send it to a stream
known as standard output.

 If an executing command encounters an error, the


error message is sent to the standard error.

2 std. Error

0-std. Input Command

1 std. Error
Redirecting Input
 Input Redirection is not as popular as Output
Redirection. Since most of the times you would
expect the input to be typed at the keyboard.

 But when it is used effectively, Input Redirection can


be of great use.

 The general use of Input Redirection is when you


have some kind of file, which you have ready and
now you would like to use some command on that
file.

2 std. Error

0- Redirected Input Command

1 std. Error

Redirecting the standard Input


 To get input for a UNIX command from a file
instead of the terminal, enter the command as
follows:

Command option arguments < inputfile

 The command, options and arguments are as you


would normally enter it.

 The input_file name is the file from which the


command is to get its input.
 For Ex.
 The wc command counts the number of bytes, word and
lines in a file. Thus if we do the following using the file
created above, we see:

$ wc < my_files

 The file name is not listed. This is because the wc


command does not know the name of the file, only that it
received a stream of bytes to count.

Redirecting Output

2 std. Error

0- Redirected Input Command

Redirecting the standard Input


 To send the output generated by a UNIX command
to a file rather than to the terminal, enter the
command as follows:

Command options argument > outputfile

 Here command, options, and argument with the full


command as you would normally enter it.

 The output file is the file to which this command


will send its output.
who > users
 Notice that no output appears at the terminal.
This is because the output has been redirected from
the default standard output device (the terminal)
into the specified file.

 If you would check users file then it would have


complete content:

Combine input and output redirection
command < input_file > output_file

 $cat < sample > sample1


the above command takes its input from the file
sample and sends its output to the file sample1
Pipes
 Piping is used in one of the following ways: to tell a
command to read its input from another command’s
output, or to tell each command in a string of
commands to get its input from the output of the last
command.
Getting input from another command
 To tell a command to get its input from another
command.

Another_command | command

 Here another command with the command that is to


generate the output and replace command with the
command that is to get the output.
 A common way to use piping is with the more
command, which causes its input to print at the
terminal one screenful at a time.

 man | more

 ls –l | cut –c 2-10 > test.txt

You might also like