linuxcommand-linefilesystems

How does Linux's mv work internally?


I know in Linux, if we want to rename a folder from f_old to f_new, we can run:

mv f_old f_new

But this command actually confuses me. Does the system copy all stuff inside f_old, paste it to new folder f_new and delete f_old at last? Or does it just rename f_old to f_new?


Solution

  • Nothing gets copied or moved if the source and destination are on the same partition. The system simply updates the directory entries, which point to the same inodes, to reflect the new name. This is a quick operation since no data is physically moved.

    If the source and destination are on different partitions, then a physical copy of the folder and its contents is performed, followed by the deletion of the original folder.

    Docs about how inode and directory entries work