Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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]
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
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]
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".
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.