Let's consider this text file:
Code:
[root@mahadeva ~]# ls -ild hello
1566924 -rw-r--r-- 1 root root 150 Oct 14 07:20 hello
I copy (cp) it with another name:
Code:
[root@mahadeva ~]# cp hello hello-copy
[root@mahadeva ~]# ls -ild hello*
1566924 -rw-r--r-- 1 root root 150 Oct 14 07:20 hello
1566966 -rw-r--r-- 1 root root 150 Oct 14 07:21 hello-copy
The inode value is changed now.
I move (mv) any of the two files and give it some other name:
Code:
[root@mahadeva ~]# mv hello-copy hell-moved
[root@mahadeva ~]# ls -lid he*
1566966 -rw-r--r-- 1 root root 150 Oct 14 07:21 hell-moved
1566924 -rw-r--r-- 1 root root 150 Oct 14 07:20 hello
The inode value is not changed even if I move the file into some other directory:
Code:
[root@mahadeva ~]# mv hello-moved /tmp
[root@mahadeva ~]# ls -lid /tmp/hello*
1566966 -rw-r--r-- 1 root root 150 Oct 14 07:21 /tmp/hello-moved
Well, let's suppose the file is very large, say 10 GB of disk space it is consuming.
We perform the above steps. Which operations: cp (copying) or mv (moving) will be more efficient and less time consuming?
The inode number
1566966 which was pointing to the file in my home directory is now pointing to the same file which is in some other directry, i.e.
/tmp/.
Isn't the inode value getting modified in some ways to point to the correct location / beginning of the data block on the hard disk? If the the physical location of the data would not change then we would not be able to free up space in our home directory. That means the
mv operation is copying the file into some other location. In other words, it is also performing the
cp operation first and then deleting the file from its original location, my home directory in the case above.
However, when a file is copied it's inode value changes. But the mv operation is retaining the same indoe value.
So, how to determine which operation - mv or cp - is more CPU-friendly or more efficient in terms of performance and time taken to perform the action?
Useless Note (You may not need to read this stuff if you are going to give your views): Those who may find the above discussion senseless should not blame me. I have been asked this question in an interview. Whatever I have stated above, I told the interviewer. But I was not sure of what subtlety he had in his mind when he asked me the question.