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

From Script | Spoken-Tutorial
Revision as of 12:51, 18 November 2014 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Working with regular files in Linux

Author: Anirban Roy Choudhury

Keywords: Files,directories,copy, move, remove,compare


Presentation and Attachments: File:Working with regular files attachment.zip

Visual Cue
Narration
Display Slide 1 Welcome to this spoken tutorial on working with regular files in Linux.
Display Slide 2

Hover the mouse over the link

Files and directories together form the Linux File System. In a previous spoken tutorial we have already seen how to work with directories. You can find the tutorial

http://spoken-tutorial.org/wiki/index.php/Linux_Spoken_Tutorial/Basic_Level_Tutorial_Set/File_System In this tutorial we will see how to handle regular files.

Display Slide 3


Hover the mouse over the link

We have already seen in another tutorial how we can create a file using cat command. For details please visit this website


Display Slide 4 Let us see how to copy a file from one place to another. For this we have the cp command.

Let us see how the command is used

To copy 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.

To copy multiple file at tha same time we write, cp space one or more of the options space the name of the source files that we want to copy and the name of the destination directory in which these file would be copy.


Open a terminal and type

cd /home/anirban/arc/

$ cat test1

$ cp test1 test2


Type

$ cat test2

Let us now see an Eg.

First we open a terminal We already have a file named test1 in our home directory. To see what is in test1 we type

$ cat space test1 and press Enter. As we can see the content of test1 is shown


Now if we want to copy it to another file call test2 we would write

cp space test1 space test2 and press enter. Now the file has been copied

If test2 doesn't exist it would be first created and then the content of test1 will be copied to it.

If it exists then it would be silently overwritten.To see the copied file type


cat space test2 and press Enter.



type

$ cp /home/anirban/arc/test1 /home/anirban/test2


type

$ ls /home/anirban

We can also copy files from and to different directories

For eg: 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.

What this will do is that it will copy the file demo1 from the source directory /home/anirban/arc/ to the destination directory /home/anirban. It will copy to a file whose name is demo2

To see whether the demo2 is there type ls space slash home slash anirban and press enter. As you can see here is demo2

Before moving ahead let us clear the screen

Type

$ cp /home/anirban/arc/test1 /home/anirban/


type

$ ls /home/anirban

Now if you want the file to have the same name in the destination directory, you may not even mention the file name.

For eg Type $ cp /home/anirban/arc/demo1space /home/anirban/ and press enter

This will again copy the file test1 present in the /home/anirban/arc/ directory to /home/anirban to a file whose name will be demo1 as well.

As before to see demo1 type ls slash home slash anirban and press enter. As you can see demo1 file is there. Moving ahead let us clear a screen.

Type

$ cp test1 test2 test3 /home/anirban/testdir

.

$ ls space /home/anirban/testdir

Another instance when we do not need to give the destination file name is when we want to copy multiple files.

We assume that we have three file namely test1, test2 and test3 in our home directory.

Now we Type $ cp test1 test2 test3 /home/anirban/testdir

This will copy all the three files test1,test2 and test3 to the directory /home/anirban/testdir without changing their names. To see that these files have truly been copied we will type type ls space

/home/anoirban/testdir and press enter. As you can see test1, test2 and test3 are present in this directory



Type $ cd ..



Type $ cp testdir/ test


Type $ cp -R testdir/ test


Type

$ ls

$ ls test

There are many options that go with cp. Here we will see only the most important of them.

Let us first go back to the slides. Among the options -R is a important one. It causes recursive copying of an entire directory structure. Let us see an example. Let us try to copy all the contents of the testdir directory to the directory called test. For that we would type cp space testdir/ and press enter. Normally we cannot copy a directory with files directly with cp.


But using the -R option we can do this. Now we type cp -R testdir/ test And press enter The files now have been copied.

To see whether the file direcory exist type ls and press enter.
As you can see the test directory exists. Let us clear the screen.

To see the contents inside test type ls test and press enter. You can see the contents of the test directory. Now we go back to the slides.

Display Slide 5 We have seen that if a file is copied to another file that already exists the existing file is overwritten.

Now what if we inadvertently overwrite an important file?

To prevent anything like this to occur, we can use the -b option. This makes a backup of each exiting destination file.

We can also use the -i(interactive)option, this always warns us before overwriting any destination file.


Display Slide 6 Now let us see how the mv command works. This is used for moving files. Now how is that useful?

It has two major uses.

It is used for rename a file or directory.

It also moves a group of files to a different directory.

mv is very similar to cp which we have already seen. So let us quickly see how mv can be used.

Open a terminal and

Type $ mv test1 test2

(Now again create test1)

Type $ mv -i test1 test2

We open the terminal and type $ mv test1 test2 and press enter.

This will rename the file named test1 which was already present in the home directory to a file named test2. If test2 already existed then it would be overwritten silently. If we want our warning before the file is overwritten.

We can use the -i option with the mv command. Say we have another file named anirban. This file we also want to renew as test2 We will type mv -i anirban test2 and press enter. As you can see a warning is provided asking whether test2 should be overwritten or not. If we press y and then press enter, the file would be actually overwritten.

Type

$ mv abc.txt pop.txt push.txt /home/anirban/testdir

Like cp we can use mv with multiple files but in that case the destination should be a directory.

Before moving ahead let us clear the screen. Suppose we have 3 files named abc.txt, pop.txt and push.txt in our home directory. To see there presence type ls and press enter. Here are the files pop.txt, push.txt and abc.txt Let us clear the screen. Now we want to move this three files to a directory called testdir. 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. To see them type ls testdir and press enter. You can see the files abc, pop and push.txt. Now let us see some options that go with mv. Let us first go back to the slides.

Display Slide 7 The -b or –backup option is present with the mv command.It will backup every file in the destination before it is overwritten.

The -i option that we have already seen warns us before overwriting any destination file.


Display Slide 8 The next command we will see is the rm command. This command is used for deleting files.
Open a terminal and type

$ ls testdir

$ rm testdir/faq.txt

$ ls testdir


$ ls

$ rm abc1 abc2

$ ls

Go back to the terminal and type ls testdir.

We can see a file name faq.txt present.Say we want to delete it. For this we type $ rm testdir/faq.txt and press enter.


This command will remove the faq.txt file from the /testdir directory. To see that the file has been actually removed or not.Let us again press ls testdir and press enter. We can no longer see the file faq.txt. We can use it with multiple files also.

We can use the rm command with multiple files as well. The testdir directory contains two files abc2 and abc1. Suppose we want to remove this files abc1 and abc2.

Display Slide 8


Switch back to terminal

$ ls

$ rm -rf testdir

$ ls

For this we would type rm testdir/abc1 testdir/abc2 and press enter.


This remove the files abc1 and abc2 from testdir directory.

Let us clear the screen before moving ahead. Now let us go back to the slides.

Let us summarize what we just said?

That is do delete a single file we write rm and than the name of the file. To delete multiple files we write rm and the name of the multiple files that we want to delete. Now let us look into some of the options of the rm command.

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.
The other common option is the -r option. Let us see where this options are useful? 

Let us switch back to the terminal.

rm command is not normally used for deleting directories, for that we have the rmdir command.

But rmdir command normally deletes a directory only then it is empty.

What if we want to delete a directory that has a number of files and subdirectories inside. Let us try the rm command to do this. Let us type rm and the directory that we want to delete which is testdir and press enter. From the output message we can see that we can not use the rm directory to delete testdir.

But if we combine the -r and -f option then we can do this.

Press rm -rf testdir and then press enter. Now the testdir directory has been successfully deleted. Let us now go back to the slides to study the next command. The cmp command.

Display Slide 9 Sometimes we need to check whether two files are same. If they are same then we may delete one of them. Also we may want to see whether a file has changed since the last version. For these and many other purposes we can use the cmp command.

It compares two files byte by byte. To compare file1 and file2 we would write cmp file1 file2.

If two files have exactly same content then there would be no message. Only the prompt will be printed. If there are differences then the location of the first mismatch will be printed on the terminal.



Type

cat > sample1

This is a Linux file to test the cmp command

[Ctrl d]


Type

cat > sample3

This is a Unix file to test the cmp command

[Ctrl d]


Type

$ cmp sample1 sample2

Let us see how cmp works.We have two files named sample1 and sample2 in our home directory.

Let us see what they contain? Type cat sample1 and press enter.It contains the text “This is a Linux file to test the cmp command”

The other file sample2 will contain the text and to see that we will type cat sample2 and press enter.


It will contain the text “This is a Unix file to test the cmp command.”




Now we apply the cmp command on these two files. We will write cmp sample1 sample2 and press enter.

As we can see the first difference between the two files sample1 and sample2 is pointed out.

Let us clear the screen before moving ahead to the next command. 
Type

$ cat sample3


$ wc sample3

The next command we will see is the wc command.

This command is used for counting the number of characters, words and lines in a file.

We have a file named sample3 in our home directory.


Let us see its content, for that we will type cat sample3 and press enter. This is the content of sample3. Now let us use the wc command on this file. For that we would write wc sample3 and press enter. The command points out that the file has 5 lines, 67 words and 385 characters.

Display Slide 10 These were some of the commands that help us to work with files. There are many more commands. Moreover each of the command that we saw has many other options. I encourage you to see more about them using the man command.The best way of learning commands is to use them again and again.


This brings me to the end of this tutorial. 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. This project is co-ordinated by http://spoken-tutorial.org.


More information on the same is available at the following link http://spoken-tutorial.org/NMEICT-Intro This is Anirban signing off . Thanks for joining.

Contributors and Content Editors

Nancyvarkey, PoojaMoolya