Difference between revisions of "Linux/C2/Working-with-Regular-Files/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 
|-
 
|-
| 0:00
+
| 00:00
|Welcome to this spoken tutorial on working with regular files in Linux.  
+
|Welcome to this spoken tutorial on '''Working with Regular Files''' in '''Linux'''.  
  
 
|-
 
|-
| 0:07
+
| 00:07
 
|Files and directories together form the Linux File System.
 
|Files and directories together form the Linux File System.
  
 
|-
 
|-
| 0:13
+
| 00:13
|In a previous tutorial we have already seen how to work with directories. You can find the tutorial at this website.
+
|In a previous tutorial, we have already seen how to work with directories. You can find the tutorial at this website.
  
 
|-
 
|-
| 0:25
+
| 00:25
|In this tutorial we will see how to handle regular files.  
+
|In this tutorial, we will see how to handle regular files.  
  
 
|-
 
|-
| 0:31
+
| 00:31
|We have already seen in another tutorial how we can create a file using the cat command. For details please visit this website.
+
|We have already seen in another tutorial how we can create a file using the ''' 'cat' command'''. For details, please visit this website.
  
 
|-
 
|-
| 0:46
+
| 00:46
|Let us see how to copy a file from one place to another. For this we have the cp command.
+
|Let us see how to copy a file from one place to another. For this we have the ''' 'cp' command'''.
  
 
|-
 
|-
| 0:55
+
| 00:55
 
|Let us see how the command is used.
 
|Let us see how the command is used.
  
 
|-
 
|-
| 1:00
+
| 01:00
|To copy a single file we type
+
|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 (DEST).  
cp space one or more of the [OPTION]... space the name of the SOURCE file space the name of the destination file DEST.
+
  
 
|-
 
|-
| 1:15
+
| 01:15
|To copy multiple files at the same time.
+
|To copy multiple files at the same time,
We write cp space one or more of the [OPTION]...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.
+
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.
  
 
|-
 
|-
| 1:34
+
| 01:34
|Let us now see an Eg.First we open a terminal.  
+
|Let us now see an eg.First we open a terminal.  
  
 
|-
 
|-
| 1:42
+
| 01:42
|We already have a file named test1 in /home/anirban/arc/
+
|We already have a file named 'test1' in our home directory.
 
   
 
   
 
|-
 
|-
| 1:49
+
| 01:49
|To see what is in test1.We type
+
|To see what is in test1, we type:$ cat space test1 and press Enter.
$ cat test1 and press enter.
+
  
 
|-
 
|-
| 2:00
+
| 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.
+
|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.
$ cp test1 test2 and press enter.
+
  
 
|-
 
|-
| 2:22
+
| 02:22
 
|Now the file has been copied.
 
|Now the file has been copied.
  
 
|-
 
|-
| 2:25
+
| 02:25
|If test2 doesn't exist it would be first created and then the content of test1 will be copied to it.
+
|If 'test2' doesn't exist, it would be first created and then the content of 'test1' will be copied to it.
  
 
|-
 
|-
| 2:35
+
| 02:35
|If it already existed then it would be silently overwritten.To see the copied file type
+
|If it already existed then it would be silently overwritten. To see the copied file, type:
 
+
$ cat space test2 and press Enter.
$ cat test2 and press enter.
+
  
 
|-
 
|-
| 2:52
+
| 02:52
|You can also copy files from and to different directories.For example.
+
|We can also copy files from and to different directories. For example,
 
+
type: $ cp space /home/anirban/arc/demo1 (which is th name of the file we want to copy) space /home/anirban/demo2 and press Enter.
 
+
type
+
 
+
$ cp /home/anirban/arc/demo1 /home/anirban/demo2 and press enter.
+
  
 
|-
 
|-
| 3:31
+
| 03:31
|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 name demo2.
+
|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 name 'demo2'.
  
 
|-
 
|-
| 3:51
+
| 03:51
|To see that the demo2 is there type
+
|To see that the 'demo2' is there, type:
ls space /home/anirban and press enter.
+
ls space /home/anirban and press Enter.
  
 
|-
 
|-
| 4:13
+
| 04:13
|We scroll up as you can see here is demo2.
+
|We scroll up as you can see here is 'demo2'.
  
 
|-
 
|-
| 4:19
+
| 04:19
|Before moving a head let us clear the screen.
+
|Before moving ahead let us clear the screen.
  
 
|-
 
|-
| 4:25
+
| 04:25
|If you want the file to have the same name in the destination directory, you may not even mention the file name.For example
+
|If you want the file to have the same name in the destination directory, you may not even mention the file name. For example
  
 
|-
 
|-
| 4:35
+
| 04:35
|Type $ cp /home/anirban/arc/demo1 /home/anirban/ and press enter.
+
|Type $ cp space /home/anirban/arc/demo1 space /home/anirban/ and press Enter.
  
 
|-
 
|-
| 5:03
+
| 05:03
|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.
+
|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.
  
 
|-
 
|-
| 5:20
+
| 05:20
|As before to see the demo1 type
+
|As before, to see the 'demo1' type:
ls/home/anirban and press enter.
+
ls /home/anirban and press Enter.
  
 
|-
 
|-
| 5:33
+
| 05:33
|Here again we would scroll up and as you can see the demo1 file is there.
+
|Here again we would scroll up and as you can see the 'demo1' file is there.
  
 
|-
 
|-
| 5:40
+
| 05:40
|Again before moving a head let us clear the screen.
+
|Again, before moving ahead let us clear the screen.
  
 
|-
 
|-
| 5:48
+
| 05:48
 
|Another instance when we do not need to give the destination file name is when we want to copy multiple files.
 
|Another instance when we do not need to give the destination file name is when we want to copy multiple files.
  
 
|-
 
|-
| 5:56
+
| 05:56
|We assume that we have three files named test1 test2 test3 in our home directory.
+
|We assume that we have three files named test1, test2, test3 in our '''home directory'''.
  
 
|-
 
|-
| 6:04
+
| 06:04
|Now we type $ cp test1 test2 test3 /home/anirban/testdir and press enter.
+
|Now we type: $ cp space test1 space test2 space test3 space /home/anirban/testdir and press Enter.
  
 
|-
 
|-
| 6:27
+
| 06:27
|This will copy all the three files test1,test2 and test3 to the directory /home/anirban/testdir without changing their names.  
+
|This will copy all the three files named test1,test2 and test3 to the directory /home/anirban/testdir without changing their names.  
  
 
|-
 
|-
| 6:41
+
| 06:41
|You see that this files have actually been copied.We will type ls /home/anirban/testdir and press enter.
+
|To see that these files have actually been copied. We will type: ls space /home/anirban/testdir and press Enter.
  
 
|-
 
|-
| 7:03
+
| 07:03
|As you can see test1,test2 and test3 are present in this directory.
+
|As you can see test1, test2 and test3 are present in this directory.
 
+
  
 
|-
 
|-
| 7:10
+
| 07:10
|There are many options that go with cp. Here we will see only the most important of them.
+
|There are many options that go with '''cp'''. Here we will see only the most important of them.
  
 
|-
 
|-
| 7:18
+
| 07:18
|Let us first will go back to the slides.
+
|Let us first go back to the slides.
  
 
|-
 
|-
| 7:23
+
| 07:23
|Among the options -R is an important one. It causes recursive copying of an entire directory structure.
+
|Among the options, '''-R''' is an important one. It causes recursive copying of an entire directory structure.
  
 
|-
 
|-
| 7:33
+
| 07:33
 
|Let us see an example.
 
|Let us see an example.
  
 
|-
 
|-
| 7:38
+
| 07:38
|Let us try to copy all the contents of the testdir directory to a directory called test.
+
|Let us try to copy all the contents of the 'testdir' directory to a directory called 'test'.
 
   
 
   
 
|-
 
|-
| 7:48
+
| 07:48
|For that we would type cp testdir/ test and press enter.
+
|For that, we would type: cp space testdir/ space test and press Enter.
  
 
|-
 
|-
| 8:02
+
| 08:02
|As you can see from the output message.
+
|As you can see from the output message,
  
 
|-
 
|-
| 8:06
+
| 08:06
|Normally we cannot copy a directory having a some content directly with cp command.
+
|normally we cannot copy a directory having some content directly with '''cp''' command.
  
 
|-
 
|-
| 8:14
+
| 08:14
|But using the -R option we can do this.
+
|But using the '''-R''' option we can do this.
  
 
|-
 
|-
| 8:19
+
| 08:19
|Now we type cp -R testdir/ test and press enter.
+
|Now we type: cp space -R space testdir/ space test and press Enter.
  
 
|-
 
|-
| 8:36
+
| 08:36
|The files have now been copied, to see that the test directory actually exist type ls and press enter.
+
|The files have now been copied. To see that the 'test' directory actually exists, type '''ls''' and press Enter.
  
 
|-
 
|-
| 8:47
+
| 08:47
|As you can see the test directory exists. Let us clear the screen.
+
|As you can see, the 'test' directory exists. Let us clear the screen.
  
 
|-
 
|-
| 8:57
+
| 08:57
|To see the contents inside test type ls test and press enter.
+
|To see the contents inside 'test', type: ls space test and press Enter.
  
 
|-
 
|-
| 9:08
+
| 09:08
|You can see the contents of the test directory.
+
|You can see the contents of the 'test' directory.
  
 
|-
 
|-
| 9:13
+
| 09:13
 
|Now we go back to the slides.
 
|Now we go back to the slides.
  
 
|-
 
|-
| 9:16
+
| 09:16
|We have seen if a file is copied to another file that already exists the existing file is overwritten.
+
|We have seen if a file is copied to another file that already exists, the existing file is overwritten.
  
 
|-
 
|-
| 9:25
+
| 09:25
 
|Now what if we inadvertently overwrite an important file?
 
|Now what if we inadvertently overwrite an important file?
  
 
|-
 
|-
| 9:30
+
| 09:30
|To prevent anything like this to occur, we have the -b option.
+
|To prevent anything like this to occur, we have the '''-b''' option.
  
 
|-
 
|-
| 9:36
+
| 09:36
|This makes a backup of each exiting destination file.
+
|This makes a ''backup'' of each exiting destination file.
  
 
|-
 
|-
| 9:41
+
| 09:41
|We can also use the -i(interactive)option, this always warns us before overwriting any destination file.  
+
|We can also use the '''-i''' (interactive)option, this always warns us before overwriting any destination file.  
  
 
|-
 
|-
| 9:54
+
| 09:54
|Now let us see how the mv command works.  
+
|Now let us see how the '''mv command''' works.  
  
 
|-
 
|-
| 9:59
+
| 09:59
 
|This is used for moving files. Now how is that useful?
 
|This is used for moving files. Now how is that useful?
  
Line 243: Line 234:
 
|-
 
|-
 
| 10:07
 
| 10:07
|It is used for rename a file or directory.
+
|It is used for ''renaming a file'' or directory.
  
 
|-
 
|-
 
| 10:11
 
| 10:11
|It also moves a group of files to a different directory.
+
|It also ''moves a group of files'' to a different directory.
  
 
|-
 
|-
 
| 10:17
 
| 10:17
|mv is very similar to cp which we have already seen. So let us quickly see how mv can be used.  
+
|'''mv''' is very similar to '''cp''' which we have already seen. So let us quickly see how '''mv''' can be used.  
  
 
|-
 
|-
 
| 10:29
 
| 10:29
|We open the terminal and type $ mv test1 test2 and press enter.
+
|We open the terminal and type: $ mv space test1 space test2 and press Enter.
  
 
|-
 
|-
 
| 10:43
 
| 10:43
|This will rename the file named test1 which was already present in the home directory to a file named test2.  
+
|This will rename the file named 'test1' which was already present in the home directory to a file named 'test2'.  
  
 
|-
 
|-
 
| 10:52
 
| 10:52
|If test2 already existed then it would be overwritten silently.
+
|If 'test2' already existed then it would be overwritten silently.
  
 
|-
 
|-
 
| 11:00
 
| 11:00
|If we want our warning before the file is overwritten.
+
|If we want our warning before the file is overwritten,
  
 
|-
 
|-
 
| 11:05
 
| 11:05
|We can use the -i option with the mv command.
+
|we can use the '''-i''' option with the '''mv''' command.
  
 
|-
 
|-
 
| 11:10
 
| 11:10
|Say we have another file named anirban. This file we also want to renew as test2  
+
|Say we have another file named 'anirban'. This file we also want to renew as 'test2'.
  
 
|-
 
|-
 
| 11:20
 
| 11:20
|We will type mv -i anirban test2 and press enter.
+
|We will type: mv space -i space anirban space test2 and press Enter.
  
 
|-
 
|-
 
| 11:32
 
| 11:32
|As you can see a warning is provided asking whether test2 should be overwritten or not.  
+
|As you can see, a warning is provided asking whether 'test2' should be overwritten or not.  
  
 
|-
 
|-
 
| 11:41
 
| 11:41
|If we press y and then press enter, the file would be actually overwritten.
+
|If we press '''y''' and then press Enter, the file would be actually overwritten.
  
 
|-
 
|-
 
| 11:49
 
| 11:49
|Like cp we can use mv with multiple files but in that case the destination should be a directory.
+
|Like '''cp''' we can use '''mv''' with multiple files but in that case the destination should be a directory.
  
 
|-
 
|-
Line 299: Line 290:
 
|-
 
|-
 
| 12:03
 
| 12:03
|Suppose we have 3 files named abc.txt, pop.txt and push.txt in our home directory.
+
|Suppose we have 3 files named abc.txt, pop.txt and push.txt in our '''home''' directory.
  
 
|-
 
|-
 
| 12:14
 
| 12:14
|To see there presence type ls and press enter.
+
|To see there presence, type '''ls''' and press Enter.
  
 
|-
 
|-
 
| 12:21
 
| 12:21
|Here are the files pop.txt,push.txt and abc.txt Let us clear the screen.
+
|Here are the files pop.txt, push.txt and abc.txt Let us clear the screen.
  
 
|-
 
|-
 
| 12:36
 
| 12:36
|Now we want to move this three files to a directory called testdir.
+
|Now we want to move these three files to a directory called 'testdir'.
  
 
|-
 
|-
 
| 12:46
 
| 12:46
|What we need to do is type mv abc.txt pop.txt push.txt and then the name of the destination folder which is testdir and press enter.
+
|What we need to do is type:''' mv space abc.txt space pop.txt space push.txt''' and then the name of the destination folder which is 'testdir' and press Enter.
  
 
|-
 
|-
 
| 13:14
 
| 13:14
|To see them type ls testdir and press enter.
+
|To see them, type '''ls space testdir''' and press Enter.
  
 
|-
 
|-
Line 327: Line 318:
 
|-
 
|-
 
| 13:27
 
| 13:27
|Now let us see some options that go with mv. Let us first go back to the slides.
+
|Now let us see some options that go with '''mv'''. Let us first go back to the slides.
  
 
|-
 
|-
 
| 13:37
 
| 13:37
|Then -b or –backup option is present with the mv command.It will backup every file in the destination before it is overwritten.  
+
|Then '''-b''' or '''–backup''' option is present with the '''mv''' command. It will backup every file in the destination before it is overwritten.  
  
 
|-
 
|-
 
| 13:48
 
| 13:48
|The -i option that we have already seen warns us before overwriting any destination file.
+
|The '''-i''' option that we have already seen warns us before overwriting any destination file.
  
 
|-
 
|-
 
| 13:58
 
| 13:58
|The next command we will see is the rm command. This command is used for deleting files.  
+
|The next command we will see is the '''rm command'''. This command is used for ''deleting'' files.  
  
 
|-
 
|-
 
| 14:06
 
| 14:06
|Go back to the terminal and type ls testdir.
+
|Go back to the terminal and type: ls space testdir
  
 
|-
 
|-
 
| 14:15
 
| 14:15
|We can see a file name faq.txt present.Say we want to delete it.
+
|We can see a file name 'faq.txt' present. Say, we want to delete it.
  
 
|-
 
|-
 
| 14:23
 
| 14:23
|For this we type
+
|For this, we type:
$ rm testdir/faq.txt and press enter.
+
$ rm space testdir/faq.txt and press Enter.
  
 
|-
 
|-
 
| 14:37
 
| 14:37
|This command will remove the file faq.txt from the /testdir directory.
+
|This command will remove the file 'faq.txt' from the '/testdir' directory.
  
 
|-
 
|-
 
| 14:46
 
| 14:46
|To see that the file has been actually removed or not.Let us again press ls testdir and press enter.
+
|To see that the file has been actually removed or not, let us again press:ls space testdir and press Enter.
  
 
|-
 
|-
 
| 15:00
 
| 15:00
|We can no longer see the file faq.txt.
+
|We can no longer see the file 'faq.txt'.
  
 
|-
 
|-
 
| 15:05
 
| 15:05
|We can use the rm command with multiple files as well.
+
|We can use the '''rm''' command with multiple files as well.
  
 
|-
 
|-
 
| 15:10
 
| 15:10
|The testdir directory contains two files abc2 and abc1.
+
|The 'testdir' directory contains two files 'abc2' and 'abc1'.
  
 
|-
 
|-
 
| 15:17
 
| 15:17
|Suppose we want to remove this files abc1 and abc2.
+
|Suppose we want to remove these files abc1 and abc2.
  
 
|-
 
|-
 
| 15:23
 
| 15:23
|For this we would type rm testdir/abc1 testdir/abc2 and press enter.
+
|For this, we would type: rm space testdir/abc1 space testdir/abc2 and press Enter.
  
 
|-
 
|-
 
| 15:45
 
| 15:45
|This remove the files abc1 and abc2 from testdir directory.
+
|This removes the files 'abc1' and 'abc2' from 'testdir' directory.
  
 
|-
 
|-
 
| 15:53
 
| 15:53
|To see that they have been removed type ls testdir again.You can no longer see abc1 and abc2.
+
|To see that they have been removed, type '''ls space testdir''' again. You can no longer see 'abc1' and 'abc2'.
  
 
|-
 
|-
Line 404: Line 395:
 
|-
 
|-
 
| 16:20
 
| 16:20
|That is do delete a single file we write rm and than the name of the file.
+
|That is, to delete a single file we write '''rm''' and then the name of the file.
  
 
|-
 
|-
 
| 16:27
 
| 16:27
|To delete multiple files we write rm and the name of the multiple files that we want to delete.
+
|To delete multiple files we write '''rm''' and the name of the multiple files that we want to delete.
  
 
|-
 
|-
 
| 16:34
 
| 16:34
|Now let us look into some of the options of the rm command.
+
|Now let us look into some of the options of the '''rm''' command.
  
 
|-
 
|-
 
| 16:40
 
| 16:40
|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.
+
|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:57
 
| 16:57
|The other common option is the -r option. Let us see where this options are useful?
+
|The other common option is the '''-r''' option. Let us see where this options are useful?
  
 
|-
 
|-
Line 428: Line 419:
 
|-
 
|-
 
| 17:12
 
| 17:12
|rm command is not normally used for deleting directories, for that we have the rmdir command.
+
|'''rm''' command is not normally used for deleting directories, for that we have the '''rmdir''' command.
  
 
|-
 
|-
 
| 17:21
 
| 17:21
|But rmdir command normally deletes a directory only then it is empty.  
+
|But '''rmdir''' command normally deletes a directory, only then it is empty.  
  
 
|-
 
|-
Line 440: Line 431:
 
|-
 
|-
 
| 17:35
 
| 17:35
|Let us try the rm command to do this.
+
|Let us try the '''rm''' command to do this.
  
 
|-
 
|-
 
| 17:38
 
| 17:38
|Let us type rm and the directory that we want to delete which is testdir and press enter.
+
|Let us type '''rm''' and the directory that we want to delete which is 'testdir' and press Enter.
  
 
|-
 
|-
 
| 17:47
 
| 17:47
|From the output message we can see that we can not use the rm directory to delete testdir.
+
|From the output message we can see that we can not use the 'rm directory' to delete 'testdir'.
  
 
|-
 
|-
 
| 17:55
 
| 17:55
|But if we combine the -r and -f option then we can do this.
+
|But if we combine the '''-r''' and '''-f''' option then we can do this.
  
 
|-
 
|-
 
| 18:03
 
| 18:03
|Press rm -rf testdir and then press enter.
+
|Press: rm space -rf space testdir and then press Enter.
  
 
|-
 
|-
 
| 18:16
 
| 18:16
|Now the testdir directory has been successfully deleted.
+
|Now the 'testdir' directory has been successfully deleted.
  
 
|-
 
|-
Line 468: Line 459:
 
|-
 
|-
 
| 18:27
 
| 18:27
|The cmp command.
+
|The '''cmp''' command.
  
 
|-
 
|-
Line 480: Line 471:
 
|-
 
|-
 
| 18:44
 
| 18:44
|For these and many other purposes we can use the cmp command.
+
|For these and many other purposes we can use the '''cmp''' command.
  
 
|-
 
|-
 
| 18:49
 
| 18:49
|It compares two files byte by byte.
+
|It ''compares two files byte by byte''.
  
 
|-
 
|-
 
| 18:54
 
| 18:54
|To compare file1 and file2 we would write cmp file1 file2.
+
|To compare file1 and file2, we would write '''cmp space file1 space file2'''.
  
 
|-
 
|-
Line 504: Line 495:
 
|-
 
|-
 
| 19:25
 
| 19:25
|Let us see how cmp works.We have two files named sample1 and sample2 in our home directory.
+
|Let us see how 'cmp' works. We have two files named 'sample1' and 'sample2' in our '''home''' directory.
  
 
|-
 
|-
Line 512: Line 503:
 
|-
 
|-
 
| 19:38
 
| 19:38
|Type cat sample1 and press enter.It contains
+
|Type '''cat space sample1''' and press Enter. It contains the text “This is a Linux file to test the cmp command”
the text “This is a Linux file to test the cmp command”
+
  
 
|-
 
|-
 
| 19:50
 
| 19:50
|The other file sample2 will contain the text and to see that we will type cat sample2 and press enter.
+
|The other file 'sample2' will contain the text and to see that we will type '''cat sample2''' and press Enter.
  
 
|-
 
|-
Line 525: Line 515:
 
|-
 
|-
 
| 20:06
 
| 20:06
|Now we would apply the cmp command on this two files.
+
|Now we would apply the '''cmp''' command on these two files.
  
 
|-
 
|-
 
| 20:11
 
| 20:11
|We will write cmp sample1 sample2 and press enter.
+
|We will write: cmp space sample1 space sample2 and press Enter.
  
 
|-
 
|-
 
| 20:23
 
| 20:23
|As we can see the first difference between the two files sample1 and sample2 is pointed out.  
+
|As we can see, the first difference between the two files 'sample1' and 'sample2' is pointed out.  
  
 
|-
 
|-
Line 541: Line 531:
 
|-
 
|-
 
| 20:38
 
| 20:38
|The next command we will see is the wc command.
+
|The next command we will see is the '''wc''' command.
  
 
|-
 
|-
 
| 20:43
 
| 20:43
|This command is used to count the number of characters, words and lines in a file.
+
|This command is used to ''count the number of characters, words and lines'' in a file.
  
 
|-
 
|-
 
| 20:50
 
| 20:50
|We have a file named sample3 in our home directory.
+
|We have a file named 'sample3' in our '''home''' directory.
  
 
|-
 
|-
 
| 20:56
 
| 20:56
|Let us see its content, for that we will type cat sample3 and press enter.
+
|Let us see its contents. For that, we will type: cat space sample3 and press Enter.
  
 
|-
 
|-
Line 561: Line 551:
 
|-
 
|-
 
| 21:10
 
| 21:10
|Now let us use the wc command on this file.
+
|Now let us use the '''wc''' command on this file.
 
+
  
 
|-
 
|-
 
| 21:14
 
| 21:14
|For that we would write wc sample3 and press enter.
+
|For that, we would write: wc space sample3 and press Enter.
  
 
|-
 
|-
Line 582: Line 571:
 
|-
 
|-
 
| 21:51
 
| 21:51
|I encourage you to see more about them using the man command.
+
|I encourage you to see more about them using the '''man''' command.
  
 
|-
 
|-

Latest revision as of 15:08, 28 May 2015

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:31 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:55 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 (DEST).
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 eg.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 space 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 th name of the file we want to copy) space /home/anirban/demo2 and press Enter.

03:31 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 name 'demo2'.
03:51 To see that the 'demo2' is there, type:

ls space /home/anirban and press Enter.

04:13 We scroll up as you can see here is 'demo2'.
04:19 Before moving ahead let us clear the screen.
04:25 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:35 Type $ cp space /home/anirban/arc/demo1 space /home/anirban/ and press Enter.
05:03 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:20 As before, to see the 'demo1' type:

ls /home/anirban and press Enter.

05:33 Here again we would scroll up and as you can see the 'demo1' file is there.
05:40 Again, before moving ahead let us clear the screen.
05:48 Another instance when we do not need to give the destination file name is when we want to copy multiple files.
05:56 We assume that we have three files named test1, test2, test3 in our home directory.
06:04 Now we type: $ cp space test1 space test2 space test3 space /home/anirban/testdir and press Enter.
06:27 This will copy all the three files named test1,test2 and test3 to the directory /home/anirban/testdir without changing their names.
06:41 To see that these files have actually been copied. We will type: ls space /home/anirban/testdir and press Enter.
07:03 As you can see test1, test2 and test3 are present in this directory.
07:10 There are many options that go with cp. Here we will see only the most important of them.
07:18 Let us first go back to the slides.
07:23 Among the options, -R is an important one. It causes recursive copying of an entire directory structure.
07:33 Let us see an example.
07:38 Let us try to copy all the contents of the 'testdir' directory to a directory called 'test'.
07:48 For that, we would type: cp space testdir/ space test and press Enter.
08:02 As you can see from the output message,
08:06 normally we cannot copy a directory having some content directly with cp command.
08:14 But using the -R option we can do this.
08:19 Now we type: cp space -R space testdir/ space test and press Enter.
08:36 The files have now been copied. To see that the 'test' directory actually exists, type ls and press Enter.
08:47 As you can see, the 'test' directory exists. Let us clear the screen.
08:57 To see the contents inside 'test', type: ls space test and press Enter.
09:08 You can see the contents of the 'test' directory.
09:13 Now we go back to the slides.
09:16 We have seen if a file is copied to another file that already exists, the existing file is overwritten.
09:25 Now what if we inadvertently overwrite an important file?
09:30 To prevent anything like this to occur, we have the -b option.
09:36 This makes a backup of each exiting destination file.
09:41 We can also use the -i (interactive)option, this always warns us before overwriting any destination file.
09:54 Now let us see how the mv command works.
09:59 This is used for moving files. Now how is that useful?
10:04 It has two major uses.
10:07 It is used for renaming a file or directory.
10:11 It also moves a group of files to a different directory.
10:17 mv is very similar to cp which we have already seen. So let us quickly see how mv can be used.
10:29 We open the terminal and type: $ mv space test1 space test2 and press Enter.
10:43 This will rename the file named 'test1' which was already present in the home directory to a file named 'test2'.
10:52 If 'test2' already existed then it would be overwritten silently.
11:00 If we want our warning before the file is overwritten,
11:05 we can use the -i option with the mv command.
11:10 Say we have another file named 'anirban'. This file we also want to renew as 'test2'.
11:20 We will type: mv space -i space anirban space test2 and press Enter.
11:32 As you can see, a warning is provided asking whether 'test2' should be overwritten or not.
11:41 If we press y and then press Enter, the file would be actually overwritten.
11:49 Like cp we can use mv with multiple files but in that case the destination should be a directory.
11:58 Before moving ahead let us clear the screen.
12:03 Suppose we have 3 files named abc.txt, pop.txt and push.txt in our home directory.
12:14 To see there presence, type ls and press Enter.
12:21 Here are the files pop.txt, push.txt and abc.txt Let us clear the screen.
12:36 Now we want to move these three files to a directory called 'testdir'.
12:46 What we need to do is type: mv space abc.txt space pop.txt space push.txt and then the name of the destination folder which is 'testdir' and press Enter.
13:14 To see them, type ls space testdir and press Enter.
13:20 You can see the files abc, pop and push.txt.
13:27 Now let us see some options that go with mv. Let us first go back to the slides.
13:37 Then -b or –backup option is present with the mv command. It will backup every file in the destination before it is overwritten.
13:48 The -i option that we have already seen warns us before overwriting any destination file.
13:58 The next command we will see is the rm command. This command is used for deleting files.
14:06 Go back to the terminal and type: ls space testdir
14:15 We can see a file name 'faq.txt' present. Say, we want to delete it.
14:23 For this, we type:

$ rm space testdir/faq.txt and press Enter.

14:37 This command will remove the file 'faq.txt' from the '/testdir' directory.
14:46 To see that the file has been actually removed or not, let us again press:ls space testdir and press Enter.
15:00 We can no longer see the file 'faq.txt'.
15:05 We can use the rm command with multiple files as well.
15:10 The 'testdir' directory contains two files 'abc2' and 'abc1'.
15:17 Suppose we want to remove these files abc1 and abc2.
15:23 For this, we would type: rm space testdir/abc1 space testdir/abc2 and press Enter.
15:45 This removes the files 'abc1' and 'abc2' from 'testdir' directory.
15:53 To see that they have been removed, type ls space testdir again. You can no longer see 'abc1' and 'abc2'.
16:07 Let us clear the screen before moving ahead.
16:14 Now let us go back to the slides.
16:18 Let us summarize what we just said?
16:20 That is, to delete a single file we write rm and then the name of the file.
16:27 To delete multiple files we write rm and the name of the multiple files that we want to delete.
16:34 Now let us look into some of the options of the rm command.
16:40 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:57 The other common option is the -r option. Let us see where this options are useful?
17:07 Let us switch back to the terminal.
17:12 rm command is not normally used for deleting directories, for that we have the rmdir command.
17:21 But rmdir command normally deletes a directory, only then it is empty.
17:27 What if we want to delete a directory that has a number of files and subdirectories inside.
17:35 Let us try the rm command to do this.
17:38 Let us type rm and the directory that we want to delete which is 'testdir' and press Enter.
17:47 From the output message we can see that we can not use the 'rm directory' to delete 'testdir'.
17:55 But if we combine the -r and -f option then we can do this.
18:03 Press: rm space -rf space testdir and then press Enter.
18:16 Now the 'testdir' directory has been successfully deleted.
18:22 Let us now go back to the slides to study the next command.
18:27 The cmp command.
18:29 Sometimes we need to check whether two files are same. If they are same then we may delete one of them.
18:37 Also we may want to see whether a file has changed since the last version.
18:44 For these and many other purposes we can use the cmp command.
18:49 It compares two files byte by byte.
18:54 To compare file1 and file2, we would write cmp space file1 space file2.
19:03 If the two files have exactly same content then no message would be shown.
19:11 Only the prompt will be printed.
19:14 If there are differences in their contents then the location of the first mismatch will be printed on the terminal.
19:25 Let us see how 'cmp' works. We have two files named 'sample1' and 'sample2' in our home directory.
19:35 Let us see what they contain?
19:38 Type cat space sample1 and press Enter. It contains the text “This is a Linux file to test the cmp command”
19:50 The other file 'sample2' will contain the text and to see that we will type cat sample2 and press Enter.
20:00 It will contain the text “This is a Unix file to test the cmp command.”
20:06 Now we would apply the cmp command on these two files.
20:11 We will write: cmp space sample1 space sample2 and press Enter.
20:23 As we can see, the first difference between the two files 'sample1' and 'sample2' is pointed out.
20:32 Let us clear the screen before moving ahead to the next command.
20:38 The next command we will see is the wc command.
20:43 This command is used to count the number of characters, words and lines in a file.
20:50 We have a file named 'sample3' in our home directory.
20:56 Let us see its contents. For that, we will type: cat space sample3 and press Enter.
21:05 This is the content of sample3.
21:10 Now let us use the wc command on this file.
21:14 For that, we would write: wc space sample3 and press Enter.
21:25 The command points out that the file has 6 lines, 67 words and 385 characters.
21:38 These were some of the commands that help us to work with files.
21:43 There are many more commands. Moreover each of the command that we saw has many other options.
21:51 I encourage you to see more about them using the man command.
22:00 This brings me to the end of this tutorial at last.
22:04 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:17 More information on the same is available at the following link http://spoken-tutorial.org/NMEICT-Intro.
22:34 This is Anirban signing off . Thanks for joining.

Contributors and Content Editors

Gaurav, Minal, Pratik kamble, Sandhya.np14, Vasudeva ahitanal