Linux/C2/Redirection-Pipes/English-timed

From Script | Spoken-Tutorial
Revision as of 09:36, 11 March 2015 by Sandhya.np14 (Talk | contribs)

Jump to: navigation, search
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 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.
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 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 the standardin to the file 'test1 dot 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 brackets or 2 double right angle brackets.
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 there 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-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, i.e connect the different streams.
10:20 This process is called pipelining.
10:22 Pipes are used to create chains of commands.
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 commands using pipes.
12:15 One common use of pipes is for reading multipage displays.
12:19 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