How to Use rsync with -L/–copy-links Flag to Follow Symbolic Links


3 views

When dealing with directory structures containing symbolic links, rsync's default behavior can be frustrating. Consider this common scenario:

/files/
├── 2011 -> /opt/lun1/2011
├── 2010 -> /opt/lun1/2010
├── 2009 -> /opt/lun2/2009
├── 2008 -> /opt/lun2/2008
└── 2007 -> /opt/lun3/2007

By default, rsync will copy the symlinks as references rather than the actual directory contents.

To make rsync follow symbolic links and copy the actual files/directories they point to, use the -L or --copy-links option:

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

This command will:

  • Follow all symbolic links (-L)
  • Preserve permissions and timestamps (-a)
  • Show verbose output (-v)
  • Compress data during transfer (-z)

For more control over which links to follow:

# Follow only certain symlinks using --include/--exclude
rsync -avzL --include='201[0-9]' --exclude='*' /files/ user@server:/files/

# Combine with --delete for mirroring
rsync -avzL --delete /files/ user@server:/files/

# Preserve hard links (add -H)
rsync -avzLH /files/ user@server:/files/

When using -L, be aware that:

  • It may copy significantly more data than expected if symlinks point to large directories
  • The original symlink structure won't be preserved at the destination
  • Circular references could cause problems

If you encounter issues:

# Dry run first to see what would be transferred
rsync -avzLnL /files/ user@server:/files/

# Check symlink targets before syncing
find /files/ -type l -exec ls -l {} \;

Remember that rsync's behavior with symlinks can vary slightly between versions, so always test first.


When dealing with directory structures that contain symbolic links pointing to different storage locations, a standard rsync command might not behave as expected. Consider this setup:

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

These directories are symbolically linked under /files/:

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

By default, rsync copies symbolic links as-is rather than following them to copy the actual files. This becomes problematic when you want to transfer the actual content rather than just the link references.

The -L or --copy-links option tells rsync to follow symbolic links and copy the actual files they point to:

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

This command will:

  • Follow all symbolic links (-L)
  • Preserve permissions and timestamps (-a)
  • Show verbose output (-v)
  • Compress data during transfer (-z)

If you only want to follow links that point within the copied tree, use:

rsync -avz --copy-unsafe-links /files/ user@server:/files/

When using these options:

  • Be aware of potential infinite loops in symlink structures
  • Consider using --safe-links to reject unsafe symlinks
  • Test with --dry-run first to verify the behavior

Here's a comprehensive command that includes additional useful options:

rsync -avzL --progress --delete --exclude='*.tmp' /files/ user@server:/files/

This adds:

  • Progress display during transfer
  • Deletion of files on destination not present in source
  • Exclusion of temporary files