30

trying to rsync files of certain extension(*.sh), but the bash script below still transfer all the files, why?

from=/home/xxx
rsync -zvr --include="*.sh" $from/*  root@$host:/home/tmp/
user121196
  • 30,032
  • 57
  • 148
  • 198

4 Answers4

26

You need to add a --exclude all and it has to come after the --include

rsync -zvr --include="*.sh" --exclude="*" $from/*  root@$host:/home/tmp/
JohnQ
  • 684
  • 6
  • 9
  • Good point about the recursion, @that. On OS X, it appears that the exclude(s) have to follow the include(s). I would have thought that **find . -name '\*.sh' | rsync -zvr --include-from=- --exclude='*' ...** would work, but it doesn't. – JohnQ Feb 01 '13 at 22:46
  • 7
    As noted the answer from @that is better, as you need the --include="*/" to include subdirectories – Joe Watkins May 02 '13 at 16:37
  • 4
    I tried this and it transfers no data. Also tried using exclude before and after my include statements and it produces the same result. – Caleb Faruki Feb 20 '16 at 18:50
24

--include is for which files you want to not --exclude. Since you haven't excluded any in future arguments, there's no effect. You can do:

from=/home/xxx
rsync -zvr --include="*.sh" --include="*/" --exclude="*" "$from" root@$host:/home/tmp/

To recursively copy all .sh files (the extra --include to not skip directories that could contain .sh files)

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • 8
    I think the exclude is in the wrong place; it should be after the includes. Rsync acts on the first matching pattern. – Hipponax43 Jul 31 '14 at 12:55
  • 1
    Hipponax43 is right: could you correct your answer, because me and surely other users have been having a bad time trying to use your wrong answer – Bob Mar 11 '17 at 02:17
  • You may add **--prune-empty-dirs** to skip directories that are empty (not matching any files). – Jaber Dec 17 '21 at 09:20
7

On thing to add, at least on my machines (FreeBSD and OS X), this does not work:

rsync -aP --include=*/ --include=*.txt --exclude=* * /path/to/dest

but this does:

rsync -aP --include=*/ --include=*.txt --exclude=* . /path/to/dest

Yes, wildcarding the current directory seem to override the exclude.

Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71
lbutlr
  • 414
  • 6
  • 18
6

I fixed the problem changing --exclude=* by --exclude=*.*, I understand that * exclude folder where include files are.

estevo
  • 941
  • 11
  • 11