In our earlier inode articles, we discussed about the inode basics and inode relations with the soft links and hard links and their differences. In this, we will discuss about the changes in inodes during the move(mv) of a file or directory.
1. Does the mv command rename a file or move a file?
It actually does both, depending on the context. If the source and target are both files, it does rename. If the source is a file and the target is a directory, it does move. However, once we understand the internal working of the mv command with respect to inodes, we will realize both are same.
2. Does moving a file to a directory using mv command create a new file and copy contents to it OR just move the file itself?
It depends on whether the target location is in the same filesystem or a different file system. If within the same file system, it is simply a move, else the mv command creates a new file and copies the contents to it from the old file and deletes the old file.
3. Why does Unix create and copy when the target directory belongs to a different file system?
In case, if the file is simply moved to the target directory as is, it may so happen that the target file system alread has a file with the same inode number as that of our source file. And hence create a new file and copy contents.
4. What is wrong if a file already exists in the target FS with the same inode number?
No 2 files can have the same inode number within a file system(except hard linked files).
5. When I move a file to a different directory, does the inum still remain the same?
Yes and No. As long as the directory being moved to is within the same file system, the inode number remains the same. Inode number changes on moving the file to a different file system.
a) Moved to a directory in same filesystem:
$ ls -li file 9962605 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file $ mv file bin/file0 $ ls -li bin/file0 9962605 -rw-r--r-- 1 guru users 98 Jul 6 15:11 bin/file0b) Moved to a directory in a different filesystem:
$ ls -li file1 9962607 -rw-r--r-- 1 guru users 19 Jul 5 17:37 file1 $ mv file1 /tmp $ ls -li /tmp/file1 6161903 -rw-r--r-- 1 guru users 19 Jul 5 17:37 /tmp/file1
6. In case of renaming a file from say f1 to f2, does the inode number change?
No. The inode number does not change when a file is simply renamed.
$ ls -li file 9962606 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file $ mv file file0 $ ls -li file0 9962606 -rw-r--r-- 1 guru users 98 Jul 6 15:11 file0
7. Say, within a directory, if a file is renamed, what exactly changes in the inode information of the source file?
Nothing changes in the inode information of the source file. This is because only the file name changes as part of the rename, and the file name is not part of the inode information at all. The mapping of the inode number with the file names happen at the parent directory level. In this mapping, the file name is updated against the inode number.
8. Is the directory also is associated with an inode number?
Yes, the directory is also a kind of file, a special file(Everything is a file in Unix, they say). One of the attributes of inode info is the type of file which tells whether it is a file, directory, socket, link, etc. The directory also has an inode number, and all the inode related information such as the permissions, size, owner, the group, etc.. are stored in the inode of the directory.
9. Is there any difference between the inode info contained for a file and a directory?
One of the data presented in the inode is the pointer to the data blocks which is the pointer to the location containing the data belonging to the file(stat command). In case of a directory, the data in the data blocks will contain the mapping of the file names to its inode numbers, which is typically the info present in a directory.
Let us consider a directory "xyz". The inode number of "xyz" is 113
$ ls -lid xyz 113 drwxr-xr-x 4 root root 1024 Jun 27 12:15 xyz/A typical directory mapping structure of "xyz" will look like this:
As seen, the directory information contains the first 2 entries : one for the current directory(.), and one for the parent directory(..). Followed by these 2 will be an entry for each and every file present under the "xyz" directory. Note: The directory mapping information does not contain its own name though.
10. Where does the directory inode number gets mapped with the directory name?
At the parent directory. As shown above, every directory maintains a table which contains the list of files and directories present under the directory. And hence in the parent directory of the current directory, the name of the current directory will be found just like how we can find the name of the sub-directory of "xyz" from the above table. Hence, Unix will read the inode number of the parent directory from its directory tree, and go to the directory mapping of the mapping directory, and compares the inode numbers one by one. One of them will match, and it can get the name of its directory from there.
The mapping of the parent directory of "xyz" directory: