Welcome to this spoken tutorial on Working with Regular Files in Linux.
Files and directories together form the Linux File System.
In a previous tutorial, we have already seen how to work with directories. You can find the tutorial at this website.
In this tutorial, we will see how to handle regular files.
We have already seen in another tutorial how we can create a file using the 'cat' command. For details, please visit this website.
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 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).
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.
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 into another file called '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 already existed then it would be silently overwritten. To see the copied file, type: $ cat space test2 and press Enter.
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.
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'.
To see that the 'demo2' is there, type: ls space /home/anirban and press Enter.
We scroll up as you can see here is 'demo2'.
Before moving ahead let us clear the screen.
If you want the file to have the same name in the destination directory, you may not even mention the file name. For example
Type $ cp space /home/anirban/arc/demo1 space /home/anirban/ and press Enter.
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.
As before, to see the 'demo1' type: ls /home/anirban and press Enter.
Here again we would scroll up and as you can see the 'demo1' file is there.
Again, before moving ahead let us clear the screen.
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 files named test1, test2, test3 in our home directory.
Now we type: $ cp space test1 space test2 space test3 space /home/anirban/testdir and press Enter.
This will copy all the three files named test1,test2 and test3 to the directory /home/anirban/testdir without changing their names.
To see that these files have actually been copied. We will type: ls space /home/anirban/testdir and press Enter.
As you can see test1, test2 and test3 are present in this directory.
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 an 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 a directory called 'test'.
For that, we would type: cp space testdir/ space test and press Enter.
As you can see from the output message,
normally we cannot copy a directory having some content directly with cp command.
But using the -R option we can do this.
Now we type: cp space -R space testdir/ space test and press Enter.
The files have now been copied. To see that the 'test' directory actually exists, 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 space test and press Enter.
You can see the contents of the 'test' directory.
Now we go back to the slides.
We have seen 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 have 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.
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 renaming 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.
We open the terminal and type: $ mv space test1 space 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 space -i space anirban space 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.
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 these three files to a directory called 'testdir'.
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.
To see them, type ls space 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.
Then -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.
The next command we will see is the rm command. This command is used for deleting files.
Go back to the terminal and type: ls space testdir
We can see a file name 'faq.txt' present. Say, we want to delete it.
For this, we type: $ rm space testdir/faq.txt and press Enter.
This command will remove the file 'faq.txt' from the '/testdir' directory.
To see that the file has been actually removed or not, let us again press:ls space testdir and press Enter.
We can no longer see the file 'faq.txt'.
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 these files abc1 and abc2.
For this, we would type: rm space testdir/abc1 space testdir/abc2 and press Enter.
This removes the files 'abc1' and 'abc2' from 'testdir' directory.
To see that they have been removed, type ls space testdir again. You can no longer see 'abc1' and 'abc2'.
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, to delete a single file we write rm and then 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 space -rf space 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.
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 space file1 space file2.
If the two files have exactly same content then no message would be shown.
Only the prompt will be printed.
If there are differences in their contents then the location of the first mismatch will be printed on the terminal.
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 space 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 would apply the cmp command on these two files.
We will write: cmp space sample1 space 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.
The next command we will see is the wc command.
This command is used to count the number of characters, words and lines in a file.
We have a file named 'sample3' in our home directory.
Let us see its contents. For that, we will type: cat space 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 space sample3 and press Enter.
The command points out that the file has 6 lines, 67 words and 385 characters.
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.
This brings me to the end of this tutorial at last.
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.
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.