LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-26-2008, 08:18 PM   #1
meili100
LQ Newbie
 
Registered: Oct 2007
Posts: 25

Rep: Reputation: 15
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?
 
Old 02-26-2008, 08:38 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by meili100 View Post
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
 
Old 02-26-2008, 08:39 PM   #3
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
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.
 
Old 02-26-2008, 08:39 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by meili100 View Post
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

Last edited by BrianK; 02-26-2008 at 08:41 PM.
 
Old 02-26-2008, 08:49 PM   #5
meili100
LQ Newbie
 
Registered: Oct 2007
Posts: 25

Original Poster
Rep: Reputation: 15
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 View Post
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
 
Old 02-27-2008, 12:18 PM   #6
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by meili100 View Post

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
 
Old 02-27-2008, 12:36 PM   #7
meili100
LQ Newbie
 
Registered: Oct 2007
Posts: 25

Original Poster
Rep: Reputation: 15
But I think mv belongs to GNU coreutils, which is not linux kernel.
Correct me if I am wrong.

Quote:
Originally Posted by jailbait View Post
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
 
Old 02-27-2008, 12:51 PM   #8
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
Quote:
Originally Posted by meili100 View Post
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
 
Old 02-27-2008, 05:44 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Look at it this way, mv is a synonym for rename (in this case); ie it doesn't 'move' anything, it just 'renames' it.
 
Old 02-27-2008, 06:24 PM   #10
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
Question on asm/atomic.h Lothar Schwab Programming 0 08-15-2007 07:13 PM
Atomic operation question PatrickNew Programming 4 05-12-2007 12:11 AM
bad: scheduling while atomic! ??? okeyla Linux - Newbie 1 09-26-2005 02:41 PM
make a file in dir1 appear to be in writeprotected dir2 magicpio Linux - Newbie 2 09-25-2005 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration