94

I have a directory that contains symbolic links to other directories located on different media on my system:

/opt/lun1/2011
/opt/lun1/2010
/opt/lun2/2009
/opt/lun2/2008
/opt/lun3/2007

But the symbolic links show up as:

/files/2011
/files/2010
/files/2009
/files/2008
/files/2007

How can I perform an rsync that follows the symbolic links?

e.g.:

rsync -XXX /files/ user@server:/files/

fduff
  • 115
  • 4
ensnare
  • 2,132
  • 6
  • 23
  • 39

3 Answers3

131

The -L flag to rsync will sync the contents of files or directories linked to, rather than the symbolic link.

MadHatter
  • 78,442
  • 20
  • 178
  • 229
  • 1
    When I do this, all the receiver symbolic links get deleted and my sender starts re-sending the files. – ensnare Mar 10 '11 at 16:40
  • Isn't that what you want? I thought you wanted to have the real files on the receiving side, not just the symlinks. Do you mean you want the symlinks copied as symlinks, but automagically rewritten to point to the "right" place on the receiving system? If so, that's not what is normally meant by "an rsync that follows the symbolic links". – MadHatter Mar 10 '11 at 17:10
  • 1
    It actually deletes the symbolic links and replaces them with the files. I want to follow the symbolic links through as if they were regular directories. – ensnare Mar 10 '11 at 17:53
  • Are these symbolic links to files, or to directories; your comment above suggests both? Could you paste an example of what you've got on the source side and what you would like to see on the destination side, in the original question? And by example, I mean some detail: `ls -al` output from the source side. – MadHatter Mar 10 '11 at 17:59
  • 5
    Hi -- these are directories. I got this to work with the -K flag. – ensnare Mar 10 '11 at 21:20
  • 6
    I'm sure SO is making me more stupid.. – John Hunt Nov 07 '14 at 11:53
  • 9
    To clarify, `-LK` if you want to follow both symlinked files and directories – Mahn Jul 14 '16 at 21:14
  • Imagine if rsync also had tar options. – sudo Mar 13 '18 at 18:50
  • 2
    Always be careful when following such suggestions. Test, test, and test before your carry out product run! *`-L` itself will remove all symlinks on your receiver side!* – HongboZhu Jul 05 '19 at 10:16
  • Just to tack on to this, I tested with my usual -avP and it looks like the -L needs to prepend my -a to copy the files and not the link itself, so -LavP for my purposes. – user8675309 May 25 '22 at 16:00
24

You need both -L and -K if you have symlinks on both sides, e.g. you already had your 1st rsync done and want to update the backup using rsync.

    -L, --copy-links            transform symlink into referent file/dir
    -K, --keep-dirlinks         treat symlinked dir on receiver as dir

In such cases, if you use only -L, the symlinks on the receiver side will be wiped out and new real dir will be made.

HongboZhu
  • 341
  • 2
  • 4
21

Just ran into this problem. And if you want rsync to treat symlinked directories as directories, you want the K option

rsync -K /files/ user@server:/files/

Jmeyering
  • 331
  • 2
  • 4