Linux 如何通过 rsync 仅复制符号链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9776387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy only symbolic links through rsync
提问by Newbiee
How do I copy symbolic links only (and not the file it points to) or other files using rsync?
如何使用 rsync 仅复制符号链接(而不是它指向的文件)或其他文件?
I tried
我试过
rsync -uvrl input_dir output_dir
rsync -uvrl input_dir output_dir
but I need to exclusively copy the symbolic links only ?
但我只需要专门复制符号链接?
any trick using include exclude options?
使用包含排除选项的任何技巧?
采纳答案by ghoti
Per this question+answer, you can script this as a pipe. Pipes are an integral part of shell programming and shell scripting.
根据这个问题+答案,您可以将其脚本化为管道。管道是 shell 编程和 shell 脚本的一个组成部分。
find /path/to/files -type l -print | \
rsync -av --files-from=- /path/to/files user@targethost:/path
What's going on here?
这里发生了什么?
The find
command starts at /path/to/files and steps recursively through everything "under" that point. The options to find
are conditions that limit what gets output by the -print
option. In this case, only things of -type l
(symbolic link, according to man find
) will be printed to find's "standard output".
该find
命令从 /path/to/files 开始,并递归地遍历该点“下”的所有内容。选项find
是限制-print
选项输出内容的条件。在这种情况下,只有-type l
(符号链接,根据man find
) 的东西才会被打印到 find 的“标准输出”。
These files become the "standard input" of the rsync
command's --file-from
option.
这些文件成为rsync
命令--file-from
选项的“标准输入” 。
Give it a shot. I haven't actually testedthis, but it seems to me that it should work.
试一试。我还没有实际测试过这个,但在我看来它应该可以工作。
回答by moul
You can generate a list of files excluding links with find input_dir -not -type l
, rsync has an option --exclude-from=exlude_file.txt
您可以生成不包括链接的文件列表find input_dir -not -type l
,rsync 有一个选项--exclude-from=exlude_file.txt
you can do it in two steps :
你可以分两步完成:
find input_dir -not -type l > /tmp/rsync-exclude.txt
find input_dir -not -type l > /tmp/rsync-exclude.txt
rsync -uvrl --exclude-from=/tmp/rsync-exclude.txt input_dir output_dir
rsync -uvrl --exclude-from=/tmp/rsync-exclude.txt input_dir output_dir
one line bash :
一行bash:
rsync -urvl --exclude-from=<(find input_dir -not -type l | sed 's/^..//') input_dir output_dir
rsync -urvl --exclude-from=<(find input_dir -not -type l | sed 's/^..//') input_dir output_dir
回答by Rehmat
You can do it more easily like:
您可以更轻松地做到这一点,例如:
find /path/to/dir/ -type l -exec rsync -avP {} ssh_server:/path/to/server/ \;
EDIT: If you want to copy symbolic links of the current directory only without making it recursive. You can do:
编辑:如果您只想复制当前目录的符号链接而不使其递归。你可以做:
find /path/to/dir/ -maxdepth 1 -type l -exec rsync -avP {} ssh_server:/path/to/server/ \;
回答by xerostomus
I prefer this:
我更喜欢这个:
find ./ -type l -print > /tmp/list_of_links.txt
rsync -av --files-from=/tmp/list_of_links.txt /path/to/files user@targethost:/path
The reason is simple. In the previous suggested version, I had to enter my password with every file. This way I can send all symlinks at once, with just one password entered.
原因很简单。在之前的建议版本中,我必须为每个文件输入密码。这样我就可以一次发送所有符号链接,只需要输入一个密码。