Super User is a question and answer site for computer enthusiasts and power users. 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'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
up vote 7 down vote accepted

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
1  
Very nice! I was able to use -f '- /*/' to restrict to top level files only. I also had to specify -f '- .*' to exclude dot files, since I was syncing a home directory. – haridsv Sep 11 '14 at 9:27

The solution above (by PleaseStand) didn't work for me for some reason. This worked though:

rsync -avc --no-r ./source/* ./destination/

There's also this alternative one:

rsync -avc --exclude "/" ./source/ ./destination/

share|improve this answer

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

You must log in to answer this question.

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