linux - 在 bash 脚本中使用 mv 丢失文件

标签 linux bash find mv

我有一个 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/

相关文章:

linux - 为Linux中的目录启用写权限

linux - 如何删除特定字符串之前的所有内容

linux - 如何查找多行内容的文件?

linux - 从 shell 脚本调用具有超时功能的 matlab

bash - 如何在 Dockerfile 中运行读取命令?

unix - 有没有办法覆盖 Unix 终端查找命令?

linux - 关于在 linux 中特定目录中的所有文件中搜索关键字

linux - Bash 管道和 SIGTERM

linux - CentOS 中的存储库是什么?

linux - 在 linux bash 脚本中恢复