Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following filestructure in my src directory:

en/a/file1 -> ../../en/mp3/file1-longer-name-a
en/a/file2 -> ../../en/mp3/file2-longer-name-a

I am interested to copy all the files under en/a/ to the destination, preserving symlinks, and also include the referent files (which BTW are all within en/) but not other files under en/mp3/ if they are not referred by any symlinks I am transferring.

I'm using the following command, that does the obvious part of the job:

rsync -avRz "${SRC}./en/a" "${DST}"

this transfers the following files:

en/a/file1 -> ../../en/mp3/file1-longer-name-a
en/a/file2 -> ../../en/mp3/file2-longer-name-a

and I hope you'll help me to tweak the rsync args so that the following files will be transferred too:


is there a way to achieve this without needing to write an algorythm to loop over the softlinks under en/a/ and transfer the files they point to one-by-one?

share|improve this question
This doesn't exactly answer your question, but you might have a look at rsync's --copy-links or -L flag. Instead of copying the symlinks it resolves them and copies the files. But that's not a good idea if you've got many links to one file. – mreithub May 6 '13 at 8:53
I already tried that, but I need the symlinks to stay symlinks in the en/a/ directory – Gavriel May 6 '13 at 9:03
up vote 0 down vote accepted

You could use find in combination with rsync's --files-from parameter:

find "${SRC}./en/a" -type l -exec readlink {} ';'|sort|uniq|rsync -avRz --files-from=- "${SRC}" "${DST}"
  • find lists all symlinks in the en/ folder and executes readlink on each of them.
  • then we sort the list and filter out duplicates (with sort and uniq)
  • and rsync gets the --files-from param set to - to read the files it should sync from stdin
share|improve this answer
OK, it worked, the only thing I had to change was: rsync -avRz --files-from=- "${SRC}" "${DST}" (i.e: removed "./en/a", because the files that find gave me already had that) – Gavriel May 6 '13 at 10:25
Fixed the answer. – mreithub May 6 '13 at 10:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.