Difference between revisions of "Linux/C2/Redirection-Pipes/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Time !Narration |- |00:00:00 |Welcome to this spoken tutorial on Redirection and Pipes. |- |00:07:00 |I am using Ubuntu 10.04. |- |00:09:00 |We assume that you alre…')
 
Line 3: Line 3:
 
!Narration
 
!Narration
 
|-
 
|-
|00:00:00
+
|00:00
 
|Welcome to this spoken tutorial on Redirection and Pipes.
 
|Welcome to this spoken tutorial on Redirection and Pipes.
 
|-
 
|-
|00:07:00
+
|00:07
 
|I am using Ubuntu 10.04.  
 
|I am using Ubuntu 10.04.  
 
|-
 
|-
|00:09:00
+
|00:09
 
|We assume that you already have an idea about the Linux operating system and have some basic idea about commands.  
 
|We assume that you already have an idea about the Linux operating system and have some basic idea about commands.  
 
|-
 
|-
|00:16:00
+
|00:16
 
|If you are interested, it is available through another spoken tutorial, on the following website  
 
|If you are interested, it is available through another spoken tutorial, on the following website  
 
|-
 
|-
|00:22:00
+
|00:22
 
|Also note that linux is case sensitive .
 
|Also note that linux is case sensitive .
 
|-
 
|-
|00:25:00
+
|00:25
 
|All the commands used in this tutorial are in lower case unless otherwise mentioned.  
 
|All the commands used in this tutorial are in lower case unless otherwise mentioned.  
 
|-
 
|-
|00:32:00
+
|00:32
 
|Most of the work that we do in Linux is through a terminal.
 
|Most of the work that we do in Linux is through a terminal.
 
|-
 
|-
|00:35:00
+
|00:35
 
|When we have to execute a command, we generally type through the keyboard.
 
|When we have to execute a command, we generally type through the keyboard.
 
|-
 
|-
|00:39:00
+
|00:39
 
|Say we have to find the date and time.  
 
|Say we have to find the date and time.  
 
|-
 
|-
|00:41:00
+
|00:41
 
|We simply type through the keyboard "date" and press enter.  
 
|We simply type through the keyboard "date" and press enter.  
 
|-
 
|-
|00:46:00
+
|00:46
 
|So we normally give input through the keyboard.
 
|So we normally give input through the keyboard.
 
|-
 
|-
|00:48:00
+
|00:48
 
|Similarly we see that the output of our command is also displayed at the terminal window.  
 
|Similarly we see that the output of our command is also displayed at the terminal window.  
 
|-
 
|-
|00:56:00
+
|00:56
 
|Also say some error occurs while we are executing some command.  
 
|Also say some error occurs while we are executing some command.  
 
|-
 
|-
|00:59:00
+
|00:59
 
|For example we type "cat space aaa". And press enter.  
 
|For example we type "cat space aaa". And press enter.  
 
|-
 
|-
|01:05:00
+
|01:05
 
|Now no such file by the name aaa exists.  
 
|Now no such file by the name aaa exists.  
 
|-
 
|-
|01:08:00
+
|01:08
 
|So there is an error displayed, saying so.  
 
|So there is an error displayed, saying so.  
 
|-
 
|-
|01:10:00
+
|01:10  
 
|Now this error also comes on the terminal window. Hence we see error reporting is also at the terminal
 
|Now this error also comes on the terminal window. Hence we see error reporting is also at the terminal
 
|-
 
|-
|01:20:00
+
|01:20  
 
|Now inputting, outputting and error reporting are the three special actions related to commands.
 
|Now inputting, outputting and error reporting are the three special actions related to commands.
 
|-
 
|-
|01:24:00
+
|01:24  
 
|Before learning about redirection we should know about two important concepts. That of stream and file descriptor.
 
|Before learning about redirection we should know about two important concepts. That of stream and file descriptor.
 
|-
 
|-
|01:31:00
+
|01:31  
 
|A Linux shell like Bash, receives input and sends output as sequences or streams of characters.  
 
|A Linux shell like Bash, receives input and sends output as sequences or streams of characters.  
 
|-
 
|-
|01:37:00
+
|01:37  
 
|Each character is independent of the one before it and the one after it.  
 
|Each character is independent of the one before it and the one after it.  
 
|-
 
|-
|01:41:00
+
|01:41  
 
|Streams are accessed using file IO techniques.  
 
|Streams are accessed using file IO techniques.  
 
|-
 
|-
|01:44:00
+
|01:44  
 
|It does not matter whether or not the actual stream of characters comes from or goes to a file, a keyboard, a window etc.  
 
|It does not matter whether or not the actual stream of characters comes from or goes to a file, a keyboard, a window etc.  
 
|-
 
|-
|01:51:00
+
|01:51  
 
|In Linux, every open file of a process is associated with an integer number.  
 
