2011-04-11

Recursive rsync, only specific file types

Sometimes you want to mirror a file tree, except you only want files matching a certain pattern (e.g. *.jpg). Rsync can do it, but the syntax isn't obvious. You need to tell rsync to include directories recursively, and to include the filetypes you want, and to ignore the rest. Here's an example:

rsync -Pav                                                  \
    --include '+ */' --include '*.jpg' --exclude '- *'      \
    /path/to/olddir /path/to/newdir

6 comments:

  1. Excellent, This solved a huge problem for me.

    Thanks a lot!!

    /AB from Sweden

    ReplyDelete
  2. You don't need the -pr in there. The -a (archive, and implies -rlptgoD ...... so a bit of duplication.

    Thanks for the --include!

    ReplyDelete
  3. I just needed this again. In revisiting the post, I went ahead and changed -pavr to -Pav, which I usually use now. Thanks for the tip, Anonymous(2).

    ReplyDelete
  4. Unfortunately not quite what I wanted, as it will still rsync a directory even though it does contain any matching files

    ReplyDelete
  5. Unfortunately not quite what I wanted, as it will still rsync a directory even though it doesn't contain any matching files

    ReplyDelete
  6. i need to copy only *.js to new folder, but, without folder tree.
    Ex:
    ab/
    ab/c/c.js
    ab/c/d/d.js
    ab/c/d/e/e.js

    to

    new/c.js
    new/d.js
    new/e.js

    some can help me ?

    ReplyDelete