0

I am trying to copy a folder and excluding two subdirectories, but rsync keeps copying the directories in the exclude.

rsync -savzPh -e "ssh -p 237"  --include '*/' --include '*.pdf' --include '*.PDF' --exclude '*' --exclude 'a1' --exclude 'my archive2' user@ip:"/home/path/my folder" .

Source looks like this:

  • /home/path/my folder/folder1
  • /home/path/my folder/folder2
  • /home/path/my folder/a1
  • /home/path/my folder/a2
  • /home/path/my folder/my archive
  • /home/path/my folder/my archive2

The pdf files in folders in exclude argument are still are getting copied.

Do you have any idea what I am doing wrong?

1 Answer 1

2

Rsync will parse the include/exclude rules based on first matching. In your case the include rules match the "excluded" folders ("a1" & "my archive 2") before it gets a chance to parse the "exclude" rule.

So in this case you need to order the include/exclude flags differently.

If you want to copy the entire contents of "my folder" excluding the "a1" and "my archive2" sub-directories then this should be your command:

rsync -savzPh -e "ssh -p 237" --exclude 'a1' --exclude 'my archive2' user@ip:"/home/path/my folder" .

If you want only the PDF files from that directory structure (excluding "a1" and "my archive2") then try the following structure:

rsync -savzPh -e "ssh -p 237"  --exclude 'a1' --exclude 'my archive2' --include '*.pdf' --include '*.PDF' --include '*/' --exclude '*' user@ip:"/home/path/my folder" .

Note that in the 2nd version you first exclude the folders, thus skipping those, then you include the PDF extensions and sub-dirs (otherwise it will skip all sub-dirs due to the last rule) and finally you exclude everything (else)

Try to think of the "include" rule as "what not to exclude" but in a sort-of reverse order.

Tested with rsync 3.2.3

1
  • Testing with a different order worked. Thanks Commented Dec 12, 2021 at 15:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.