How does linux handle a move command

How does linux handle a move command under the hood?

Let's say I move my home dir

/home/me

and I move this into another directory

/home/foo/me

How are all the files and directories paths under me changed? I know my Desktop dir under me is now /home/foo/me/Desktop as well as Documents /home/foo/me/Documents but does the file system explicitly update every path under me to to reflect the change? That doesn't sound very efficient and it's probably not this.

Where can I get more information on this?


To understand how it move folders you may need to understand a bit about file system under linux. Every files and folders are stored as part of a data structure called an "inode". Each file has an inode number, so does folders.

To view the inode of your folder, use the command ls -ial foldername. The first column shows the inode number of the file. For each folder there are two unique names . and .., representing the directory of its own, and the parent directory respectively.

You can try doing an experiment to move a directory (say, /home/me/source) with sub-directories and files to another directory (e.g. /home/me/somewhere/else). The inode number of /home/me/source and all its contents remains the same before and after moving. The only difference is the inode number of .., which originally shares the inode number of /home/me and now becomes the inode number of /home/me/somewhere/else. In simple wording, Linux update the link to directory source and then it's done.

The contents on the hard disk are not modified anyway, only the inode index is updated when the folder is moved. This is, of course, not the case if you move the folder to a different physical location.


If you're interested in how programs such as mv and cp work, remember that they're open source and you can get the most accurate explanation by reading through the code. Here has links to all the core utilities. Specifically, you can find mv here