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