2

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 ...

1
  • 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, 2012 at 13:57

2 Answers 2

3

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.

2
  • 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. Apr 24, 2012 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. Apr 24, 2012 at 14:08
1

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.

1
  • 1
    I take it back, the other answer filters "everything except"
    – Paul
    Apr 24, 2012 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .