如果 Linuxmv
命令被中断会发生什么?比如说,我正在将整个目录移动到其他地方,并在移动时中断它。源目录是否仍然保持不变?
小智 68
如果移动同一文件系统上的目录,则只会将目录条目从文件系统中的一个位置移动到另一个位置。例如,mv /source/dir /target/dir
将删除dir
from的目录条目/source
并在/target
. 这是由一个原子系统调用(即,不可中断的)完成的。包含目录条目dir
以及目录本身的实际内容的 inode不受影响。
如果您将目录从一个文件系统移动到另一个文件系统,所有文件首先被复制到新文件系统,然后与原始文件系统取消链接。因此,如果您mv
在复制过程中中断,您可能会得到一些文件的两个副本——旧位置和新位置。
Sim*_*ter 38
GNU 实现迭代命令行上的参数,首先尝试重命名,如果失败,则递归复制,然后递归删除源。所以
mv a b c/
Run Code Online (Sandbox Code Playgroud)
将删除一个复制之前b,并且不会开始删除任何一个之前目的地复制完成。
请注意,这仅适用于 GNU 实现。
澄清:如果a是包含d和e的目录,而b是一个文件,则顺序为
use*_*878 12
您移动一个目录,中断移动,原始目录将保持不变:
$ mv a b/
Run Code Online (Sandbox Code Playgroud)
如果您移动多个目录,则每个目录在源或目标上都将完好无损,具体取决于您何时中断:
$ mv a b c/
Run Code Online (Sandbox Code Playgroud)
我是如何得到答案的:
$ mv --version
mv (GNU coreutils) 8.21
$ info mv
... 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.
Run Code Online (Sandbox Code Playgroud)
作为测试,我复制了一个大文件夹到一个NFS目录,中断,我的源大文件夹中的文件数量保持不变,部分内容留在NFS目录中。我用“find .-type f | wc -l”来验证。
看起来西蒙的答案是正确的。
小智 9
关于在文件系统之间移动,公认的答案绝对是错误的——这一事实已经为我省去了很多麻烦。移动包含子目录的目录时,在复制整个子目录之前,不会删除子目录中的任何文件。这是,顺便说一句。“逐个对象”的实际含义 - 子目录是一个对象(文件),因此在删除任何内容之前,必须通过目的地的完整副本来保留其完整性。所以西蒙的答案在我看来是正确的。
如果你想中断 mv 因为你想与终端断开连接,你可以将它发送到后台:
* press Ctrl+Z
# bg
# disown
Run Code Online (Sandbox Code Playgroud)