4

I want to rsync from a folder, which contanins index.php (which need to be excluded), and many other directories contains index.php too. i created an exclude file and added index.php there. but all the index.php files were gone. Is there any proper method to avoid the only file from syncing?

9

rsync uses directories relative to the root rsync folder. You should be able to achieve this using something like

rsync -aAX ./ ../target --exclude=/index.php

The "/" will indicate the root directory.

4
  • 1
    I tried that too (./index.php) but, unfortunately, excluded all index.php Nov 17 '14 at 9:19
  • @RajeshRajendran I have updated my answer, this worked for me.
    – psiphi75
    Nov 17 '14 at 9:37
  • This doesn't work at all in my version of rsync. Jun 20 '16 at 6:49
  • apparently this only works if the source directory has a trailing '/'. i couldn't find a reference to this in the docs anywhere, i'm guessing it must be a bug.
    – Spongman
    Sep 29 '20 at 23:24
5

The --exclude filter works like this:

  • If the pattern does not start with a slash, then it matches at the end of the path, anywhere in the tree.

    --exclude myfile.txt matches any file called myfile.txt in any directory.

    --exclude logs/myfile.txt matches myfile.txt inside any directory called logs.

  • If the pattern begins with a slash, it is anchored to the root of the transferred files.

    To determine what the root of the transferred files is, remove everything after the last slash in the source path.

    For example, in the following rsync command, the source path is /srv/files/something and so the root of the transfer is /srv/files/.

    rsync -a /srv/files/something host:/backup/files/
    

    So, to exclude source file /srv/files/logs/myfile.txt you would use --exclude /logs/myfile.txt, where that slash at the start of the exclude pattern refers to the root of the transfer, which is rooted at /srv/files/ (on the source).

2
  • this is a really good explanation - I was unable to find this documented in my version of rsync man page. is there are officially documented source that explains this?
    – billynoah
    Apr 7 '18 at 0:32
  • 1
    It's explained under the subheadings "INCLUDE/EXCLUDE PATTERN RULES" and "ANCHORING INCLUDE/EXCLUDE PATTERNS" in the manpage (eg here or here). Apr 7 '18 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.