Difference between revisions of "BOSS-Linux/C2/Redirection-Pipes/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
|- | |- | ||
|00:00 | |00:00 | ||
− | |Welcome to this spoken tutorial on Redirection | + | |Welcome to this spoken tutorial on '''Redirection Pipes'''. |
|- | |- | ||
|00:07 | |00:07 | ||
− | |I am using Linux Operating System. | + | |I am using '''Linux Operating System'''. |
|- | |- | ||
|00:09 | |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:16 | ||
− | |If you are interested, it is available through another spoken tutorial | + | |If you are interested, it is available through another spoken tutorial on the following website. |
|- | |- | ||
|00:22 | |00:22 | ||
− | |Also note that | + | |Also note that Linux is '''case sensitive''' . |
|- | |- | ||
|00:25 | |00:25 | ||
Line 22: | Line 22: | ||
|- | |- | ||
|00:32 | |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:35 | ||
Line 31: | Line 31: | ||
|- | |- | ||
|00:41 | |00:41 | ||
− | |We simply type through the keyboard '''date''' and press ''' | + | |We simply type through the keyboard '''date''' and press '''Enter.''' |
|- | |- | ||
|00:46 | |00:46 | ||
− | |So we normally give input through the keyboard. | + | |So, we normally give input through the keyboard. |
|- | |- | ||
|00:48 | |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: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:59 | ||
Line 46: | Line 46: | ||
|- | |- | ||
|01:05 | |01:05 | ||
− | |Now no such file by the name aaa exists. | + | |Now, no such file by the name 'aaa' exists. |
|- | |- | ||
|01:08 | |01:08 | ||
− | |So there is an error displayed | + | |So, there is an '''error''' displayed saying so. |
|- | |- | ||
|01:10 | |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 | |01:20 | ||
Line 58: | Line 58: | ||
|- | |- | ||
|01:24 | |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 | |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 | |01:37 | ||
Line 67: | Line 67: | ||
|- | |- | ||
|01:41 | |01:41 | ||
− | |'''Streams''' are accessed using file IO techniques. | + | |'''Streams''' are accessed using file '''IO techniques'''. |
|- | |- | ||
|01:44 | |01:44 | ||
Line 76: | Line 76: | ||
|- | |- | ||
|01:57 | |01:57 | ||
− | |These numeric values are known as file descriptors. | + | |These numeric values are known as '''file descriptors'''. |
|- | |- | ||
|02:05 | |02:05 | ||
− | |Linux shells use three standard I/O streams. | + | |Linux '''shells''' use three standard '''I/O streams'''. |
|- | |- | ||
|02:08 | |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 | |02:12 | ||
− | |'''stdin''' is the standard input stream. | + | |'''stdin''' is the '''standard input stream'''. |
|- | |- | ||
|02:15 | |02:15 | ||
Line 97: | Line 97: | ||
|- | |- | ||
|02:22 | |02:22 | ||
− | |It displays output from commands. It has file descriptor 1. | + | |It displays output from commands. It has '''file descriptor 1'''. |
|- | |- | ||
|02:26 | |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 | |02:36 | ||
Line 106: | Line 106: | ||
|- | |- | ||
|02:40 | |02:40 | ||
− | |By default it takes from terminal keystrokes. | + | |By default, it takes from terminal keystrokes. |
|- | |- | ||
|02:44 | |02:44 | ||
Line 121: | Line 121: | ||
|- | |- | ||
|03:01 | |03:01 | ||
− | |But in linux, we can change this default | + | |But in linux, we can change this default behavior. |
|- | |- | ||
|03:04 | |03:04 | ||
Line 130: | Line 130: | ||
|- | |- | ||
|03:09 | |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 | |03:14 | ||
− | |First let us see how the standard input is redirected. | + | |First, let us see how the standard input is redirected. |
|- | |- | ||
|03:17 | |03:17 | ||
− | | | + | |We redirect '''standardin''' from a file, using the < (left angled bracket) operator. Let us see how. |
− | Let us see how. | + | |
|- | |- | ||
|03:22 | |03:22 | ||
− | |We know that the use of '''wc''' command is to find the number of lines,words and | + | |We know that the use of '''wc''' command is to find the number of lines, words and characters in a file. |
|- | |- | ||
|03:28 | |03:28 | ||
Line 146: | Line 145: | ||
|- | |- | ||
|03:31 | |03:31 | ||
− | |Now press ''' | + | |Now press '''Enter.''' What happens?? We have a blinking cursor. It means that we need to enter through the keyboard. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|03:37 | |03:37 | ||
− | |Enter some text say | + | |Enter some text say "This tutorial is very important". |
|- | |- | ||
|03:46 | |03:46 | ||
Line 167: | Line 163: | ||
|- | |- | ||
|03:57 | |03:57 | ||
− | |Now here no file is given after '''wc command ''' | + | |Now here no file is given after '''wc command. ''' |
|- | |- | ||
|04:01 | |04:01 | ||
− | |So it takes input from standard input stream. | + | |So, it takes input from standard input stream. |
|- | |- | ||
|04:04 | |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 the keyboard. |
|- | |- | ||
|04:12 | |04:12 | ||
− | |Now if we write '''wc space | + | |Now, if we write '''wc space left-angled bracket space test1 dot txt'''. |
|- | |- | ||
|04:19 | |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 | |04:27 | ||
− | |Now type '''wc space test1 dot txt''' | + | |Now type: '''wc space test1 dot txt'''. |
|- | |- | ||
|04:34 | |04:34 | ||
− | | | + | |We see the same result. |
|- | |- | ||
|04:37 | |04:37 | ||
− | |So what is the difference? | + | |So, what is the difference? |
|- | |- | ||
|04:39 | |04:39 | ||
Line 194: | Line 190: | ||
|- | |- | ||
|04:46 | |04:46 | ||
− | |But when we write '''wc space | + | |But when we write '''wc space left-angled bracket test1 dot txt''', '''wc''' still does not get any file to open. |
|- | |- | ||
|04:53 | |04:53 | ||
− | |Instead ,it looks to pick up the input from '''standardin.''' | + | |Instead, it looks to pick up the input from '''standardin.''' |
|- | |- | ||
|04:57 | |04:57 | ||
− | |Now we have directed | + | |Now we have directed '''standardin''' to the file '''test1dot txt.''' |
|- | |- | ||
|05:01 | |05:01 | ||
− | |Hence the command reads from '' | + | |Hence the command reads from 'test1'. |
|- | |- | ||
|05:04 | |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 | |05:10 | ||
− | |So, we have seen how to redirect standard input. | + | |So, we have seen how to '''redirect''' '''standard input'''. |
|- | |- | ||
|05:12 | |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 | |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 | |05:20 | ||
− | |Suppose, n refers to the file descriptor | + | |Suppose, 'n' refers to the file descriptor, 'n' single right-angled bracket redirects output from file descriptor 'n' to a file. |
|- | |- | ||
|05:29 | |05:29 | ||
− | |You must have write authority to the file. | + | |You must have '''write''' authority to the file. |
|- | |- | ||
|05:32 | |05:32 | ||
Line 227: | Line 223: | ||
|- | |- | ||
|05:35 | |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 | |05:40 | ||
− | |' n 'double right-angled bracket' also redirects output from file descriptor | + | |' 'n' double right-angled bracket' also redirects output from file descriptor 'n' to a file. |
|- | |- | ||
|05:47 | |05:47 | ||
− | |Again, you must have write authority to the file. | + | |Again, you must have '''write''' authority to the file. |
|- | |- | ||
|05:50 | |05:50 | ||
Line 242: | Line 238: | ||
|- | |- | ||
|05:59 | |05:59 | ||
− | |The | + | |The 'n' in 'n single right angle bracket' or 'n double right angle bracket' refers to the ''file descriptor''. |
|- | |- | ||
|06:05 | |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 | |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 | |06:15 | ||
− | |But, to redirect error stream, you have to use 2 right angle bracket | + | |But, to redirect error stream, you have to use 2 right angle-bracket or 2 double right angle-bracket. |
|- | |- | ||
|06:22 | |06:22 | ||
Line 257: | Line 253: | ||
|- | |- | ||
|06:24 | |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 | |06:31 | ||
Line 266: | Line 262: | ||
|- | |- | ||
|06:38 | |06:38 | ||
− | |By default wc writes its output to the '''standardout''' | + | |By default, '''wc''' writes its output to the '''standardout''' |
|- | |- | ||
|06:42 | |06:42 | ||
− | |The '''standardout''' is by default connected to the terminal window. | + | |The '''standardout''' is, by default, connected to the terminal window. |
|- | |- | ||
|06:45 | |06:45 | ||
Line 275: | Line 271: | ||
|- | |- | ||
|06:48 | |06:48 | ||
− | |But if we can redirect the standardout to a 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 | |06:57 | ||
− | |Say we write ''' wc space test1 dot txt 'right-angled bracket' wc_results dot txt''' . | + | |Say, we write: ''' wc space test1 dot txt 'right-angled bracket' wc_results dot txt''' . |
|- | |- | ||
|07:09 | |07:09 | ||
− | |Press ''' | + | |Press '''Enter.''' |
|- | |- | ||
|07:11 | |07:11 | ||
− | |Now to see whether this is actually happened we can display the contents of '''wc_results dot txt''' by the '''c-a-t''' command. | + | |Now, to see whether this is actually happened, we can display the contents of '''wc_results dot txt''' by the '''c-a-t''' command. |
|- | |- | ||
|07:23 | |07:23 | ||
− | |Yes it has. | + | |Yes! it has.Suppose, we have another file '''test2''' in the same directory. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
|07:30 | |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 | |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 | |07:48 | ||
− | |Let's see this . | + | |Let's see this. |
|- | |- | ||
|07:56 | |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 | |08:07 | ||
− | |the new contents will not overwrite the already present contents of the file '''wc underscore results dot txt,''' it | + | |the new contents will not overwrite the already present contents of the file '''wc underscore results dot txt,''' it will be appended. |
|- | |- | ||
|08:15 | |08:15 | ||
Line 313: | Line 307: | ||
|- | |- | ||
|08:26 | |08:26 | ||
− | |Redirecting the standard error is done similarly. | + | |Redirecting the '''standard error''' is done similarly. |
|- | |- | ||
|08:29 | |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 | |08:38 | ||
− | |Like we know that their exists no file by the name aaa write the following '''wc space aaa''' | + | |Like we know that their exists no file by the name 'aaa', write the following '''wc space aaa'''. |
|- | |- | ||
|08:46 | |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 | |08:50 | ||
− | |Now say we | + | |Now say, we don't want error messages on screen. They can be redirected to some other file . |
|- | |- | ||
|08:55 | |08:55 | ||
− | |For this we may give the command ''' wc space aaa space 2 'right- | + | |For this, we may give the command ''' wc space aaa space 2 'right angled- bracket' errorlog dot txt'''. |
|- | |- | ||
|09:06 | |09:06 | ||
− | |Now the error will not show on the terminal, rather it will be written in the file | + | |Now the error will not show on the terminal, rather it will be written in the file 'errorlog dot txt'. |
|- | |- | ||
|09:12 | |09:12 | ||
− | | | + | |We can see this by the command '''cat space errorlog dot txt'''. |
|- | |- | ||
|09:22 | |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 | + | '''cat space bbb space 2 'right angled- bracket' errorlog dot txt.''' |
|- | |- | ||
|09:34 | |09:34 | ||
− | | | + | |The previous error would be overwritten and the new error will show. |
|- | |- | ||
|09:39 | |09:39 | ||
− | |See '''cat space errorlog dot txt''' | + | |See '''cat space errorlog dot txt'''. |
|- | |- | ||
|09:46 | |09:46 | ||
− | |But what if we want to list all errors?? | + | |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'''. |
− | + | ||
− | + | ||
− | + | ||
− | '''wc space aaa space 2 'right-angled bracket' twice errorlog dot txt''' | + | |
|- | |- | ||
|09:58 | |09:58 | ||
Line 358: | Line 348: | ||
|- | |- | ||
|10:06 | |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, | + | |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, i.e connect the different streams. |
|- | |- | ||
|10:20 | |10:20 | ||
Line 364: | Line 354: | ||
|- | |- | ||
|10:22 | |10:22 | ||
− | |Pipes are used to create chains of command. | + | |'''Pipes''' are used to create chains of command. |
|- | |- | ||
|10:25 | |10:25 | ||
− | |A Pipe | + | |A '''Pipe''' connects the output of one command to the input of the next command in the chain. |
|- | |- | ||
|10:30 | |10:30 | ||
− | |It looks like ''' command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4''' | + | |It looks like: ''' command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4''' |
|- | |- | ||
|10:46 | |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 | |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 | |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 | |11:08 | ||
− | |Run '''cat space files dot txt''' | + | |Run '''cat space files dot txt'''. |
|- | |- | ||
|11:14 | |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 | |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 | |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 | |11:32 | ||
− | |Though this serves our purpose there are a few problems. | + | |Though this serves our purpose, there are a few problems. |
|- | |- | ||
|11:35 | |11:35 | ||
− | |First we need an intermediate file , here '''files dot txt''' | + | |First, we need an intermediate file, here '''files dot txt'''. |
|- | |- | ||
|11:40 | |11:40 | ||
− | |If the first command produces a lot of data,it may | + | |If the first command produces a lot of data, it may unnecessarily eat away the disk memory. |
|- | |- | ||
|11:46 | |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 | |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''' | ||
|- | |- | ||
Line 421: | Line 407: | ||
|- | |- | ||
|12:10 | |12:10 | ||
− | |We can add even longer chains of command using pipes. | + | |We can add even longer chains of command using '''pipes'''. |
|- | |- | ||
|12:15 | |12:15 | ||
− | |One common use of pipes is for reading | + | |One common use of pipes is for reading multi-page displays. |
|- | |- | ||
|12:19 | |12:19 | ||
− | |'''cd space slash user slash bin'''. | + | |Type: '''cd space slash user slash bin'''. |
|- | |- | ||
− | |12:24 | + | |12:24 |
− | |So, we are now in bin directory. | + | |So, we are now in '''bin''' directory. |
|- | |- | ||
|12:28 | |12:28 | ||
− | | | + | |Now, run '''ls minus l'''. |
|- | |- | ||
|12:31 | |12:31 | ||
− | | | + | |We cannot see the output properly. But if we use it joined with a pipe to more, we can. |
|- | |- | ||
|12:37 | |12:37 | ||
− | |Press | + | |Press '''Enter''' to scroll through the list. |
|- | |- | ||
|12:41 | |12:41 | ||
Line 451: | Line 437: | ||
|- | |- | ||
|12:50 | |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 | |12:54 | ||
Line 463: | Line 449: | ||
|- | |- | ||
|13:07 | |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 | |13:15 |
Latest revision as of 12:13, 24 March 2017
Time | Narration |
00:00 | Welcome to this spoken tutorial on Redirection Pipes. |
00:07 | I am using Linux Operating System. |
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 behavior. |
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 characters in a file. |
03:28 | Type wc on the terminal window. |
03:31 | Now press Enter. 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 the 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 test1 dot txt , the command opens the file test1 dot 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 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, 'n' single 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 is actually happened, we can display the contents of wc_results dot txt by the c-a-t command. |
07:23 | Yes! it has.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 will 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 don't 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 angled- 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, i.e 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 connects 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 unnecessarily 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 multi-page 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. |