BOSS-Linux/C2/Working-with-Regular-Files/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:00 Welcome to this spoken tutorial on working with regular files in Linux.
00:07 Files and directories together form the Linux File System.
00:13 In a previous tutorial, we have already seen how to work with directories. You can find the tutorial at this website.
00:25 In this tutorial, we will see how to handle regular files.
00:32 We have already seen in another tutorial how we can create a file using the cat command. For details, please visit this website.
00:46 Let us see how to copy a file from one place to another. For this, we have the cp command.
00:56 Let us see how the command is used.
01:00 To copy a single file, we type: cp space one or more of the [OPTIONs]... space the name of the SOURCE file space the name of the destination file.
01:15 To copy multiple files at the same time, we write: cp space one or more of the [OPTIONs]...the name of the SOURCE files that we want to copy and the name of the destination DIRECTORY in which these files would be copied.
01:34 Let us now see an Example. First we open a terminal.
01:42 We already have a file named "test1" in our home directory.
01:49 To see what is in test1, we type $ cat space test1 and press Enter.
02:00 As we can see the content of test1 is shown. Now if we want to copy it into another file called test2, we would write:

$ cp space test1 space test2 and press Enter.

02:22 Now the file has been copied.
02:25 If 'test2' doesn't exist, it would be first created and then the content of 'test1' will be copied to it.
02:35 If it already existed then it would be silently overwritten. To see the copied file, type:

cat test2 and press Enter.

02:52 We can also copy files from and to different directories. For example-

type: cp space /home/anirban/arc/demo1 which is the name of the file that we want to copy, space /home/anirban/demo2 and press Enter.

