从文件中读取rsync源会导致使用空格不正确地解析文件名

时间:2015-06-17 18:09:31

标签: bash shell unix scripting rsync

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

问题 名称中包含空格的目录不是rsync。空格导致空格之前的所有内容以及将空格作为单独的参数传递,从而导致无效的文件名。

观察当我检查临时文件的内容时,每个目录都按预期显示在一行上。它似乎只有当它从文件

读入rsync时

如何防止目录名称中的空格阻止这些目录无法进行rsync?

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

3 个答案:

答案 0 :(得分:1)

您需要rsync端的-0选项和find的-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.

答案 1 :(得分:1)

替换

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

答案 2 :(得分:1)

如果您不需要tmp文件,也可以使用“一行”命令:

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

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