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'm trying to copy the contents of a series of directories non-recursively to another remote system.

/dirA/dir1/file
/dirA/dir2/file
/dirA/dir3/file

dir1, dir2, and dir3 contain many directories that I do not want copied. Copy on the remote host to /dirB maintaining the same directory structure.

I tried:

rsync /dirA/*/ host:/dirB/
rsync /dirA/   host:/dirB/

But they don't do what I want.

share|improve this question
    
Are the nested subdirectories known or random. If random, that will complicate the complexity of the response. –  killermist Jun 13 '12 at 2:35
    
The directories are known, but may change, at any time, without notice. –  Felipe Alvarez Jun 14 '12 at 2:45
add comment

2 Answers

rsync allows you to specify patterns that trigger the inclusion or exclusion of files and directories. I think you want to use something like this:

rsync -a -f '- /*/*/' /dirA/ host:/dirB/

Explanation:

  • -a triggers the archive mode that activates both recursion and preservation of "symbolic links, devices, attributes, permissions, ownerships, etc.", according to man rsync.
  • -f is short for --filter=, which adds a file-filtering rule.
    • The pattern is inside single quotes so that the shell does not expand wildcards; double quotes would work equally well in this case.
    • - means this is an exclude pattern.
    • The leading / means the pattern must start at dirA/ (the rsync "transfer-root").
    • The */* part of the pattern refers to anything inside of a subdirectory.
    • The trailing / limits the exclusion to directories. Files inside a subdirectory of dirA/ are not affected.

So in the end, rsync copies nothing more than one level down (and also does not create second-level directories).

share|improve this answer
    
This solution is vastly better organized than the thought I was in mid-process of thinking-out. It also makes me think that there should be a "rsync-genius" kind of realm/site where rsync experts can supply their expertise to those looking for custom rsync commands. –  killermist Jun 13 '12 at 3:11
add comment

One of the "features" of rsync is how directories are parsed.

rsync /dirA/ host:/dirB/ and rsync /dirA host:/dirB should theoretically be equivalent.

As not wanting to encourage recursiveness, you want to avoid -r, -a which in addition to other things implies -r.

I'm still not quite done with research, but this is my starting answer.

share|improve this answer
add comment

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.