Understand "mv" command

denaba

Explorer
Joined
Jan 12, 2014
Messages
59
Not a problem, but more of a question on understand the mv command

I was moving a folder from one location to another using the shell command "mv" and through my Windows system I saw two folders at one point. Then later in the old location it was was gone and in the new folder the stuff was there.

So is it safe to say that when the "mv" command is initiated, TrueNAS first copies the folder and the contents and then sends all that to the new destination? So in essence there will be two of everything at some point? Then when the action is done TrueNAS then deletes the old folder (or file) location?

If this is true then at the moment TrueNAS is done, does TrueNAS check the integrity of the files at the new location automatically? And then once done checking it then deletes the "from" items?

I never thought of this until the other day when I moved a decent size folder with contents in it and since I was just sitting there (usually I mv then get something to eat or read something else on the Internet) I just watched it and first time I ever did. Made me just wonder like if I am doing a "mv" and if anything interrupts that then I could delete the new destination folder and files and then redo another "mv" because the source is still intact?

Anyways, just wondering and thank you. I look forward to understanding TrueNAS more and more
 
Last edited:

Kris Moore

SVP of Engineering
Administrator
Moderator
iXsystems
Joined
Nov 12, 2015
Messages
1,471
The "mv" command is part of the standard UNIX/Linux toolset. When moving across datasets, it creates a copy of the files at the new location first, and then removes them as part of the last step.

The filesystem is what handles the integrity tests (ZFS in this cse)

If you want something more advanced that moves and deletes files as it goes (not waiting for an entire directory) you may want to look at "rsync".
 

Ericloewe

Server Wrangler
Moderator
Joined
Feb 15, 2014
Messages
20,194
So is it safe to say that when the "mv" command is initiated, TrueNAS first copies the folder and the contents and then sends all that to the new destination? So in essence there will be two of everything at some point? Then when the action is done TrueNAS then deletes the old folder (or file) location?
POSIX is, as usual, somewhat hard to parse, but it does seem to state that this is the correct procedure (The mv utility shall not modify both source_file and the destination path simultaneously; termination at any point shall leave either source_file or the destination path complete. - note that source_file may be a directory).
When crossing filesystem boundaries, FreeBSD explicitly states:
Code:
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

GNU mv's online docs explicitly state that directories are either completely copied successfully or left as-is in the source path (i.e. partial copies are deleted):
It first uses some of the same code that’s used by cp -a to copy the requested directories and files, then (assuming the copy succeeded) it removes the originals. If the copy fails, then the part that was copied to the destination partition is removed. If you were to copy three directories from one partition to another and the copy of the first directory succeeded, but the second didn’t, the first would be left on the destination partition and the second and third would be left on the original partition.

This is effectively a feature for increased predictability of filesystem operations. It would be a major pain in the ass if a failed mv left half your stuff in one place and the other half in a different place.

Note that within a filesystem, mv is generally just a rename and happens with zero copying.
 

denaba

Explorer
Joined
Jan 12, 2014
Messages
59
OK, thank you for both of the replies. Definitely Ericloewe is way advanced for me, but I get it. And what you mentioned Kris is what I was thinking in terms of TrueNAS; file integrity first. I have seen Windows do that; during a move and there is an interruption. You get the "file is not complete" or "file cannot be read".

Thanks again everyone. Just really curious.
 
Top