我有一个 bash 脚本,可以将大量文件从一个硬盘驱动器移动到另一个硬盘驱动器。 问题是有时脚本会在目标文件夹中生成很多空文件并删除原始文件夹中的文件(我怀疑当目标磁盘已满时会发生这种情况) 这基本上是我认为导致问题的代码行:
find /mnt/$server/ar/Folder01 -mtime +30 -exec mv -t /home/user/Folder01/$server/ {} \+
find /mnt/$server/ar/Folder02 -mtime +30 -exec mv -t /home/user/Folder02/$server/ {} \+
我无法使用 mv *,因为该文件夹有太多文件。
有什么想法或建议吗?
最佳答案
来自info coreutils mv
mv can move any type of file from one file system to another. Prior to version 4.0 of the fileutils, mv could move only regular files between file systems. For example, now mv can move an entire directory hierarchy including special device files from one partition to another. 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.
看来只有复制成功才会删除。
对于大小为 0 的文件,情况应该相反:您应该已经完成原始文件系统中的空间。 (您使用其文件名创建一个文件,但不能为其提供任何可自由写入的扇区)。
在开始复制之前进行同步可以在您尚未完成空间但系统正在等待将文件写入磁盘的情况下有所帮助(不应该需要,但您始终可以这样做...)
关于linux - 在 bash 脚本中使用 mv 丢失文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643181/