我想在未知的目录结构上运行 rsync 并让它仅同步文件而不同步目录。
我尝试了很多方法,甚至包括管道查找,但没有成功。
示例源目录:
somedir/
file1.txt
file2.txt
anotherdir/
file3.txt
Run Code Online (Sandbox Code Playgroud)
示例目标目录:
file1.txt
file2.txt
file3.txt
Run Code Online (Sandbox Code Playgroud)
rsync您可以在非递归模式(不带-r)下与递归 shell glob 一起使用。这样,您可以将文件夹中的所有文件和子目录的列表作为参数提供给它,但它会忽略所有目录并仅传输文件。
注意:递归 glob ( **) 需要globstar在 Bash 中设置 shell 选项。为当前会话启用它,shopt -s globstar或者取消.bashrc文件中该行的注释以永久启用它。
$ tree .\n.\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 somedir\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 anotherdir\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file3.txt\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1.txt\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file2.txt\n\n$ rsync ** myserver:/tmp/rsynctest\nskipping directory somedir\nskipping directory anotherdir\n\n$ ssh myserver tree /tmp/rsynctest\n/tmp/rsynctest\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file2.txt\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file3.txt\nRun Code Online (Sandbox Code Playgroud)\n\n如果您想指定其他rsync暗示递归的参数(例如例如-a),您可以再次显式禁用递归--no-r。
递归 glob 也可以是更长路径或更复杂模式的一部分。只需确保包含您想要的所有文件,任何目录都将被忽略。
\n\n$ rsync -avz --no-r somewhere/local/**/*.txt myserver:/tmp/rsynctest/\nskipping directory .\nskipping directory somedir\nskipping directory anotherdir\nfile1.txt\nfile2.txt\nfile3.txt\n\nsent 231 bytes received 73 bytes 202.67 bytes/sec\ntotal size is 0 speedup is 0.00\nRun Code Online (Sandbox Code Playgroud)\n