Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    What happens to destination inode if it already exists in mv command

    I am interested in knowing what happens in the following case.
    file1 exists as /home/file1.txt on disk
    file2 exists as /home/file2.txt on disk
    file1 and file2 both have inode's. when the following command issued,
    #mv /home/file1.txt /home/file2.txt
    The rename operation happens on the same filesystem and on the same directory, but what will happen to file2's inode? and what are file system operations?

    I googled but I couldn't fine anywhere.
    I am trying to understand the code. Please help in understanding "what happens to destination inode when mv command issued on the same filesystem and same directory?".

    Thanks in advance.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Harrow, UK
    A move on the same filesystem doesn't actually move anything. So file1 stays in the same place and its inode is not affected. All that happens is that file1's entry in /home is modified to show the new name file2.

    But two files of the same name cannot coexist in the same directory so the original file2 must go. I think what happens is that the old entry for file2 in /home is removed using the unlink() function. This means that the file's inode is no longer accessible to users, so it and the blocks it points to are marked for recycling. Effectively the file is deleted.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Hi Hazel,
    Thanks for your reply.
    I understand the same that the file2 will get delete. While debugging the code I didn't get the unlink() operation. But somehow the file got deleted. I am interested to know how the file2 got deleted without unlink().
    PS: The mv is applied on the same filesystem and the same directory.

  4. $spacer_open
  5. #4
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    London, England
    I'm not sure what it is you're having trouble understanding. The data in an inode contains everything about the file except for its name, so when you mv file1.txt to file2.txt, file2.txt is deleted and file1.txt gets the new name file2.txt, but it still has all the same inode information.
    root@webserver:~# ls -i file1.txt
    254023 file1.txt
    root@webserver:~# ls -i file2.txt
    254019 file2.txt
    Note that file1.txt has the inode identifier 254023.
    root@webserver:~# mv file1.txt file2.txt
    root@webserver:~# ls -i file2.txt
    254023 file2.txt

  6. #5
    Thanks Krendoshazin,

    I understand the same what you said . Your point of view is from user side. But my point of view is from programming side.
    So, my question was "what will happen to file2's inode and how it will be deleted?". If you understand my question, for instance what happens to 254019 (file2's inode from the above example)? Yes, I understand finally file2's inode will be deleted. But how and who will delete file2's data and it's inode without unlink() operation?

    Now it is clear, below is the explanation.
    At the end of rename operation vfs_rename() function calls dput(file2_dentry). This dput() deletes the inode and data of file2. Problem solved.

    It's so interesting to learn kernel programming.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts