1

I’ve heard for a while that for the mv tool in Linux, moving a file is the same as renaming a file. Here is the comment that piqued my interest recently:

“On one file system renaming and moving is exactly the same thing; "moving" is just renaming the directory-entry's full path (i. e. the part stating its parent directories) instead of just the directory-entry's base name (the part after the last slash). It is done via the system call rename(2); tools like mv test whether using this is possible and only if it is not fall back to copy-and-delete.”

I want to know, how does renaming a file move the file? I thought that a file is moved by removing the entry in the old parent directory, and creating an entry in the new parent directory. I don’t see how this is related to renaming a file.

6
  • 1
    What's the difference between moving a file from /a/b/c to /a/b/x and moving a file from /a/b/c to /a/x/c? You could maybe add some optimizations, but those are basically the same thing
    – muru
    Mar 19 at 4:08
  • Sorry, I don’t fully understand your point. I’m thinking that moving a file to a new location would be different compared to renaming a file in place, because rather than modifying the existing parent directory entry you need to add an entry in the new parent directory.
    – Roy Yi
    Mar 19 at 4:52
  • When you create a new directory entry in the same filesystem for the file, the file has a new name. You can see this action as "moving" from one directory to the other, although you don't really move the file (i.e. the inode), you only give it a new name. It's like changing the names of a web site from www.example.com to www.example.org - the web site gets a new name, or you could say that the site "moved" to www.example.org, although it is still at the same server. Mar 19 at 5:28
  • Thanks, that makes the idea more clear. I know that the file’s inode is not modified when moving to a new location, but I suppose the process of making a new entry in the parent directory can be considered “renaming,” even when the new file name is the same as the original one. The main concern I have is that modifying the existing entry is not sufficient to move the file. The original directory entry needs to be deleted and a new directory entry needs to be created elsewhere, so that feels like a different process compared to renaming.
    – Roy Yi
    Mar 19 at 5:59
  • There's an optimisation you've made in that process: you're implicitly assuming that you could just change whatever part of the directory entry holds the name. After all, both "moving" and "renaming" could be done fine via adding an entry and then deleting the old entry (both operations need to possible for moving). Now the question is: what constraints do you need to impose to ensure that this optimization can be done? Is it necessary that all filesystems follow those constraints? What if I have a hash/map implementation in which the name is the key and not an attribute of directory entry?
    – muru
    Mar 19 at 7:41
1

I thought that a file is moved by removing the entry in the old parent directory, and creating an entry in the new parent directory. I don’t see how this is related to renaming a file.

Two words for the same process. In English, move normally implies the file has the same name, but is in a different directory, whereas rename implies it is in the same directory, and given a different name. The OS doesn't care about this distinction. Either way, one directory entry is removed, and another is added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.