|In Linux, every open file of a process is associated with an integer number.  
 
|-
 
|-
|01:57:00
+
|01:57  
 
|These numeric values are known as file descriptors.
 
|These numeric values are known as file descriptors.
 
|-
 
|-
|02:05:00
+
|02:05  
 
|Linux shells use three standard I/O streams.  
 
|Linux shells use three standard I/O streams.  
 
|-
 
|-
|02:08:00
+
|02:08  
 
|Each of them is associated with a well-known file descriptor.  
 
|Each of them is associated with a well-known file descriptor.  
 
|-
 
|-
|02:12:00
+
|02:12  
 
|''stdin is the standard input stream.
 
|''stdin is the standard input stream.
 
|-
 
|-
|02:15:00
+
|02:15  
 
|It provides input to commands.  
 
|It provides input to commands.  
 
|-
 
|-
|02:17:00
+
|02:17  
 
|It has file descriptor 0. ''
 
|It has file descriptor 0. ''
 
|-
 
|-
|02:19:00
+
|02:19  
 
|''stdout'' is the ''standard output stream.  
 
|''stdout'' is the ''standard output stream.  
 
|-
 
|-
|02:22:00
+
|02:22
 
|It'' displays output from commands. It has file descriptor 1.   
 
|It'' displays output from commands. It has file descriptor 1.   
 
|-
 
|-
|02:26:00
+
|02:26
 
|''stderr is the standard error stream.It displays error output from commands. It has file descriptor 2.''
 
|''stderr is the standard error stream.It displays error output from commands. It has file descriptor 2.''
 
|-
 
|-
|02:36:00
+
|02:36
 
|''Input streams provide input to programs.  
 
|''Input streams provide input to programs.  
 
|-
 
|-
|02:40:00
+
|02:40
 
|By default it takes from terminal keystrokes. ''
 
|By default it takes from terminal keystrokes. ''
 
|-
 
|-
|02:44:00
+
|02:44
 
|''Output streams print text characters, by default to the terminal.  
 
|''Output streams print text characters, by default to the terminal.  
 
|-
 
|-
|02:47:00
+
|02:47
 
|The terminal was originally an ASCII typewriter or display terminal.  
 
|The terminal was originally an ASCII typewriter or display terminal.  
 
|-
 
|-
|02:52:00
+
|02:52
 
|But is now more often a text window on a graphical desktop. ''
 
|But is now more often a text window on a graphical desktop. ''
 
|-
 
|-
|02:56:00
+
|02:56
 
|We have seen that the 3 streams are connected to some files by default.  
 
|We have seen that the 3 streams are connected to some files by default.  
 
|-
 
|-
|03:01:00
+
|03:01
 
|But in linux, we can change this default behaviour.
 
|But in linux, we can change this default behaviour.
 
|-
 
|-
|03:04:00
+
|03:04
 
|We can connect these 3 streams to other files.  
 
|We can connect these 3 streams to other files.  
 
|-
 
|-
|03:07:00
+
|03:07
 
|This process is called redirection.  
 
|This process is called redirection.  
 
|-
 
|-
|03:09:00
+
|03:09
 
|Now let us see how redirection is done in the 3 streams.
 
|Now let us see how redirection is done in the 3 streams.
 
|-
 
|-
|03:14:00
+
|03:14
 
|First let us see how the standard input is redirected.
 
|First let us see how the standard input is redirected.
 
|-
 
|-
|03:17:00
+
|03:17
 
|we redirect standardin from a file, using the < (left angled bracket) operator.
 
|we redirect standardin from a file, using the < (left angled bracket) operator.
 
Let us see how.
 
Let us see how.
 
|-
 
|-
|03:22:00
+
|03:22
 
|We know that the use of wc command is to find the number of lines,words and charecters in a file.  
 
|We know that the use of wc command is to find the number of lines,words and charecters in a file.  
 
|-
 
|-
|03:28:00
+
|03:28
 
|Type wc on the terminal window.  
 
|Type wc on the terminal window.  
 
|-
 
|-
|03:31:00
+
|03:31
 
|Now press enter.  
 
|Now press enter.  
 
|-
 
|-
|03:32:00
+
|03:32
 
|What happens?? we have a blinking cursor. It means that we need to enter through the keyboard.
 
|What happens?? we have a blinking cursor. It means that we need to enter through the keyboard.
 
|-
 
|-
|03:37:00
+
|03:37
 
|Enter some text say "This tutorial is very important".  
 
|Enter some text say "This tutorial is very important".  
 
|-
 
|-
|03:46:00
+
|03:46
 
|Now press enter.  
 
|Now press enter.  
 
|-
 
|-
|03:48:00
+
|03:48  
 
|Now press Ctrl and d keys together.
 
|Now press Ctrl and d keys together.
 
|-
 
|-
|03:52:00
+
|03:52  
 
|Now the command will work on the lines that we entered.  
 
|Now the command will work on the lines that we entered.  
 
|-
 
|-
|03:55:00
+
|03:55  
 
|The command will give an output on the terminal.  
 
|The command will give an output on the terminal.  
 
|-
 
|-
|03:57:00
+
|03:57  
 
|Now here no file is given after wc command .  
 
|Now here no file is given after wc command .  
 
|-
 
|-
|04:01:00
+
|04:01  
 
|So it takes input from standard input stream.  
 
|So it takes input from standard input stream.  
 
|-
 
|-
|04:04:00
+
|04:04  
 
|Now the standard input stream is by default connected to the keyboard. Hence wc will take input from keyboard.
 
|Now the standard input stream is by default connected to the keyboard. Hence wc will take input from keyboard.
 
|-
 
|-
|04:12:00
+
|04:12  
 
|Now if we write  
 
|Now if we write  
  
 
"wc space 'left-angled bracket" space test1 dot txt"
 
"wc space 'left-angled bracket" space test1 dot txt"
 
|-
 
|-
|04:19:00
+
|04:19  
 
|what happens is that wc will tell us the no. of lines,words and characters in the file test1 dot txt
 
|what happens is that wc will tell us the no. of lines,words and characters in the file test1 dot txt
 
|-
 
|-
|04:27:00
+
|04:27  
 
|Now type  
 
|Now type  
  
 
"wc space test1 dot txt"
 
"wc space test1 dot txt"
 
|-
 
|-
|04:34:00
+
|04:34  
 
|we see the same result.  
 
|we see the same result.  
 
|-
 
|-
|04:37:00
+
|04:37  
 
|So what is the difference?
 
|So what is the difference?
 
|-
 
|-
|04:39:00
+
|04:39  
 
|When we write "wc space test1dot txt" , the command opens the file test1dot txt and reads from it.  
 
|When we write "wc space test1dot txt" , the command opens the file test1dot txt and reads from it.  
 
|-
 
|-
|04:46:00
+
|04:46  
 
|But when we write "wc space 'left-angled bracket' test1 dot txt", wc still does not get any file to open.  
 
|But when we write "wc space 'left-angled bracket' test1 dot txt", wc still does not get any file to open.  
 
|-
 
|-
|04:53:00
+
|04:53  
 
|Instead ,it looks to pick up the input from standardin.  
 
|Instead ,it looks to pick up the input from standardin.  
 
|-
 
|-
|04:57:00
+
|04:57  
 
|Now we have directed the standardin to the file test1dot txt.  
 
|Now we have directed the standardin to the file test1dot txt.  
 
|-
 
|-
|05:01:00
+
|05:01  
 
|Hence the command reads from test1.  
 
|Hence the command reads from test1.  
 
|-
 
|-
|05:04:00
+
|05:04  
 
|But actually it is unaware as to from where the data is coming to standardin.  
 
|But actually it is unaware as to from where the data is coming to standardin.  
 
|-
 
|-
|05:10:00
+
|05:10  
 
|So, we have seen how to redirect standard input.  
 
|So, we have seen how to redirect standard input.  
 
|-
 
|-
|05:12:00
+
|05:12  
 
|Now let us see how to redirect standard output and standard error.  
 
|Now let us see how to redirect standard output and standard error.  
 
|-
 
|-
|05:17:00
+
|05:17  
 
|There are two ways to redirect output or error to a file:
 
|There are two ways to redirect output or error to a file:
 
|-
 
|-
|05:20:00
+
|05:20  
 
|Suppose, n refers to the file descriptor.  
 
|Suppose, n refers to the file descriptor.  
  
Line 228: Line 228:
 
redirects output from file descriptor ''n'' to a file.  
 
redirects output from file descriptor ''n'' to a file.  
 
|-
 
|-
|05:29:00
+
|05:29  
 
|You must have write authority to the file.  
 
|You must have write authority to the file.  
 
|-
 
|-
|05:32:00
+
|05:32  
 
|If the file does not exist, it is created.  
 
|If the file does not exist, it is created.  
 
|-
 
|-
|05:35:00
+
|05:35  
 
|If it does exist, the existing contents are usually lost without any warning
 
|If it does exist, the existing contents are usually lost without any warning
 
|-
 
|-
|05:40:00
+
|05:40  
 
|' n 'double right-angled bracket' also redirects output from file descriptor ''n'' to a file.  
 
|' n 'double right-angled bracket' also redirects output from file descriptor ''n'' to a file.  
 
|-
 
|-
|05:47:00
+
|05:47  
 
|Again, you must have write authority to the file.  
 
|Again, you must have write authority to the file.  
 
|-
 
|-
|05:50:00
+
|05:50  
 
|If the file does not exist, it is created.  
 
|If the file does not exist, it is created.  
 
|-
 
|-
|05:52:00
+
|05:52  
 
|If it does exist, the output is appended to the existing file.   
 
|If it does exist, the output is appended to the existing file.   
 
|-
 
|-
|05:59:00
+
|05:59  
 
|The ''n'' in n single right angle bracket or n double right angle bracket refers to the ''file descriptor''.  
 
|The ''n'' in n single right angle bracket or n double right angle bracket refers to the ''file descriptor''.  
 
|-
 
|-
|06:05:00
+
|06:05  
 
|If it is omitted, then standard output i.e. file descriptor 1 is assumed.
 
|If it is omitted, then standard output i.e. file descriptor 1 is assumed.
 
|-
 
|-
|06:10:00
+
|06:10  
 
|So, just a right angle bracket is same as 1 right angle bracket.  
 
|So, just a right angle bracket is same as 1 right angle bracket.  
 
|-
 
|-
|06:15:00
+
|06:15  
 
|But, to redirect error stream, you have to use 2 right angle bracket  or 2 double right angle bracket.
 
|But, to redirect error stream, you have to use 2 right angle bracket  or 2 double right angle bracket.
 
|-
 
|-
|06:22:00
+
|06:22  
 
|Let us see this practically.  
 
|Let us see this practically.  
 
|-
 
|-
|06:24:00
+
|06:24  
 
|From the last example we have seen that the result of the wc command on a file or standardin is displayed at the terminal window.
 
|From the last example we have seen that the result of the wc command on a file or standardin is displayed at the terminal window.
 
|-
 
|-
|06:31:00
+
|06:31  
 
|What if we do not want to display this on the terminal?  
 
|What if we do not want to display this on the terminal?  
 
|-
 
|-
|06:34:00
+
|06:34  
 
|We want to store it in a file , so that the information can be later used.
 
|We want to store it in a file , so that the information can be later used.
 
|-
 
|-
|06:38:00
+
|06:38  
 
|By default wc writes its output to the standardout .  
 
|By default wc writes its output to the standardout .  
 
|-
 
|-
|06:42:00
+
|06:42  
 
|The standardout is by default connected to the terminal window.  
 
|The standardout is by default connected to the terminal window.  
 
|-
 
|-
|06:45:00
+
|06:45  
 
|Hence we see the output in the terminal window.  
 
|Hence we see the output in the terminal window.  
 
|-
 
|-
|06:48:00
+
|06:48  
 
|But if we can redirect the standardout to a file, then the output from the wc command will be written to that file.
 
|But if we can redirect the standardout to a file, then the output from the wc command will be written to that file.
 
|-
 
|-
|06:57:00
+
|06:57  
 
|Say we write  
 
|Say we write  
  
 
"wc space test1 dot txt 'right-angled bracket' wc_results dot txt" .  
 
"wc space test1 dot txt 'right-angled bracket' wc_results dot txt" .  
 
|-
 
|-
|07:09:00
+
|07:09  
 
|Press enter.
 
|Press enter.
 
|-
 
|-
|07:11:00
+
|07:11  
 
|Now to see whether this has actually happened we can display the contents of wc_results dot txt by the c-a-t command.  
 
|Now to see whether this has actually happened we can display the contents of wc_results dot txt by the c-a-t command.  
 
|-
 
|-
|07:23:00
+
|07:23  
 
|Yes it has.
 
|Yes it has.
 
|-
 
|-
|07:24:00
+
|07:24  
 
|Suppose,we have another file test2 in the same directory.
 
|Suppose,we have another file test2 in the same directory.
 
|-
 
|-
|07:30:00
+
|07:30  
 
|Now we again execute the command with test2 file. We type  
 
|Now we again execute the command with test2 file. We type  
  
 
"wc space test2 dot txt 'right-angled bracket' wc_results dot txt"  
 
"wc space test2 dot txt 'right-angled bracket' wc_results dot txt"  
 
|-
 
|-
|07:44:00
+
|07:44  
 
|So,the contents of the file wc_results would be overwritten.
 
|So,the contents of the file wc_results would be overwritten.
 
|-
 
|-
|07:48:00
+
|07:48  
 
|Let's see this .
 
|Let's see this .
 
|-
 
|-
|07:56:00
+
|07:56  
 
|Instead if we write "wc space test1 dot txt 'right-angled bracket' twice wc underscore results dot txt"  
 
|Instead if we write "wc space test1 dot txt 'right-angled bracket' twice wc underscore results dot txt"  
 
|-
 
|-
|08:07:00
+
|08:07
 
|the new contents will not overwrite the already present contents of the file wc underscore results dot txt, it would be appended.
 
|the new contents will not overwrite the already present contents of the file wc underscore results dot txt, it would be appended.
 
|-
 
|-
|08:15:00
+
|08:15  
 
|Let's see this too.
 
|Let's see this too.
 
|-
 
|-
|08:26:00
+
|08:26  
 
|Redirecting the standard error is done similarly.  
 
|Redirecting the standard error is done similarly.  
 
|-
 
|-
|08:29:00
+
|08:29  
 
|Only difference is that in this case we need to mention the file descriptor number of standard error before the right angle bracket  or double right angle bracket sign.
 
|Only difference is that in this case we need to mention the file descriptor number of standard error before the right angle bracket  or double right angle bracket sign.
 
|-
 
|-
|08:38:00
+
|08:38  
 
|Like we know that their exists no file by the name aaa write the following
 
|Like we know that their exists no file by the name aaa write the following
  
 
"wc space aaa"
 
"wc space aaa"
 
|-
 
|-
|08:46:00
+
|08:46  
 
|The shell will give the error “No such file or directory”.
 
|The shell will give the error “No such file or directory”.
 
|-
 
|-
|08:50:00
+
|08:50  
 
|Now say we dont want error messages on screen. They can be redirected to some other file .
 
|Now say we dont want error messages on screen. They can be redirected to some other file .
 
|-
 
|-
|08:55:00
+
|08:55  
 
|For this we may give the command
 
|For this we may give the command
  
 
"wc space aaa space 2 'right-anged bracket' errorlog dot txt"
 
"wc space aaa space 2 'right-anged bracket' errorlog dot txt"
 
|-
 
|-
|09:06:00
+
|09:06  
 
|Now the error will not show on the terminal, rather it will be written in the file errorlog dot txt
 
|Now the error will not show on the terminal, rather it will be written in the file errorlog dot txt
 
|-
 
|-
|09:12:00
+
|09:12  
 
|we can see this by the command
 
|we can see this by the command
  
 
"cat space errorlog dot txt"
 
"cat space errorlog dot txt"
 
|-
 
|-
|09:22:00
+
|09:22  
 
|Now suppose that I make some other error by running the command
 
|Now suppose that I make some other error by running the command
  
 
"cat space bbb space 2 'right-angled bracket' errorlog dot txt".
 
"cat space bbb space 2 'right-angled bracket' errorlog dot txt".
 
|-
 
|-
|09:34:00
+
|09:34  
 
|the previous error would be overwritten and the new error will show.
 
|the previous error would be overwritten and the new error will show.
 
|-
 
|-
|09:39:00
+
|09:39  
 
|See "cat space errorlog dot txt"
 
|See "cat space errorlog dot txt"
 
|-
 
|-
|09:46:00
+
|09:46  
 
|But what if we want to list all errors??
 
|But what if we want to list all errors??
  
Line 370: Line 370:
 
"wc space aaa space 2 'right-angled bracket' twice errorlog dot txt"
 
"wc space aaa space 2 'right-angled bracket' twice errorlog dot txt"
 
|-
 
|-
|09:58:00
+
|09:58  
 
|We check this using the cat command.
 
|We check this using the cat command.
 
|-
 
|-
|10:06:00
+
|10:06  
 
|We have seen how the three streams standard out,standard in,standard error are redirected and manipulated separately. But the real power of this concept can be gauged when we can manipulate the streams together, ie connect the different streams.
 
|We have seen how the three streams standard out,standard in,standard error are redirected and manipulated separately. But the real power of this concept can be gauged when we can manipulate the streams together, ie connect the different streams.
 
|-
 
|-
|10:20:00
+
|10:20  
 
|This process is called pipelining.  
 
|This process is called pipelining.  
 
|-
 
|-
|10:22:00
+
|10:22  
 
|Pipes are used to create chains of command.  
 
|Pipes are used to create chains of command.  
 
|-
 
|-
|10:25:00
+
|10:25  
 
|A Pipe connect the output of one command to the input of the next command in the chain.
 
|A Pipe connect the output of one command to the input of the next command in the chain.
 
|-
 
|-
|10:30:00
+
|10:30  
 
|It looks like  
 
|It looks like  
  
 
  command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4
 
  command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4
 
|-
 
|-
|10:46:00
+
|10:46  
 
|Say we want to know the total number of files and directories present in the current directory
 
|Say we want to know the total number of files and directories present in the current directory
 
|-
 
|-
|10:51:00
+
|10:51  
 
|What we can do? We know
 
|What we can do? We know
  
 
"ls space minus l" will list all files and directories of the present directory.
 
"ls space minus l" will list all files and directories of the present directory.
 
|-
 
|-
|10:58:00
+
|10:58  
 
|We can redirect the output to a file
 
|We can redirect the output to a file
  
 
"ls space minus l 'right-angled bracket' files dot txt"
 
"ls space minus l 'right-angled bracket' files dot txt"
 
|-
 
|-
|11:08:00
+
|11:08  
 
|Run "cat space files dot txt"
 
|Run "cat space files dot txt"
 
|-
 
|-
|11:14:00
+
|11:14  
 
|Now each line is a name of a file or directory .  
 
|Now each line is a name of a file or directory .  
 
|-
 
|-
|11:17:00
+
|11:17  
 
|So if we measure the total lines in this file, we can use files dot txt to serve our purpose.
 
|So if we measure the total lines in this file, we can use files dot txt to serve our purpose.
 
|-
 
|-
|11:24:00
+
|11:24  
 
|This we can do using the command "wc space minus  l files dot txt"
 
|This we can do using the command "wc space minus  l files dot txt"
 
|-
 
|-
|11:32:00
+
|11:32  
 
|Though this serves our purpose there are a few problems.
 
|Though this serves our purpose there are a few problems.
 
|-
 
|-
|11:35:00
+
|11:35  
 
|First we need an intermediate file , here files dot txt .  
 
|First we need an intermediate file , here files dot txt .  
 
|-
 
|-
|11:40:00
+
|11:40  
 
|If the first command produces a lot of data,it may unneccesarily eat away the disk memory.  
 
|If the first command produces a lot of data,it may unneccesarily eat away the disk memory.  
 
|-
 
|-
|11:46:00
+
|11:46  
 
|Also if we want to chain several commands, this method is slow.
 
|Also if we want to chain several commands, this method is slow.
 
|-
 
|-
|11:50:00
+
|11:50  
 
|We can do it much easily using pipes like this . We write
 
|We can do it much easily using pipes like this . We write
  
 
"ls space minus l 'vertical bar' wc space minus l"  
 
"ls space minus l 'vertical bar' wc space minus l"  
 
|-
 
|-
|12:01:00
+
|12:01  
 
|and we can get the same result with much more ease.
 
|and we can get the same result with much more ease.
 
|-
 
|-
|12:06:00
+
|12:06  
 
|The output from the ls command goes as input for the wc command.
 
|The output from the ls command goes as input for the wc command.
 
|-
 
|-
|12:10:00
+
|12:10  
 
|We can add even longer chains of command using pipes.
 
|We can add even longer chains of command using pipes.
 
|-
 
|-
|12:15:00
+
|12:15  
 
|One common use of pipes is for reading multipage displays.  
 
|One common use of pipes is for reading multipage displays.  
 
|-
 
|-
|12:19:00
+
|12:19  
 
|Type "cd space slash user slash bin".  
 
|Type "cd space slash user slash bin".  
 
|-
 
|-
|12:24:00
+
|12:24  
 
|So, we are now in bin directory.  
 
|So, we are now in bin directory.  
 
|-
 
|-
|12:28:00
+
|12:28  
 
|now run "ls minus l"  
 
|now run "ls minus l"  
 
|-
 
|-
|12:31:00
+
|12:31  
 
|we cannot see the output properly. But if we use it joined with a pipe to more, we can.
 
|we cannot see the output properly. But if we use it joined with a pipe to more, we can.
 
|-
 
|-
|12:37:00
+
|12:37  
 
|Press enter to scroll through the list.
 
|Press enter to scroll through the list.
 
|-
 
|-
|12:41:00
+
|12:41  
 
|Press "q" to come out of it.
 
|Press "q" to come out of it.
 
|-
 
|-
|12:45:00
+
|12:45  
 
|These were some of the commands that help us to work with files.  
 
|These were some of the commands that help us to work with files.  
 
|-
 
|-
|12:48:00
+
|12:48  
 
|There are many more commands.  
 
|There are many more commands.  
 
|-
 
|-
|12:50:00
+
|12:50  
 
|Moreover each of the commands that we saw has many other options.  
 
|Moreover each of the commands that we saw has many other options.  
 
|-
 
|-
|12:54:00
+
|12:54  
 
|I encourage you to see more about them using the 'man' command.
 
|I encourage you to see more about them using the 'man' command.
 
|-
 
|-
|12:58:00
+
|12:58  
 
|The best way of learning commands is to use them again and again.
 
|The best way of learning commands is to use them again and again.
 
|-
 
|-
|13:04:00
+
|13:04
 
|This brings me to the end of this tutorial.  
 
|This brings me to the end of this tutorial.  
 
|-
 
|-
|13:07:00
+
|13:07  
 
|Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
 
|Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
 
|-
 
|-
|13:15:00
+
|13:15  
 
|More information on the same is available at the following link .
 
|More information on the same is available at the following link .
 
|-
 
|-
|13:19:00
+
|13:19  
 
|This script has been contributed by ----------------------(name of the translator) and this is -----------------------(name of the recorder) from --------------------------(name of the place)signing off . Thank you for joining.
 
|This script has been contributed by ----------------------(name of the translator) and this is -----------------------(name of the recorder) from --------------------------(name of the place)signing off . Thank you for joining.
  
 
|}
 
|}

Revision as of 11:11, 22 April 2013

Time Narration
00:00 Welcome to this spoken tutorial on Redirection and Pipes.
00:07 I am using Ubuntu 10.04.
00:09 We assume that you already have an idea about the Linux operating system and have some basic idea about commands.
00:16 If you are interested, it is available through another spoken tutorial, on the following website
00:22 Also note that linux is case sensitive .
00:25 All the commands used in this tutorial are in lower case unless otherwise mentioned.
00:32 Most of the work that we do in Linux is through a terminal.
00:35 When we have to execute a command, we generally type through the keyboard.
00:39 Say we have to find the date and time.
00:41 We simply type through the keyboard "date" and press enter.
00:46 So we normally give input through the keyboard.
00:48 Similarly we see that the output of our command is also displayed at the terminal window.
00:56 Also say some error occurs while we are executing some command.
00:59 For example we type "cat space aaa". And press enter.
01:05 Now no such file by the name aaa exists.
01:08 So there is an error displayed, saying so.
01:10 Now this error also comes on the terminal window. Hence we see error reporting is also at the terminal
01:20 Now inputting, outputting and error reporting are the three special actions related to commands.
01:24 Before learning about redirection we should know about two important concepts. That of stream and file descriptor.
01:31 A Linux shell like Bash, receives input and sends output as sequences or streams of characters.
01:37 Each character is independent of the one before it and the one after it.
01:41 Streams are accessed using file IO techniques.
01:44 It does not matter whether or not the actual stream of characters comes from or goes to a file, a keyboard, a window etc.
01:51 In Linux, every open file of a process is associated with an integer number.
01:57 These numeric values are known as file descriptors.
02:05 Linux shells use three standard I/O streams.
02:08 Each of them is associated with a well-known file descriptor.
02:12 stdin is the standard input stream.
02:15 It provides input to commands.
02:17 It has file descriptor 0.
02:19 stdout is the standard output stream.
02:22 It displays output from commands. It has file descriptor 1.
02:26 stderr is the standard error stream.It displays error output from commands. It has file descriptor 2.
02:36 Input streams provide input to programs.
02:40 By default it takes from terminal keystrokes.
02:44 Output streams print text characters, by default to the terminal.
02:47 The terminal was originally an ASCII typewriter or display terminal.
02:52 But is now more often a text window on a graphical desktop.
02:56 We have seen that the 3 streams are connected to some files by default.
03:01 But in linux, we can change this default behaviour.
03:04 We can connect these 3 streams to other files.
03:07 This process is called redirection.
03:09 Now let us see how redirection is done in the 3 streams.
03:14 First let us see how the standard input is redirected.
03:17 we redirect standardin from a file, using the < (left angled bracket) operator.

Let us see how.

03:22 We know that the use of wc command is to find the number of lines,words and charecters in a file.
03:28 Type wc on the terminal window.
03:31 Now press enter.
03:32 What happens?? we have a blinking cursor. It means that we need to enter through the keyboard.
03:37 Enter some text say "This tutorial is very important".
03:46 Now press enter.
03:48 Now press Ctrl and d keys together.
03:52 Now the command will work on the lines that we entered.
03:55 The command will give an output on the terminal.
03:57 Now here no file is given after wc command .
04:01 So it takes input from standard input stream.
04:04 Now the standard input stream is by default connected to the keyboard. Hence wc will take input from keyboard.
04:12 Now if we write

"wc space 'left-angled bracket" space test1 dot txt"

04:19 what happens is that wc will tell us the no. of lines,words and characters in the file test1 dot txt
04:27 Now type

"wc space test1 dot txt"

04:34 we see the same result.
04:37 So what is the difference?
04:39 When we write "wc space test1dot txt" , the command opens the file test1dot txt and reads from it.
04:46 But when we write "wc space 'left-angled bracket' test1 dot txt", wc still does not get any file to open.
04:53 Instead ,it looks to pick up the input from standardin.
04:57 Now we have directed the standardin to the file test1dot txt.
05:01 Hence the command reads from test1.
05:04 But actually it is unaware as to from where the data is coming to standardin.
05:10 So, we have seen how to redirect standard input.
05:12 Now let us see how to redirect standard output and standard error.
05:17 There are two ways to redirect output or error to a file:
05:20 Suppose, n refers to the file descriptor.

nsingle right-angled bracket

redirects output from file descriptor n to a file.

