Rsync - copy symlinks as directories, but skip if they point to file

11,480

Rsync has a range of options for dealing with links (from the man page):

    -l, --links                 copy symlinks as symlinks
    -L, --copy-links            transform symlink into referent file/dir
        --copy-unsafe-links     only "unsafe" symlinks are transformed
        --safe-links            ignore symlinks that point outside the tree
        --munge-links           munge symlinks to make them safer
    -k, --copy-dirlinks         transform symlink to dir into referent dir
    -K, --keep-dirlinks         treat symlinked dir on receiver as dir

If it so happens that the top-level links are "unsafe" and the other links are all "safe", then --copy-unsafe-links will Just Work, for you.

Otherwise, I'd recommend that you use a loop to sync the top-level directories one at a time:

rsync -a --delete /source/link1/ /target/link1/

Note that -a implies --links, so any links inside the directories will be copied as links, verbatim. The top-level link1 will be treated as a directory if you use the trailing / to dereference it (i.e. it's outside the tree rsync is scanning).

Share:
11,480

Related videos on Youtube

Zvezda
Author by

Zvezda

Updated on June 04, 2022

Comments

  • Zvezda
    Zvezda almost 2 years

    I have the following structure:

    /source/link1/->/dir1
    /source/link2/->/dir2
    /source/link1/file2 
    /source/link1/link11->/dir2/file21
    
    link=symbolic link, file=real file.
    

    At the target (remote) directory links in the "root" of source folder should be copied as directories, and links under /source/link1, /source/link2 folders should not be copied, or they should be copied only as links (to avoid duplicate structure). Target structure is the following:

    /target/link1 - directory
    /target/link2 - directory
    /target/link1/link11->/target/link2/file21
    /target/link1/file2
    

    How to maintain that with rsync? It is needed for the daily sync - every day target and source folder should be synced, and rsync should be used with --delete command.