Rsync copying directory contents non-recursively

rsync

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.

Best Answer

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

Related Question