Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
5  
About efficiency: no, that wouldn't be efficient. When you move data to another directory, the data is not actually read in one place and written in another. A file system contains a set of pointers to blocks of data. In the case of a move, just the pointers are updated. –  Jos Mar 13 at 10:00
    
Ah, that is probably what jeff meant... –  Jacob Vlijm Mar 13 at 10:25
    
@Jos Which is where fragmentation comes from right? –  zero298 Mar 13 at 13:24
3  
@zero298 No, fragmentation generally is a completely different and unrelated isse - fragmentation is not about contents of folder structure, but about the contents of a single file being scattered throughout the physical disc platters (instead of being a single continuous line), reducing read/write performance of such files. Contents of large/deep/branching folder trees aren't usually expected to be continuous and moved around that way. –  Peteris Mar 14 at 0:26
add comment

2 Answers 2

up vote 20 down vote accepted

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.

share|improve this answer
3  
Right, as long as the move doesn't cross filesystem boundaries. –  kojiro Mar 13 at 13:22
add comment

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

share|improve this answer
4  
you can get the most accurate explanation by reading through the code. I beg to differ; For a long time I had absolutely no experience with C, and even now I don't prefer it. Also, the source code is often optimized, and contains more edge-cases than needed. A symbolic explanation often helps more. –  shelvacu Mar 14 at 0:11
    
@shelvacu "contains more edge cases than needed". Robust code handles all the edge cases that may be present. However it's true that presenting all the edge cases in the first explanation of a concept may not be necessary. –  OregonTrail Mar 14 at 2:52
    
@OregonTrail Sorry that's what I meant, the code has more edge cases then are needs for an explanation –  shelvacu Mar 15 at 3:44
    
I'm glad that was your original sentiment, but you still seemed to have missed my emphasis of "handles". Well-written code doesn't have edge cases, it handles edge cases. ;) I know you're coming from the right place here, but you should be careful with your verbs. –  OregonTrail Mar 15 at 7:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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