MacOS – How does the mv command work with external drives

command lineexternal-diskmacos

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 ?

Best Answer

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));
}