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

From Script | Spoken-Tutorial
Revision as of 12:46, 17 June 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 see


http://spoken-tutorial.org/wiki/index.php/Linux_Spoken_Tutorial/Basic_Level_Tutorial_Set/General_Purpose_Utilities_in_Linux.

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

cp [OPTION]... SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

it copies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Open a terminal and type

cd /home/anirban/arc/

$ cat test1

$ cp test1 test2


Type

$ cat test2

Let us see an Eg.

We already have a file named test1 in /home/anirban/arc/

Open a terminal and type

$ cat test1


Now type

$ cp test1 test2

Here the file test1 present in the current directory is copied to another file test2 also in the current directory. 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 type


$ cat test2



type

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


type

$ ls /home/anirban

You can copy files from and to different directories as well.


type

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

This will copy the file test1 from /home/anirban/arc/ to /home/anirban with a new name test2

as we can see

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 it.


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

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



Type

$ cp test1 test2 test3 /home/anirban/testdir


type

$ ls /home/anirban/testdir

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


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.




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.

Among the options -R is a important one. It causes recursive copying of an entire directory structure.

Normally we cannot copy a directory with files directly with cp.


But using the -R option we can.


One interesting feature is that if test does not exist it would be created as a copy of testdir directory but if test already exists testdir would be created as a subdirectory of test.

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.

Rename a file or directory

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

Open a terminal and type $ mv test1 test2

This will rename the file named test1 as test2. If test2 already existed then it would be overwritten silently.

Using the -i option will again ask before overwriting an existing file.

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.


This will move the 3 files named abc, pop and push from the current directory to the directory /home/anirban/testdir

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



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

Open a terminal and type

$ rm /testdir/faq.txt

This command will remove the faq.txt file from the /testdir directory.

We can use it with multiple files also.


This will remove both the files abc1 and abc2

Display Slide 8


Switch back to terminal

$ ls

$ rm -rf testdir

$ ls

Sometimes a file is write protected,using rm will not delete the file then. In this case we need the -f option which is used to force delete such files.

The other very common option is the -r option. Now where is this useful.

rm command is not normally used for deleting files, for that we have the rmdir command.But rmdir normally deletes a directory when it is empty. What if we want to delete a directory having a number of files and subdirectories inside. We combine the -r and -f option. $ rm -rf testdir this will delete the directory testdir along with all its subdirectories and files recursively.

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.

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 open a terminal window and see how cmp works. We make two sample files named sample1 and sample2 using the cat command .


One contains the text “This is a Linux file to test the cmp command”


while the other contains the text “This is a Unix file to test the cmp command.”

Open the terminal and type

$ cmp sample1 sample2


Now we apply the cmp command on these two files.


We can see how the difference between the first difference between the files is pointed out.

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 created a file named sample3 that contains the following content


The command points out that the file has 5 lines, 67 words and 381 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

Chandrika, PoojaMoolya