How does the mv command work with external drives? The 2019 Stack Overflow Developer Survey Results Are InHow can the 'mail' command be used?How can I remove the Mac recovery partition from an external drive?How do I write-protect a partition of a USB drive on OSX that can work cross-platform?Fixing my external hard drive's partition mapExternal hard drive : Problems were found with the partition mapIs it possible or likely for malware or a virus to be transferred by plugging a USB hard drive into my Mac?can i use migration assistant to migrate files to a secondary drive?Mounting an external hard disk clone that shares the same partition GUIDs as the source/host hard diskMake Time Machine use two drives from TerminalWhat is the most efficient and reliable method for transfering/copying a large number of files from one external hard drive to another?
What is the grammatical structure of "Il est de formation classique"?
Command for nulifying spaces
What is the most efficient way to store a numeric range?
Cooking pasta in a water boiler
Falsification in Math vs Science
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
Why doesn't mkfifo with a mode of 1755 grant read permissions and sticky bit to the user?
Mathematics of imaging the black hole
Landlord wants to switch my lease to a "Land contract" to "get back at the city"
How to type this arrow in math mode?
Does a dangling wire really electrocute me if I'm standing in water?
Are there any other methods to apply to solving simultaneous equations?
How do I free up internal storage if I don't have any apps downloaded?
Can we generate random numbers using irrational numbers like π and e?
Is it okay to consider publishing in my first year of PhD?
The phrase "to the numbers born"?
If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?
How to check whether the reindex working or not in Magento?
What is the meaning of Triage in Cybersec world?
For what reasons would an animal species NOT cross a *horizontal* land bridge?
How much of the clove should I use when using big garlic heads?
Button changing its text & action. Good or terrible?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Why are there uneven bright areas in this photo of black hole?
How does the mv command work with external drives?
The 2019 Stack Overflow Developer Survey Results Are InHow can the 'mail' command be used?How can I remove the Mac recovery partition from an external drive?How do I write-protect a partition of a USB drive on OSX that can work cross-platform?Fixing my external hard drive's partition mapExternal hard drive : Problems were found with the partition mapIs it possible or likely for malware or a virus to be transferred by plugging a USB hard drive into my Mac?can i use migration assistant to migrate files to a secondary drive?Mounting an external hard disk clone that shares the same partition GUIDs as the source/host hard diskMake Time Machine use two drives from TerminalWhat is the most efficient and reliable method for transfering/copying a large number of files from one external hard drive to another?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
On the same drive I assume it would change something like a FAT table to repoint to the location of files.
Question
When going to a different drive (maybe even partition) does the mv
command first copy, then remove the old files - in order to prevent data loss if an exception occured ?
macos command-line external-disk
add a comment |
On the same drive I assume it would change something like a FAT table to repoint to the location of files.
Question
When going to a different drive (maybe even partition) does the mv
command first copy, then remove the old files - in order to prevent data loss if an exception occured ?
macos command-line external-disk
2
Note thatmv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes arename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIXrename(2)
man page for how simple it is.
– Peter Cordes
Mar 30 at 0:12
add a comment |
On the same drive I assume it would change something like a FAT table to repoint to the location of files.
Question
When going to a different drive (maybe even partition) does the mv
command first copy, then remove the old files - in order to prevent data loss if an exception occured ?
macos command-line external-disk
On the same drive I assume it would change something like a FAT table to repoint to the location of files.
Question
When going to a different drive (maybe even partition) does the mv
command first copy, then remove the old files - in order to prevent data loss if an exception occured ?
macos command-line external-disk
macos command-line external-disk
asked Mar 29 at 16:29
JacksonkrJacksonkr
18218
18218
2
Note thatmv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes arename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIXrename(2)
man page for how simple it is.
– Peter Cordes
Mar 30 at 0:12
add a comment |
2
Note thatmv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes arename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIXrename(2)
man page for how simple it is.
– Peter Cordes
Mar 30 at 0:12
2
2
Note that
mv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes a rename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIX rename(2)
man page for how simple it is.– Peter Cordes
Mar 30 at 0:12
Note that
mv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes a rename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIX rename(2)
man page for how simple it is.– Peter Cordes
Mar 30 at 0:12
add a comment |
2 Answers
2
active
oldest
votes
macOS's mv is based on the BSD source code. You can find the source code to the mv command online. Using https://github.com/freebsd/freebsd/blob/master/bin/mv/mv.c as a reference, you can see that they do indeed first try to rename the file and then if it is crossing filesystems, it does a cp followed by a rm.
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
It looks like the code snippet given is the most relevant part. It is preceeded byif (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.
– Mike Hill
Mar 29 at 21:21
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
|
show 1 more comment
Yes, you're right in thinking that moving a single file on the same file system is really just implemented as a rename operating that the file system structure is changed to update the new name/location of the file, but the file contents are not read/written to the drive again.
When the move happens across two different file systems (drives or partitions), then the mv
commands first deletes the destination (if there were an old file there already), copies over the contents of the file to the destination and then finally removes the source file.
The behavior is explained in the manual for mv on macOS:
As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to accomplish the move. The effect is equivalent to:
rm -f destination_path &&
cp -pRP source_file destination &&
rm -rf source_file
In regards to the other answer that compares this behavior with the FreeBSD source code - the mv command on macOS is actually a bit different than on FreeBSD. In particular it makes sure that extended attributes and resource forks are moved over correctly and do not disappear when moving across file system boundaries.
You can read the actual macOS source for mv
.
You'll see that it is similar in structure as the FreeBSD version, but contains various Apple specific enhancement. In addition to the functionality regarding extended attributes and resource forks as described above, it also has performance enhancements for use with Xsan (distributed file system).
You'll find in the code that first a rename is attempted:
if (!rename(from, to))
if (vflg)
printf("%s -> %sn", from, to);
return (0);
If this rename()
fails, the code checks why it failed. Especially it checks for the error number EXDEV, which means that the rename would have crossed file systems, and thus cannot be done:
if (errno == EXDEV)
struct statfs sfs;
char path[PATH_MAX];
/* Can't mv(1) a mount point. */
if (realpath(from, path) == NULL)
warnx("cannot resolve %s: %s", from, path);
return (1);
if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname))
warnx("cannot rename a mount point");
return (1);
else
warn("rename %s to %s", from, to);
return (1);
Note here that this code aborts the move in case that the source contains unresolvable symbolic links, or if it is actually a mount point - and also generally if the rename()
fails for other reasons than EXDEV.
Only in case that rename()
fails with error number EXDEV, and not for the above mentioned reasons, the following code is run:
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
This code branches out to do the move between file systems in two different ways depending on whether or not the source to be moved is actually a regular file - or it is something else. "Something else" is usually a directory, a symbolic link, a device node or similar.
In case of a regular file, it uses fastcopy()
which simply opens the source and destination files, read()
s the data from the source and write()
s them to the destination. Unlike the FreeBSD version, the fastcopy()
function uses fcopyfile()
to copy over ACLs and extended attributes from the source to the destination.
In case of something that is not a regular file, it simply spawns external commands to perform the move: cp
for copying and rm
for deleting.
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device movesmv
does first copy and then delete the source file.
– Mike Hill
Mar 29 at 21:32
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
macOS's mv is based on the BSD source code. You can find the source code to the mv command online. Using https://github.com/freebsd/freebsd/blob/master/bin/mv/mv.c as a reference, you can see that they do indeed first try to rename the file and then if it is crossing filesystems, it does a cp followed by a rm.
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
It looks like the code snippet given is the most relevant part. It is preceeded byif (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.
– Mike Hill
Mar 29 at 21:21
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
|
show 1 more comment
macOS's mv is based on the BSD source code. You can find the source code to the mv command online. Using https://github.com/freebsd/freebsd/blob/master/bin/mv/mv.c as a reference, you can see that they do indeed first try to rename the file and then if it is crossing filesystems, it does a cp followed by a rm.
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
It looks like the code snippet given is the most relevant part. It is preceeded byif (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.
– Mike Hill
Mar 29 at 21:21
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
|
show 1 more comment
macOS's mv is based on the BSD source code. You can find the source code to the mv command online. Using https://github.com/freebsd/freebsd/blob/master/bin/mv/mv.c as a reference, you can see that they do indeed first try to rename the file and then if it is crossing filesystems, it does a cp followed by a rm.
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
macOS's mv is based on the BSD source code. You can find the source code to the mv command online. Using https://github.com/freebsd/freebsd/blob/master/bin/mv/mv.c as a reference, you can see that they do indeed first try to rename the file and then if it is crossing filesystems, it does a cp followed by a rm.
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
}
edited Mar 29 at 23:12
user3439894
28.8k64665
28.8k64665
answered Mar 29 at 16:37
ParanoidGeekParanoidGeek
32115
32115
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
It looks like the code snippet given is the most relevant part. It is preceeded byif (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.
– Mike Hill
Mar 29 at 21:21
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
|
show 1 more comment
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
It looks like the code snippet given is the most relevant part. It is preceeded byif (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.
– Mike Hill
Mar 29 at 21:21
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
1
1
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
The source code snippet you link to actually do not have something to do with the detecting a cross-filesystem move or actually doing a cp followed by a rm. It is only the comment that describes this behavior - the actual code for it is really before the comment. The code after the comment already assumes that it is a cross-device move, and determines whether the source is a regular file to be copied internally, or something else that is moved using calls to the external programs cp and mv.
– jksoegaard
Mar 29 at 17:11
2
2
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
@jksoegaard - Correct. I was highlighting the section of code from the linked source code that directly addressed the question being posed.
– ParanoidGeek
Mar 29 at 18:16
2
2
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
Well, the comment addresses the question. The code does not, as the relevant code is before the comment :-)
– jksoegaard
Mar 29 at 20:01
2
2
It looks like the code snippet given is the most relevant part. It is preceeded by
if (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.– Mike Hill
Mar 29 at 21:21
It looks like the code snippet given is the most relevant part. It is preceeded by
if (!rename(from, to)) ... return (0); if (errno == EXDIV) ... else ... return (1);
, which, if I'm reading it right, will exit in all scenarios except for those in which the rename fails due to cross-device links. The snippet given shows the methods used to copy in cross-device scenarios: A fast-copy (built into mv) if the source is a regular file, or execution of the cp binary followed by the rm binary otherwise.– Mike Hill
Mar 29 at 21:21
2
2
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
Yes, I wrote that explanation in my answer above - however the snippet is not alone relevant if you want to look at code. The question was (1) what happens if it is on the same drive (the code before this snippet), and (2) what happens if it is on a different drive (the code after this snippet inside the fastcopy() function for regular files and copy() otherwise). Without knowing what’s inside those functions, the snippet here doesn’t make much sense. You could be led to believe that fastcopy() does a rename(), which it doesn’t.
– jksoegaard
Mar 29 at 21:52
|
show 1 more comment
Yes, you're right in thinking that moving a single file on the same file system is really just implemented as a rename operating that the file system structure is changed to update the new name/location of the file, but the file contents are not read/written to the drive again.
When the move happens across two different file systems (drives or partitions), then the mv
commands first deletes the destination (if there were an old file there already), copies over the contents of the file to the destination and then finally removes the source file.
The behavior is explained in the manual for mv on macOS:
As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to accomplish the move. The effect is equivalent to:
rm -f destination_path &&
cp -pRP source_file destination &&
rm -rf source_file
In regards to the other answer that compares this behavior with the FreeBSD source code - the mv command on macOS is actually a bit different than on FreeBSD. In particular it makes sure that extended attributes and resource forks are moved over correctly and do not disappear when moving across file system boundaries.
You can read the actual macOS source for mv
.
You'll see that it is similar in structure as the FreeBSD version, but contains various Apple specific enhancement. In addition to the functionality regarding extended attributes and resource forks as described above, it also has performance enhancements for use with Xsan (distributed file system).
You'll find in the code that first a rename is attempted:
if (!rename(from, to))
if (vflg)
printf("%s -> %sn", from, to);
return (0);
If this rename()
fails, the code checks why it failed. Especially it checks for the error number EXDEV, which means that the rename would have crossed file systems, and thus cannot be done:
if (errno == EXDEV)
struct statfs sfs;
char path[PATH_MAX];
/* Can't mv(1) a mount point. */
if (realpath(from, path) == NULL)
warnx("cannot resolve %s: %s", from, path);
return (1);
if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname))
warnx("cannot rename a mount point");
return (1);
else
warn("rename %s to %s", from, to);
return (1);
Note here that this code aborts the move in case that the source contains unresolvable symbolic links, or if it is actually a mount point - and also generally if the rename()
fails for other reasons than EXDEV.
Only in case that rename()
fails with error number EXDEV, and not for the above mentioned reasons, the following code is run:
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
This code branches out to do the move between file systems in two different ways depending on whether or not the source to be moved is actually a regular file - or it is something else. "Something else" is usually a directory, a symbolic link, a device node or similar.
In case of a regular file, it uses fastcopy()
which simply opens the source and destination files, read()
s the data from the source and write()
s them to the destination. Unlike the FreeBSD version, the fastcopy()
function uses fcopyfile()
to copy over ACLs and extended attributes from the source to the destination.
In case of something that is not a regular file, it simply spawns external commands to perform the move: cp
for copying and rm
for deleting.
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device movesmv
does first copy and then delete the source file.
– Mike Hill
Mar 29 at 21:32
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
add a comment |
Yes, you're right in thinking that moving a single file on the same file system is really just implemented as a rename operating that the file system structure is changed to update the new name/location of the file, but the file contents are not read/written to the drive again.
When the move happens across two different file systems (drives or partitions), then the mv
commands first deletes the destination (if there were an old file there already), copies over the contents of the file to the destination and then finally removes the source file.
The behavior is explained in the manual for mv on macOS:
As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to accomplish the move. The effect is equivalent to:
rm -f destination_path &&
cp -pRP source_file destination &&
rm -rf source_file
In regards to the other answer that compares this behavior with the FreeBSD source code - the mv command on macOS is actually a bit different than on FreeBSD. In particular it makes sure that extended attributes and resource forks are moved over correctly and do not disappear when moving across file system boundaries.
You can read the actual macOS source for mv
.
You'll see that it is similar in structure as the FreeBSD version, but contains various Apple specific enhancement. In addition to the functionality regarding extended attributes and resource forks as described above, it also has performance enhancements for use with Xsan (distributed file system).
You'll find in the code that first a rename is attempted:
if (!rename(from, to))
if (vflg)
printf("%s -> %sn", from, to);
return (0);
If this rename()
fails, the code checks why it failed. Especially it checks for the error number EXDEV, which means that the rename would have crossed file systems, and thus cannot be done:
if (errno == EXDEV)
struct statfs sfs;
char path[PATH_MAX];
/* Can't mv(1) a mount point. */
if (realpath(from, path) == NULL)
warnx("cannot resolve %s: %s", from, path);
return (1);
if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname))
warnx("cannot rename a mount point");
return (1);
else
warn("rename %s to %s", from, to);
return (1);
Note here that this code aborts the move in case that the source contains unresolvable symbolic links, or if it is actually a mount point - and also generally if the rename()
fails for other reasons than EXDEV.
Only in case that rename()
fails with error number EXDEV, and not for the above mentioned reasons, the following code is run:
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
This code branches out to do the move between file systems in two different ways depending on whether or not the source to be moved is actually a regular file - or it is something else. "Something else" is usually a directory, a symbolic link, a device node or similar.
In case of a regular file, it uses fastcopy()
which simply opens the source and destination files, read()
s the data from the source and write()
s them to the destination. Unlike the FreeBSD version, the fastcopy()
function uses fcopyfile()
to copy over ACLs and extended attributes from the source to the destination.
In case of something that is not a regular file, it simply spawns external commands to perform the move: cp
for copying and rm
for deleting.
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device movesmv
does first copy and then delete the source file.
– Mike Hill
Mar 29 at 21:32
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
add a comment |
Yes, you're right in thinking that moving a single file on the same file system is really just implemented as a rename operating that the file system structure is changed to update the new name/location of the file, but the file contents are not read/written to the drive again.
When the move happens across two different file systems (drives or partitions), then the mv
commands first deletes the destination (if there were an old file there already), copies over the contents of the file to the destination and then finally removes the source file.
The behavior is explained in the manual for mv on macOS:
As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to accomplish the move. The effect is equivalent to:
rm -f destination_path &&
cp -pRP source_file destination &&
rm -rf source_file
In regards to the other answer that compares this behavior with the FreeBSD source code - the mv command on macOS is actually a bit different than on FreeBSD. In particular it makes sure that extended attributes and resource forks are moved over correctly and do not disappear when moving across file system boundaries.
You can read the actual macOS source for mv
.
You'll see that it is similar in structure as the FreeBSD version, but contains various Apple specific enhancement. In addition to the functionality regarding extended attributes and resource forks as described above, it also has performance enhancements for use with Xsan (distributed file system).
You'll find in the code that first a rename is attempted:
if (!rename(from, to))
if (vflg)
printf("%s -> %sn", from, to);
return (0);
If this rename()
fails, the code checks why it failed. Especially it checks for the error number EXDEV, which means that the rename would have crossed file systems, and thus cannot be done:
if (errno == EXDEV)
struct statfs sfs;
char path[PATH_MAX];
/* Can't mv(1) a mount point. */
if (realpath(from, path) == NULL)
warnx("cannot resolve %s: %s", from, path);
return (1);
if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname))
warnx("cannot rename a mount point");
return (1);
else
warn("rename %s to %s", from, to);
return (1);
Note here that this code aborts the move in case that the source contains unresolvable symbolic links, or if it is actually a mount point - and also generally if the rename()
fails for other reasons than EXDEV.
Only in case that rename()
fails with error number EXDEV, and not for the above mentioned reasons, the following code is run:
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
This code branches out to do the move between file systems in two different ways depending on whether or not the source to be moved is actually a regular file - or it is something else. "Something else" is usually a directory, a symbolic link, a device node or similar.
In case of a regular file, it uses fastcopy()
which simply opens the source and destination files, read()
s the data from the source and write()
s them to the destination. Unlike the FreeBSD version, the fastcopy()
function uses fcopyfile()
to copy over ACLs and extended attributes from the source to the destination.
In case of something that is not a regular file, it simply spawns external commands to perform the move: cp
for copying and rm
for deleting.
Yes, you're right in thinking that moving a single file on the same file system is really just implemented as a rename operating that the file system structure is changed to update the new name/location of the file, but the file contents are not read/written to the drive again.
When the move happens across two different file systems (drives or partitions), then the mv
commands first deletes the destination (if there were an old file there already), copies over the contents of the file to the destination and then finally removes the source file.
The behavior is explained in the manual for mv on macOS:
As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to accomplish the move. The effect is equivalent to:
rm -f destination_path &&
cp -pRP source_file destination &&
rm -rf source_file
In regards to the other answer that compares this behavior with the FreeBSD source code - the mv command on macOS is actually a bit different than on FreeBSD. In particular it makes sure that extended attributes and resource forks are moved over correctly and do not disappear when moving across file system boundaries.
You can read the actual macOS source for mv
.
You'll see that it is similar in structure as the FreeBSD version, but contains various Apple specific enhancement. In addition to the functionality regarding extended attributes and resource forks as described above, it also has performance enhancements for use with Xsan (distributed file system).
You'll find in the code that first a rename is attempted:
if (!rename(from, to))
if (vflg)
printf("%s -> %sn", from, to);
return (0);
If this rename()
fails, the code checks why it failed. Especially it checks for the error number EXDEV, which means that the rename would have crossed file systems, and thus cannot be done:
if (errno == EXDEV)
struct statfs sfs;
char path[PATH_MAX];
/* Can't mv(1) a mount point. */
if (realpath(from, path) == NULL)
warnx("cannot resolve %s: %s", from, path);
return (1);
if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname))
warnx("cannot rename a mount point");
return (1);
else
warn("rename %s to %s", from, to);
return (1);
Note here that this code aborts the move in case that the source contains unresolvable symbolic links, or if it is actually a mount point - and also generally if the rename()
fails for other reasons than EXDEV.
Only in case that rename()
fails with error number EXDEV, and not for the above mentioned reasons, the following code is run:
/*
* If rename fails because we're trying to cross devices, and
* it's a regular file, do the copy internally; otherwise, use
* cp and rm.
*/
if (lstat(from, &sb))
warn("%s", from);
return (1);
return (S_ISREG(sb.st_mode) ?
fastcopy(from, to, &sb) : copy(from, to));
This code branches out to do the move between file systems in two different ways depending on whether or not the source to be moved is actually a regular file - or it is something else. "Something else" is usually a directory, a symbolic link, a device node or similar.
In case of a regular file, it uses fastcopy()
which simply opens the source and destination files, read()
s the data from the source and write()
s them to the destination. Unlike the FreeBSD version, the fastcopy()
function uses fcopyfile()
to copy over ACLs and extended attributes from the source to the destination.
In case of something that is not a regular file, it simply spawns external commands to perform the move: cp
for copying and rm
for deleting.
edited Mar 30 at 8:35
Melebius
1617
1617
answered Mar 29 at 16:38
jksoegaardjksoegaard
20.5k12150
20.5k12150
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device movesmv
does first copy and then delete the source file.
– Mike Hill
Mar 29 at 21:32
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
add a comment |
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device movesmv
does first copy and then delete the source file.
– Mike Hill
Mar 29 at 21:32
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device moves
mv
does first copy and then delete the source file.– Mike Hill
Mar 29 at 21:32
This is a more verbose but much more descriptive answer than the current top-rated answer. From your description, it sounds like the simple answer to OP's question is yes, when working with cross-device moves
mv
does first copy and then delete the source file.– Mike Hill
Mar 29 at 21:32
1
1
Correct! ......
– jksoegaard
Mar 29 at 21:47
Correct! ......
– jksoegaard
Mar 29 at 21:47
add a comment |
2
Note that
mv
itself doesn't know anything about filesystem internals like the FAT table on a FAT filesystem. As the answers point out, it merely makes arename()
system call, and leaves it to the kernel to return success or failure. The Unix/POSIX file API leaves all the VFS details to the kernel; the kernel itself is the only thing with drivers for HFS+, VFAT, NTFS, etc. See the POSIXrename(2)
man page for how simple it is.– Peter Cordes
Mar 30 at 0:12