从文件中读取 rsync 源导致文件名解析不正确 space

Reading rsync source from file results in improper parsing of file names with white space

我编写了一个简单的脚本来搜索由变量 "SCOPE" 定义的特定目录,生成在过去 24 小时内修改过的目录列表,并将它们打印到一个临时文件中。文件的第一行被删除(以排除目录的根级别)。最后,它遍历临时文件的内容和 rsync 的每个目录到目标。

问题 名称中包含白色 space 的目录不会进行 rsync。 space 导致 whitespace 之前和 whitespace 之后的所有内容都作为单独的参数传递,因此文件名无效。

观察当我检查临时文件的内容时,每个目录都按预期出现在一行中。看来只有当它从文件

读入rsync时

如何防止目录名称中的白色space 阻止这些目录无法同步?

SCOPE="/base/directory"           
DESTINATION="/volumes/destination/"       

find "$SCOPE" -maxdepth 1 -type d -mtime 0 > /tmp/jobs.txt;

sed '1d' /tmp/jobs.txt > /tmp/tmpjobs.txt;                      
mv /tmp/tmpjobs.txt /tmp/jobs.txt;

for JOB in `cat /tmp/jobs.txt`; do
    rsync -avvuh "$JOB" "$DESTINATION";          
done

你想要 rsync 端的 -0 选项,以及查找的 -print0 选项。有很多实用程序对此有所不同,因此很容易修复!

来自 Linux 上的 find(1) 联机帮助页:

   -print0
          True; print the full file name on the standard output, followed by a null  character  (instead
          of  the  newline character that -print uses).  This allows file names that contain newlines or
          other types of white space to be correctly interpreted by programs that process the find  out-
          put.  This option corresponds to the -0 option of xargs.

替换

for JOB in `cat /tmp/jobs.txt`; do
    rsync -avvuh "$JOB" "$DESTINATION";          
done

来自

while read -r JOB; do
    rsync -avvuh "$JOB" "$DESTINATION"
done < /tmp/jobs.txt

如果你不需要tmp文件你也可以使用"one line"命令:

find "$SCOPE" -maxdepth 1 -mindepth 1 -type d -mtime 0 -exec rsync -avvuh {} "$DESTINATION" \;

-mindepth 1 # This handle sed
-exec       # This handle whole loop