03:32 What this will do is that it will copy the file 'demo1' from source diretory- /home/anirban/arc/ to the destination directory- /home/anirban; it will copy to a file whose name is demo2.
03:51 To see that the 'demo2' is there, type: ls space /home/anirban and press Enter.
04:07 And as you can see, here is demo2.
04:12 Before moving ahead, let us clear the screen.
04:17 If you want the file to have the same name in the destination directory, you may not even mention the file name. For example-
04:27 Type: cp space /home/anirban/arc/demo1 space /home/anirban/ and press Enter.
04:55 This will again copy the file 'demo1' presenting the /home/anirban/arc/ directory to /home/anirban directory to a file whose name will be 'demo1' as well.
05:11 As before, to see the demo1, type: ls/home/anirban and press Enter.
05:25 And, as you can see the demo1 file is there.
05:30 Again, before moving ahead, let us clear the screen.
05:37 Another instance when we do not need to give the destination file name is when we want to copy multiple files.
05:44 We assume that we have three files named test1, test2, test3 in our home directory.
05:53 Now, we type: cp space test1 space test2 space test3 space /home/anirban/testdir and press Enter.
06:16 This will copy all the three files named test1,test2 and test3 to the directory /home/anirban/testdir without changing their names.
06:30 To see that these files have actually been copied, we will type: ls space /home/anirban/testdir and press Enter.
06:52 As you can see test1,test2 and test3 are present in this directory.
06:58 There are many options that go with cp. Here, we will see only the most important of them.
07:07 Let us first go back to the slides.
07:12 Among the options, -R (The capital R) is an important one. It causes recursive copying of an entire directory structure.
07:23 Let us see an example.
07:27 Let us try to copy all the contents of the 'testdir' directory to a directory called 'test'.
07:36 For that, we would type: cp space testdir slash test and press Enter.
07:51 As you can see from the output message,
07:54 normally we cannot copy a directory having some contents directly with the 'cp command.
08:02 But using the -R (mius R ) option we can do this.
08:07 Now we type: cp space -R(minus capital R) space testdir/ test and press Enter.
08:25 The files have now been copied. To see that the test directory actually exists, type: ls and press Enter.
08:37 As you can see, the 'test' directory exists. Let us clear the screen.
08:45 To see the contents inside 'test', type: ls test and press Enter.
08:57 You can see the contents of the 'test' directory.
09:01 Now, we go back to the slides.
09:05 We have seen if a file is copied to another file that already exists, the existing file is overwritten.
09:14 Now, what if we inadvertently overwrite an important file?
09:19 To prevent anything like this to occur, we have the '-b' option.
09:25 This makes a backup of each existing destination file.
09:32 We can also use the -i(interactive)option. This always warns us before overwriting any destination file.
09:43 Now, let us see how the mv command works.
09:47 This is used for moving files. Now how is that useful?
09:53 It has two major uses.
09:57 It is used for renaming a file or directory.
10:00 It also moves a group of files to a different directory.
10:05 mv is very similar to cp which we have already seen. So, let us quickly see how mv can be used.
10:17 We open the terminal and type: mv space test1 space test2 and press Enter.
10:32 This will rename the file named test1 which was already present in the home directory to a file named test2.
10:40 If test2 already existed then it would be overwritten silently.
10:49 If we want our warning before the file is overwritten,
10:54 we can use the -i option with the mv command.
10:59 Say we have another file named 'anirban'. This file we also want to renew as 'test2'.
11:08 We will type: mv space -i space anirban space test2 and press Enter.
11:21 As you can see, a warning is provided asking whether test2 should be overwritten or not.
11:30 If we press 'y' and then press Enter, the file would be actually overwritten.
11:37 Like cp, we can use mv with multiple files but in that case the destination should be a directory.
11:47 Before moving ahead, let us clear the screen.
11:52 Suppose we have 3 files named abc.txt, pop.txt and push.txt in our home directory.
12:03 To see their presence, type ls and press Enter.
12:09 Here are the files pop.txt, push.txt and abc.txt. Let us clear the screen.
12:24 Now we want to move these three files to a directory called testdir.
12:32 What we need to do is, type: mv space abc.txt pop.txt push.txt and then the name of the destination folder which is testdir and press Enter.
12:58 To see them, type: ls testdir and press Enter.
13:06 You can see the files abc, pop and push.txt.
13:14 Now, let us see some options that go with mv'. Let us first go back to the slides.
13:22 The '-b' or '–backup' option is present with the mv command. It will backup every file in the destination before it is overwritten.
13:34 The '-i' (minus i) option that we have already seen, warns us before overwriting any destination file.
13:44 The next command we will see is the rm command. This command is used for deleting files.
13:52 Go back to the terminal and type: ls testdir.
14:00 We can see a file name 'faq.txt' present. Say we want to delete it.
14:09 For this, we type: rm space testdir/faq.txt and press Enter.
14:23 This command will remove the file 'faq.txt' from the /testdir directory.
14:32 To see that the file has been actually removed or not, let us again press ls space testdir and press Enter.
14:47 We can no longer see the file 'faq.txt'.
14:51 We can use the rm command with multiple files as well.
14:57 The testdir directory contains two files 'abc2' and 'abc1.'
15:03 Suppose, we want to remove these files- 'abc1' and 'abc2.'
15:09 For this, we would type: rm space testdir/abc1 space testdir/abc2 and press Enter.
15:31 This removes the files 'abc1' and 'abc2' from testdir directory.
15:39 To see that they have been removed, type: ls space testdir again. You can no longer see abc1' and abc2.
15:53 Let us clear the screen before moving ahead.
15:58 Now, let us go back to the slides.
16:02 Let us summarize what we just said.
16:04 That is, to delete a single file we write rm and then the name of the file.
16:11 To delete multiple files, we write rm and the name of the multiple files that we want to delete.
16:19 Now, let us look into some of the options of the rm command.
16:24 Sometimes a file is write protected. Using rm will not delete the file then. In this case, we have the -f option which can be used to force delete a file.
16:41 The other common option is the -r option. Let us see where these option are useful?
16:52 Let us switch back to the terminal.
16:57 rm command is not normally used for deleting directories. For that, we have the rmdir command.
17:05 But rmdir command normally deletes a directory only when it is empty.
17:12 What if we want to delete a directory that has a number of files and sub-directories inside?
17:19 Let us try the rm command to do this.
17:23 Let us type rm and the directory that we want to delete which is testdir and press Enter.
17:31 From the output message, we can see that we can not use the rm directory to delete 'testdir'.
17:39 But if we combine the '-r' and '-f' options then we can do this.
17:47 Press rm -rf testdir and then press Enter.
18:00 Now, the testdir directory has been successfully deleted.
18:06 Let us now go back to the slides to study the next command-
18:11 the cmp command.
18:13 Sometimes we need to check whether two files are same. If they are same then we may delete one of them.
18:22 Also we may want to see whether a file has changed since the last version.
18:28 For these and many other purposes, we can use the cmp command.
18:33 It compares two files byte by byte.
18:38 To compare 'file1' and 'file2', we would write cmp space file1 space file2.
18:47 If the two files have exactly the same contents then no message would be shown.
18:55 Only the prompt will be printed.
18:58 If there are differences in their contents then the location of the first mismatch will be printed on the terminal.
19:10 Let us see how cmp works. We have two files named sample1 and sample2 in our home directory.
19:19 Let us see what they contain?
19:22 Type: cat sample1 and press Enter. It contains the text- "This is a Linux file to test the cmp command".
19:34 The other file sample2 will contain the text and to see that we will type: cat sample2 and press Enter.
19:44 It will contain the text- "This is a Unix file to test the cmp command".
19:50 Now we would apply the cmp command on these two files.
19:55 We will write: cmp space sample1 space sample2 and press Enter.
20:08 As we can see, the first difference between the two files sample1 and sample2 is pointed out.
20:16 Let us clear the screen before moving ahead to the next command.
20:22 The next command we will see is the wc command.
20:26 This command is used to count the number of characters, words and lines in a file.
20:34 We have a file named "sample3" in our home directory.
20:39 Let us see its contents. For that, we will type: cat space sample3 and press Enter.
20:50 This is the content of sample3.
20:54 Now, let us use the wc command on this file.
20:59 For that, we would write wc space sample3 and press Enter.
21:10 This command points out that the file has 6 lines, 67 words and 385 characters.
21:22 These were some of the commands that help us to work with files.
21:27 There are many more commands. Moreover each of the command that we saw has many other options.
21:36 I encourage you to see more about them using the man command.
21:44 This brings me to the end of this tutorial at last.
21:48 Spoken Tutorial Project is a part of the "Talk to a Teacher" project, supported by the National Mission on Education through ICT, MHRD, Government of India.
22:02 More information on the same is available at the following link- http://spoken-tutorial.org/NMEICT-Intro.
22:18 This is Anirban, signing off . Thanks for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14