05:29 You must have write authority to the file.
05:32 If the file does not exist, it is created.
05:35 If it does exist, the existing contents are usually lost without any warning
05:40 ' n 'double right-angled bracket' also redirects output from file descriptor n to a file.
05:47 Again, you must have write authority to the file.
05:50 If the file does not exist, it is created.
05:52 If it does exist, the output is appended to the existing file.
05:59 The n in n single right angle bracket or n double right angle bracket refers to the file descriptor.
06:05 If it is omitted, then standard output i.e. file descriptor 1 is assumed.
06:10 So, just a right angle bracket is same as 1 right angle bracket.
06:15 But, to redirect error stream, you have to use 2 right angle bracket or 2 double right angle bracket.
06:22 Let us see this practically.
06:24 From the last example we have seen that the result of the wc command on a file or standardin is displayed at the terminal window.
06:31 What if we do not want to display this on the terminal?
06:34 We want to store it in a file , so that the information can be later used.
06:38 By default wc writes its output to the standardout .
06:42 The standardout is by default connected to the terminal window.
06:45 Hence we see the output in the terminal window.
06:48 But if we can redirect the standardout to a file, then the output from the wc command will be written to that file.
06:57 Say we write

"wc space test1 dot txt 'right-angled bracket' wc_results dot txt" .

07:09 Press enter.
07:11 Now to see whether this has actually happened we can display the contents of wc_results dot txt by the c-a-t command.
07:23 Yes it has.
07:24 Suppose,we have another file test2 in the same directory.
07:30 Now we again execute the command with test2 file. We type

"wc space test2 dot txt 'right-angled bracket' wc_results dot txt"

07:44 So,the contents of the file wc_results would be overwritten.
07:48 Let's see this .
07:56 Instead if we write "wc space test1 dot txt 'right-angled bracket' twice wc underscore results dot txt"
08:07 the new contents will not overwrite the already present contents of the file wc underscore results dot txt, it would be appended.
08:15 Let's see this too.
08:26 Redirecting the standard error is done similarly.
08:29 Only difference is that in this case we need to mention the file descriptor number of standard error before the right angle bracket or double right angle bracket sign.
08:38 Like we know that their exists no file by the name aaa write the following

"wc space aaa"

08:46 The shell will give the error “No such file or directory”.
08:50 Now say we dont want error messages on screen. They can be redirected to some other file .
08:55 For this we may give the command

"wc space aaa space 2 'right-anged bracket' errorlog dot txt"

09:06 Now the error will not show on the terminal, rather it will be written in the file errorlog dot txt
09:12 we can see this by the command

"cat space errorlog dot txt"

09:22 Now suppose that I make some other error by running the command

"cat space bbb space 2 'right-angled bracket' errorlog dot txt".

09:34 the previous error would be overwritten and the new error will show.
09:39 See "cat space errorlog dot txt"
09:46 But what if we want to list all errors??

simple we would run the command

"wc space aaa space 2 'right-angled bracket' twice errorlog dot txt"

09:58 We check this using the cat command.
10:06 We have seen how the three streams standard out,standard in,standard error are redirected and manipulated separately. But the real power of this concept can be gauged when we can manipulate the streams together, ie connect the different streams.
10:20 This process is called pipelining.
10:22 Pipes are used to create chains of command.
10:25 A Pipe connect the output of one command to the input of the next command in the chain.
10:30 It looks like
command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4
10:46 Say we want to know the total number of files and directories present in the current directory
10:51 What we can do? We know

"ls space minus l" will list all files and directories of the present directory.

10:58 We can redirect the output to a file

"ls space minus l 'right-angled bracket' files dot txt"

11:08 Run "cat space files dot txt"
11:14 Now each line is a name of a file or directory .
11:17 So if we measure the total lines in this file, we can use files dot txt to serve our purpose.
11:24 This we can do using the command "wc space minus l files dot txt"
11:32 Though this serves our purpose there are a few problems.
11:35 First we need an intermediate file , here files dot txt .
11:40 If the first command produces a lot of data,it may unneccesarily eat away the disk memory.
11:46 Also if we want to chain several commands, this method is slow.
11:50 We can do it much easily using pipes like this . We write

"ls space minus l 'vertical bar' wc space minus l"

12:01 and we can get the same result with much more ease.
12:06 The output from the ls command goes as input for the wc command.
12:10 We can add even longer chains of command using pipes.
12:15 One common use of pipes is for reading multipage displays.
12:19 Type "cd space slash user slash bin".
12:24 So, we are now in bin directory.
12:28 now run "ls minus l"
12:31 we cannot see the output properly. But if we use it joined with a pipe to more, we can.
12:37 Press enter to scroll through the list.
12:41 Press "q" to come out of it.
12:45 These were some of the commands that help us to work with files.
12:48 There are many more commands.
12:50 Moreover each of the commands that we saw has many other options.
12:54 I encourage you to see more about them using the 'man' command.
12:58 The best way of learning commands is to use them again and again.
13:04 This brings me to the end of this tutorial.
13:07 Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
13:15 More information on the same is available at the following link .
13:19 This script has been contributed by ----------------------(name of the translator) and this is -----------------------(name of the recorder) from --------------------------(name of the place)signing off . Thank you for joining.

Contributors and Content Editors

Minal, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha