1 00:00:00 --> 00:00:06 Welcome to this spoken tutorial on Redirection and Pipes. 2 00:00:07 --> 00:00:08 I am using Ubuntu 10.04. 3 00:00:09 --> 00:00:15 We assume that you already have an idea about the Linux operating system and have some basic idea about commands. 4 00:00:16 --> 00:00:21 If you are interested, it is available through another spoken tutorial, on the following website. 5 00:00:22 --> 00:00:24 Also note that Linux is case sensitive. 6 00:00:25 --> 00:00:31 All the commands used in this tutorial are in lower case unless otherwise mentioned. 7 00:00:32 --> 00:00:34 Most of the work that we do in Linux is through a terminal. 8 00:00:35 --> 00:00:38 When we have to execute a command, we generally type through the keyboard. 9 00:00:39 --> 00:00:40 Say we have to find the date and time. 10 00:00:41 --> 00:00:45 We simply type through the keyboard date and press Enter. 11 00:00:46 --> 00:00:47 So, we normally give input through the keyboard. 12 00:00:48 --> 00:00:55 Similarly we see that the output of our command is also displayed at the terminal window. 13 00:00:56 --> 00:00:58 Also, say some error occurs while we are executing some command. 14 00:00:59 --> 00:01:04 For example, we type cat space aaa and press Enter. 15 00:01:05 --> 00:01:07 Now, no such file by the name 'aaa' exists. 16 00:01:08 --> 00:01:09 So, there is an error displayed, saying so. 17 00:01:10 --> 00:01:19 Now this error also comes on the terminal window. Hence we see error reporting is also at the terminal. 18 00:01:20 --> 00:01:23 Now inputting, outputting and error reporting are the three special actions related to commands. 19 00:01:24 --> 00:01:30 Before learning about redirection, we should know about two important concepts. That of stream and file descriptor. 20 00:01:31 --> 00:01:36 A Linux shell like Bash, receives input and sends output as sequences or streams of characters. 21 00:01:37 --> 00:01:40 Each character is independent of the one before it and the one after it. 22 00:01:41 --> 00:01:43 Streams are accessed using 'file IO' techniques. 23 00:01:44 --> 00:01:50 It does not matter whether or not the actual stream of characters comes from or goes to a file, a keyboard, a window etc. 24 00:01:51 --> 00:01:56 In Linux, every open file of a process is associated with an integer number. 25 00:01:57 --> 00:02:04 These numeric values are known as file descriptors. 26 00:02:05 --> 00:02:07 Linux shells use three standard I/O streams. 27 00:02:08 --> 00:02:11 Each of them is associated with a well-known file descriptor. 28 00:02:12 --> 00:02:14 stdin is the standard input stream. 29 00:02:15 --> 00:02:16 It provides input to commands. 30 00:02:17 --> 00:02:18 It has file descriptor 0. 31 00:02:19 --> 00:02:21 stdout is the standard output stream. 32 00:02:22 --> 00:02:25 It displays output from commands. It has file descriptor 1. 33 00:02:26 --> 00:02:35 stderr is the standard error stream. It displays error output from commands. It has file descriptor 2. 34 00:02:36 --> 00:02:39 Input streams provide input to programs. 35 00:02:40 --> 00:02:43 By default, it takes from terminal keystrokes. 36 00:02:44 --> 00:02:46 Output streams print text characters, by default to the terminal. 37 00:02:47 --> 00:02:51 The terminal was originally an ASCII typewriter or display terminal. 38 00:02:52 --> 00:02:55 But is now more often a text window on a graphical desktop. 39 00:02:56 --> 00:03:00 We have seen that the 3 streams are connected to some files by default. 40 00:03:01 --> 00:03:03 But in Linux, we can change this default behavior. 41 00:03:04 --> 00:03:06 We can connect these 3 streams to other files. 42 00:03:07 --> 00:03:08 This process is called Redirection. 43 00:03:09 --> 00:03:13 Now let us see how redirection is done in the 3 streams. 44 00:03:14 --> 00:03:16 First let us see how the standard input is redirected. 45 00:03:17 --> 00:03:21 we redirect standardin from a file, using the '<' (left angled bracket) operator. Let us see how. 46 00:03:22 --> 00:03:27 We know that the use of wc command is to find the number of lines, words and characters in a file. 47 00:03:28 --> 00:03:30 Type wc on the terminal window. 48 00:03:31 --> 00:03:31 Now press Enter. 49 00:03:32 --> 00:03:36 What happens?? we have a blinking cursor. It means that we need to enter through the keyboard. 50 00:03:37 --> 00:03:45 Enter some text, say This tutorial is very important. 51 00:03:46 --> 00:03:47 Now press Enter. 52 00:03:48 --> 00:03:51 Now press Ctrl and d keys together. 53 00:03:52 --> 00:03:54 Now the command will work on the lines that we entered. 54 00:03:55 --> 00:03:56 The command will give an output on the terminal. 55 00:03:57 --> 00:04:00 Now here no file is given after wc command 56 00:04:01 --> 00:04:03 So it takes input from standard input stream. 57 00:04:04 --> 00:04:11 Now the standard input stream is, by default, connected to the keyboard. Hence wc will take input from keyboard. 58 00:04:12 --> 00:04:18 Now if we write wc space 'left-angled bracket' space test1 dot txt, 59 00:04:19 --> 00:04:26 what happens is that wc will tell us the no. of lines, words and characters in the file 'test1 dot txt'. 60 00:04:27 --> 00:04:33 Now type: wc space test1 dot txt 61 00:04:34 --> 00:04:36 we see the same result. 62 00:04:37 --> 00:04:38 So what is the difference? 63 00:04:39 --> 00:04:45 When we write wc space test1 dot txt , the command opens the file 'test1 dot txt' and reads from it. 64 00:04:46 --> 00:04:52 But when we write wc space 'left-angled bracket' test1 dot txt, wc still does not get any file to open. 65 00:04:53 --> 00:04:56 Instead it looks to pick up the input from standardin. 66 00:04:57 --> 00:05:00 Now we have directed the standardin to the file 'test1 dot txt'. 67 00:05:01 --> 00:05:03 Hence the command reads from 'test1'. 68 00:05:04 --> 00:05:09 But actually it is unaware as to from where the data is coming to standardin. 69 00:05:10 --> 00:05:11 So, we have seen how to redirect standard input. 70 00:05:12 --> 00:05:16 Now, let us see how to redirect standard output and standard error. 71 00:05:17 --> 00:05:19 There are two ways to redirect output or error to a file: 72 00:05:20 --> 00:05:28 Suppose, 'n' refers to the file descriptor. 'n single right-angled bracket' redirects output from file descriptor n to a file. 73 00:05:29 --> 00:05:31 You must have write authority to the file. 74 00:05:32 --> 00:05:34 If the file does not exist, it is created. 75 00:05:35 --> 00:05:39 If it does exist, the existing contents are usually lost without any warning. 76 00:05:40 --> 00:05:46 'n double right-angled bracket' also redirects output from file descriptor n to a file. 77 00:05:47 --> 00:05:49 Again, you must have write authority to the file. 78 00:05:50 --> 00:05:51 If the file does not exist, it is created. 79 00:05:52 --> 00:05:58 If it does exist, the output is appended to the existing file. 80 00:05:59 --> 00:06:04 The n in 'n single right angle bracket' or 'n double right angle bracket' refers to the file descriptor. 81 00:06:05 --> 00:06:09 If it is omitted, then standard output i.e. file descriptor 1 is assumed. 82 00:06:10 --> 00:06:14 So, just a right angle bracket is same as 1 right angle bracket. 83 00:06:15 --> 00:06:21 But to redirect error stream, you have to use 2 right angle brackets or 2 double right angle brackets. 84 00:06:22 --> 00:06:23 Let us see this practically. 85 00:06:24 --> 00:06:30 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. 86 00:06:31 --> 00:06:33 What if we do not want to display this on the terminal? 87 00:06:34 --> 00:06:37 We want to store it in a file so that the information can be later used. 88 00:06:38 --> 00:06:41 By default, wc writes its output to the standardout. 89 00:06:42 --> 00:06:44 The standardout is by default connected to the terminal window. 90 00:06:45 --> 00:06:47 Hence we see the output in the terminal window. 91 00:06:48 --> 00:06:56 But if we can redirect the standardout to a file, then the output from the wc command will be written to that file. 92 00:06:57 --> 00:07:08 Say, we write: wc space test1 dot txt 'right-angled bracket' wc_results dot txt. 93 00:07:09 --> 00:07:10 Press Enter. 94 00:07:11 --> 00:07:22 Now, to see whether this has actually happened we can display the contents of wc_results dot txt by the c-a-t command. 95 00:07:23 --> 00:07:23 Yes it has. 96 00:07:24 --> 00:07:29 Suppose, we have another file test2 in the same directory. 97 00:07:30 --> 00:07:43 Now we again execute the command with test2 file. We type: wc space test2 dot txt 'right-angled bracket' wc_results dot txt 98 00:07:44 --> 00:07:47 So, the contents of the file wc_results would be overwritten. 99 00:07:48 --> 00:07:55 Let's see this. 100 00:07:56 --> 00:08:06 Instead, if we write wc space test1 dot txt 'right-angled bracket' twice wc underscore results dot txt, 101 00:08:07 --> 00:08:14 the new contents will not overwrite the already present contents of the file wc underscore results dot txt, it would be appended. 102 00:08:15 --> 00:08:25 Let's see this too. 103 00:08:26 --> 00:08:28 Redirecting the standard error is done similarly. 104 00:08:29 --> 00:08:37 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. 105 00:08:38 --> 00:08:45 Like we know that there exists no file by the name 'aaa', write the following: wc space aaa 106 00:08:46 --> 00:08:49 The shell will give the error No such file or directory. 107 00:08:50 --> 00:08:54 Now, say we don't want error messages on screen. They can be redirected to some other file . 108 00:08:55 --> 00:09:05 For this, we may give the command: wc space aaa space 2 'right-anged bracket' errorlog dot txt 109 00:09:06 --> 00:09:11 Now the error will not show on the terminal, rather it will be written in the file errorlog dot txt. 110 00:09:12 --> 00:09:21 we can see this by the command cat space errorlog dot txt. 111 00:09:22 --> 00:09:33 Now, suppose that I make some other error by running the command cat space bbb space 2 'right-angled bracket' errorlog dot txt. 112 00:09:34 --> 00:09:38 The previous error would be overwritten and the new error will show. 113 00:09:39 --> 00:09:45 See cat space errorlog dot txt 114 00:09:46 --> 00:09:57 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 115 00:09:58 --> 00:10:05 We check this using the cat command. 116 00:10:06 --> 00:10:19 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. 117 00:10:20 --> 00:10:21 This process is called pipelining. 118 00:10:22 --> 00:10:24 Pipes are used to create chains of commands. 119 00:10:25 --> 00:10:29 A Pipe connects the output of one command to the input of the next command in the chain. 120 00:10:30 --> 00:10:45 It looks like command1 vertical bar command2 hyphen option vertical bar command3 hyphen option1 hyphen option2 vertical bar command4. 121 00:10:46 --> 00:10:50 Say we want to know the total number of files and directories present in the current directory. 122 00:10:51 --> 00:10:57 What we can do? We know ls space minus l will list all files and directories of the present directory. 123 00:10:58 --> 00:11:07 We can redirect the output to a file: ls space minus l 'right-angled bracket' files dot txt. 124 00:11:08 --> 00:11:13 Run cat space files dot txt. 125 00:11:14 --> 00:11:16 Now each line is a name of a file or directory . 126 00:11:17 --> 00:11:23 So, if we measure the total lines in this file, we can use files dot txt to serve our purpose. 127 00:11:24 --> 00:11:31 This we can do using the command wc space minus l files dot txt. 128 00:11:32 --> 00:11:34 Though this serves our purpose there are a few problems. 129 00:11:35 --> 00:11:39 First, we need an intermediate file. Here files dot txt. 130 00:11:40 --> 00:11:45 If the first command produces a lot of data, it may unnecessarily eat away the disk memory. 131 00:11:46 --> 00:11:49 Also if we want to chain several commands, this method is slow. 132 00:11:50 --> 00:12:00 We can do it much easily using pipes like this. We write: ls space minus l 'vertical bar' wc space minus l 133 00:12:01 --> 00:12:05 and we can get the same result with much more ease. 134 00:12:06 --> 00:12:09 The output from the ls command goes as input for the wc command. 135 00:12:10 --> 00:12:14 We can add even longer chains of commands using pipes. 136 00:12:15 --> 00:12:18 One common use of pipes is for reading multipage displays. 137 00:12:19 --> 00:12:23 cd space slash user slash bin. 138 00:12:24 --> 00:12:27 So, we are now in bin directory. 139 00:12:28 --> 00:12:30 Now run "ls minus l" 140 00:12:31 --> 00:12:36 We cannot see the output properly. But if we use it joined with a pipe to more, we can. 141 00:12:37 --> 00:12:40 Press Enter to scroll through the list. 142 00:12:41 --> 00:12:44 Press "q" to come out of it. 143 00:12:45 --> 00:12:47 These were some of the commands that help us to work with files. 144 00:12:48 --> 00:12:49 There are many more commands. 145 00:12:50 --> 00:12:53 Moreover each of the commands that we saw has many other options. 146 00:12:54 --> 00:12:57 I encourage you to see more about them using the man command. 147 00:12:58 --> 00:13:03 The best way of learning commands is to use them again and again. 148 00:13:04 --> 00:13:06 This brings me to the end of this tutorial. 149 00:13:07 --> 00:13:14 Spoken Tutorials are a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT. 150 00:13:15 --> 00:13:18 More information on the same is available at the following link . 151 00:13:19 --> 00:13:24 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.