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 have a file tree that looks as such:

├── file1
├── dir1
└── dir2
    ├── dir3
    │   ├──── file2
    │   └──── junk1
    ├── junk2
    └── junk3

I want to create a sibling of in which looks like the following:

├── file1
├── dir1
└── dir2
    └─── dir3
         └── file2

In other words, recursively copy in to out excluding in/dir2 but including in/dir2/dir3/file2.

I have tried the following:

rsync -a --exclude='in/dir2' --include='in/dir2/dir3/file2' in out

Which results in the following:

└── in
    └── file1

So I tried the following:

rsync -a --exclude='dir2' --include='dir2/dir3/file2' in/ out

Which results in:

└── file1

How can I achieve what I want with rsync or other unix tools? I would prefer to stay away from using weird methods like piping tar into tar...

share|improve this question
up vote 0 down vote accepted

What about breaking this up into more than one rsync command and bundle them into a shell script.

Another possible alternative is a combination of find with cpio to the destination device.

share|improve this answer
Not exactly what I wanted but I guess it works. – KPthunder Feb 12 '13 at 22:01

You can explore the --exclude-from=FILE and --include-from=FILE options with rsync.

        This  option is related to the --include option, but it specifies a FILE that contains include patterns
        (one per line).  Blank lines in the file and lines starting with ’;’ or ’#’ are ignored.  If FILE is -,
        the list will be read from standard input.
share|improve this answer

You must log in to answer this question.

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