How to copy only symbolic links through rsync
Asked Answered
P

4

13

How do I copy symbolic links only (and not the file it points to) or other files using rsync?

I tried

rsync -uvrl input_dir output_dir

but I need to exclusively copy the symbolic links only ?

any trick using include exclude options?

Pippa answered 19/3, 2012 at 19:13 Comment(0)
V
9

Per this question+answer, you can script this as a pipe. Pipes are an integral part of shell programming and shell scripting.

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".

These files become the "standard input" of the rsync command's --file-from option.

Give it a shot. I haven't actually tested this, but it seems to me that it should work.

Vaticide answered 19/3, 2012 at 19:26 Comment(3)
This won't copy the symlinks as symlinks, though.Jaella
@Jaella ... Well, -a translates to -rlptgoD, and -l is documented to, "when symlinks are encountered, recreate the symlink on the destination." What results did you encounter?Vaticide
Oh! I missed that -a includes -l!Jaella
M
6

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

you can do it in two steps :

find input_dir -not -type l > /tmp/rsync-exclude.txt

rsync -uvrl --exclude-from=/tmp/rsync-exclude.txt input_dir output_dir

one line bash :

rsync -urvl --exclude-from=<(find input_dir -not -type l | sed 's/^..//') input_dir output_dir

Mungovan answered 19/3, 2012 at 19:29 Comment(0)
A
3

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/ \;
Arterialize answered 9/5, 2017 at 4:52 Comment(0)
L
1

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.

Lovesome answered 28/5, 2018 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.