A walkthrough of the process from detainment to deportation

Butler County Correctional Complex is one of only five places in Ohio where undocumented immigrants are detained, and any undocumented person who is arrested south of I-70 is sent there. “We [the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




MV vs CP

What is the difference between a mv and cp in linux. I use it all the time and never knew what is happening behind the scenes. I decided to understand learn by experiments as theory is always obscure. This is article assumes the reader has basic C knowledge.

When we move a file the inode is removed from the existing directory and mapped to the new directory. The inode number of the file is not changed. Except in few cases which we will discuss later

When we copy a file a new file is created and the contents will be copied to the file. So the existing file will have the same inode number the new file will have different inode number.

Lets see how this works with programs which will give a better understanding as it did for me.

Before we go to the experiments one important point to note, in EXT file system each file has a inode number. You will be able to get the inode number by

The above program creates a file filled with 1. Similarly to create a file called twos.txt filled with 2.

What we do in the program is randomly index in to the file and print the number.

Now lets jump into the experiment. create a two files ones.txt and twos.txt with 1 and 2 filed respectively. keep the ones.txt in the current folder and move the twos.txt file into a sub-folder. I have put down the steps to do the same.

The directory hierechy will look like this

Now lets see their inode numbers. The inode numbers will mostly be different in your system.

You will see the program continously print the values form ramdom position of the file. (It values will all be 1). The printed format is below

The number between the [] are the index position of the file.

We are moving the file twos on the directory as ones.txt. So the program should start printing twos now. Does it?

Now lets see the inode numbers.

The inode number for the file did not change when we moved froms twos.txt to ones.txt. So old ones.txt file has the same file and it is still in the file system. It will not be deleted till the program stops. There are ways to print the filename and content using debugfs. I will cover it in a different chapter.

If you stop the program and run again, you will see that the program now prints 2.

Now lets check how cp works. Let get back the same test environment as Experiment one.

Now we see the program immediately start printing 2s. Let us see the inode number of the files.

The old ones.txt inode number is retained it is not changed to the twos.txt. So there we go

In this experiment lets have a similer test environment as both experiments as Experiment one and two, except the sub folder will be a different file system. We will create a new file system and mount is to the sub folder. Don’t worry I will explain the commands and explain them. As before create two files ones.txt and twos.txt. and create a subfolder sub, but do not move the twos into sub folder.

Now lets create a small filesystem of 10MB

The dd command creates the space for the filesystem . The mkfs.ext creates create the ext3 file system on the small.fs. Now lets mount the filesystem onto the subfolder sub. Then we move twos.txt file into that

The chown is required otherwise you will have to use sudo for each operation on the sub folder. The folder hirrachy will look like this.

It looks very same, but remember that sub is file system. Lets look at the inode number.

I think you have guessed why the inode number for twos.txt is small. It is because it is a new file system and there are no files in the file system(there could be hidden file system related files).

While the program execute mv the file twos.txt into the folder as ones.txt. Lets see what happens.

It still continues to print 1s, but if you look at the inode numbers.

where did this inode come from? it is not supposed to be 12 as per experiment 1? That is what happens when we mv between filesystems. It cannot do a direct mapping of inode number because the inode 12 is only for the small.fs file system. The same 12 could point to some other file in out host filesystem . So the system does a copy to the file system and then maps the copies file inode number to the directory.

When we copy a file, the contents inside the old file is written with the content from the new file. The inode number of the old file is the same. Whereas when we move the file the inode of the moved file will be mapped to the directory. No copying is done. When we move between file systems. First a copy of file is created in the other file system and then the inode number of the new file is mapped to the directory.

Add a comment

Related posts:

The Worst Saleswoman In The World

I picked up the scarf to make sure it was what I wanted — soft, lightweight, penguins, perfect! — and brought it up to the counter. She gestured toward the top of the shelf where there were ceramic…