LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Is `mv dir dir2` atomic ? (https://www.linuxquestions.org/questions/linux-software-2/is-%60mv-dir-dir2%60-atomic-624077/)

meili100 02-26-2008 08:18 PM

Is `mv dir dir2` atomic ?
 
if I rename a dir
mv dir dir2

Is this operation atomic? Suppose there 100 files in dir, does linux rename them one by one or at once?

In other words, is there a time at which both dir and dir2 exist, with dir has, say 30 files and dir2 has the rest 70 files?

jailbait 02-26-2008 08:38 PM

Quote:

Originally Posted by meili100 (Post 3070934)
if I rename a dir
mv dir dir2

Is this operation atomic? Suppose there 100 files in dir, does linux rename them one by one or at once?

In other words, is there a time at which both dir and dir2 exist, with dir has, say 30 files and dir2 has the rest 70 files?

No. The only thing that mv renames is the directory name. It does so in place i.e. the file system inode containing the directory name is updated in place. The file inodes are not renamed, moved, or even looked at.

----------------------
Steve Stites

PatrickNew 02-26-2008 08:39 PM

I'm not certain, but I think that there are no truly atomic file system operations - or at least no guarantees. I think the details are specific to each file system implementation.

BrianK 02-26-2008 08:39 PM

Quote:

Originally Posted by meili100 (Post 3070934)
if I rename a dir
mv dir dir2

Is this operation atomic? Suppose there 100 files in dir, does linux rename them one by one or at once?

In other words, is there a time at which both dir and dir2 exist, with dir has, say 30 files and dir2 has the rest 70 files?

there can be a million files in a directory. Renaming the directory has nothing to do with the files it contains. It happens in a single operation. I don't know if it's technically atomic in that it takes only one CPU cycle, but the OS locks the dir (which is actually just a file, but that's a topic for another discussion) during the change so it acts like it is [atomic]

edit: lots of simultaneous replies. hehehe

meili100 02-26-2008 08:49 PM

Thanks all.
Can I get an official link or resource for "mv is actually change the dir name" and "OS locks the dir duing the change"? I am writing a tech document of them. Thanks.

Quote:

Originally Posted by BrianK (Post 3070959)
there can be a million files in a directory. Renaming the directory has nothing to do with the files it contains. It happens in a single operation. I don't know if it's technically atomic in that it takes only one CPU cycle, but the OS locks the dir (which is actually just a file, but that's a topic for another discussion) during the change so it acts like it is [atomic]

edit: lots of simultaneous replies. hehehe


jailbait 02-27-2008 12:18 PM

Quote:

Originally Posted by meili100 (Post 3070964)

Can I get an official link or resource for "mv is actually change the dir name" and "OS locks the dir duing the change"? I am writing a tech document of them.

The most likely source that I know of for that information is the two Oreilly books, "Running Linux" and "Understanding the Linux Kernel".


http://www.oreilly.com/pub/topic/linux


----------------------
Steve Stites

meili100 02-27-2008 12:36 PM

But I think mv belongs to GNU coreutils, which is not linux kernel.
Correct me if I am wrong.

Quote:

Originally Posted by jailbait (Post 3071729)
The most likely source that I know of for that information is the two Oreilly books, "Running Linux" and "Understanding the Linux Kernel".


http://www.oreilly.com/pub/topic/linux


----------------------
Steve Stites


jailbait 02-27-2008 12:51 PM

Quote:

Originally Posted by meili100 (Post 3071745)
But I think mv belongs to GNU coreutils, which is not linux kernel.
Correct me if I am wrong.

True. That answer is probably in "Running Linux". It's been years since I read these books but they are probably where I learned the information.


---------------------
Steve Stites

chrism01 02-27-2008 05:44 PM

Look at it this way, mv is a synonym for rename (in this case); ie it doesn't 'move' anything, it just 'renames' it.

PatrickNew 02-27-2008 06:24 PM

I agree that the "final" answer to your question would come from reading up on the rename system call. Ultimately, mv just wraps around that system call. The man page available here:
http://www.linuxmanpages.com/man2/rename.2.php
seems to suggest that it is atomic.

That said, we should clarify some things. Moving one directory to another may be atomic, but moving multiple things may not be. That is because the coreutils (or busybox, etc) mv just calls rename over and over. Each rename is atomic, but if you specify a number of files to move that might not be.

Summary:
mv dir dir2 - looks atomic
mv file1 file2 file3 file4 dir - probably not atomic

Hope that helps!


All times are GMT -5. The time now is 05:22 AM.