How mv command works in Linux ?

In this quick tip, we will look how mv command works on Linux systems and renames a file.

As we know mv renames a file in Linux.

Lets see the inside out of the command, what changes performed on our filesystem when a file is renamed, or moved to some other path.

1. Renaming the file at same location.

### Check the inode number of the file.
[root@nglinux ~]# ls -il file4
918295 -rw-r--r--. 1 root root 76 Jan 29 05:08 file4

### Now rename file4 to file00
[root@nglinux ~]# mv file4 file00

### Now check the file inode information again.
[root@nglinux ~]# ls -il file00
918295 -rw-r--r--. 1 root root 76 Jan 29 05:08 file00
[root@nglinux ~]# 

What you have observed ?
The inode number is same, the mv command simply changes the file name inside directory inode information.

2. Moving the file to different directory path on same partition.
Now lets move the file to some other path and check out if inode information is updated or not.

### Check the inode of the file.
[root@nglinux ~]# ls -il file00
918295 -rw-r--r--. 1 root root 76 Jan 29 05:08 file00

### Lets move the file to /tmp path.
[root@nglinux ~]# mv file00 /tmp/

### Check for inode information.
[root@nglinux ~]# ls -il /tmp/file00
918295 -rw-r--r--. 1 root root 76 Jan 29 05:08 /tmp/file00
[root@nglinux ~]# 

We can observe that inode number is still same.

3. Moving the file to different partition.
Inodes exists on one partition only, and hence if we move the file to any other partition than a new inode will be assigned to it.

Why changing directory path preserves inode ?
If we change directory path on same partition, mv command simply updates the directory inode information and adds the file inode information to it, hence there is no change in file inode information.

This is the reason if we move the file on same partition, it is very quick.

Now the common question is how to look directory inode information for /root and /tmp to check the inode updation, so for this, you need to dump the file data to disk using dumpe2fs and debugfs commands and analyze the dump to find the information.

I hope you liked the article.

Do pot your comments/suggestions below.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments