Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to copy all files "pom.xml" from a big tree to a new tree: On each level of the src tree I have this file. I want to get the same tree structure, but ignore empty directories.

sounds like a rsync one-liner but I don't get it ... :-/

thanks in advance ...

share|improve this question
    
Just a matter of --include and --exclude. "-m" strips off empty directories: rsync -avzm --include "*/" --include "pom.xml" --exclude "*" /path/to/src/tree /path/to/dest/tree –  Bastl Apr 24 '12 at 13:57

2 Answers 2

up vote 1 down vote accepted

You can do it by e.g.

rsync -rvm --include='*/' --filter='-! pom.xml' SOURCE DEST
  • -r recurses,
  • -v prints verbosely what happens,
  • -m prunes empty directories (dirs that don't contain anything after the transfer),
  • --include='*/' tells rsync to explicitly include the directories themselves which are otherwised pruned by the next filter (this is a caveat that I've been stuck on for shameful amount of times: that the directories explicitly needs to be allowed in cases like this),
  • --filter='-! pom.xml' excludes files that does not (that's what "!" does) match the pattern (this would include all directories as well without the --include pattern above).

As I said, I have been scratching my head several times over the behavior of directories in complex filter rules for rsync, and perhaps it is clear by the manual, but in that case I have been able to miss it for a long time, so a clarification would be appreciated.

share|improve this answer
    
And remember, the -n flag (dry run) is your friend. Whenever I am setting up a major rsync (like 20GB of video files), I always use -n before the run to see what will happen. –  Peter Rowell Apr 24 '12 at 14:04
    
@PeterRowell: True, I also follow that school. I have a script that syncs certain folders on my laptop with my stationary computer through rsync on every start up, but not before confirming the file list after a dry run. It has saved me a lot of unnecessary transfers. For completely automated jobs, it is of course not applicable, but even then, during the testing phase it is most convenient. –  Daniel Andersson Apr 24 '12 at 14:08

From the root folder of the tree:

find . -name pom.xml | rsync -ax --files-from=- . /destination/tree/

rsync cannot do an "exclude everything except", so we find the files first, then feed them into the files-from list via standard input (the -). The -a means archive, so does the recursion and retention of permissions. The -x means "don't traverse file systems", which I tend to include out of habit, and remove it when I am certain I want to traverse filesystems.

share|improve this answer
    
I take it back, the other answer filters "everything except" –  Paul Apr 24 '12 at 14:04

Your Answer

 
discard

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.