Doubidou
Doubidou

Reputation: 1781

rsync: How to apply a recursively include filter while excluding other files

I'm trying to rsync a folder, but only some specific contents matching pattern and exluding the remaining files. I tried many solutions, followed advices from #2503 and #11111793, but I can't achieve a simple (at least in my mind) copy :/

This is my folders tree :

/
  - A
  - B.html
  css/
    - .gitkeep
    - source.css
    - source.min.css
    - source.min.css.map
    sub/
      - source.css
      - source.min.css
      - source.min.css.map
      - ...
  js/
    - ... Same as CSS
  img/
    - image.png
    - sub/
      - image.png
      - ...

The goal is to rsync :

I tried so many things, like include /* and exclude *, etc.

An example :

rsync -zarv \
  --include="/*.html" \
  --include="/js/**/*/min.js" \
  --include="/js/**/*.min.js.map" \
  --include="/css/**/*.min.css" \
  --include="/css/**/*.min.css.map" \
  --include="/img/***" \
  --exclude="*" \
  --delete \
  ./ $to

The 3 stars helped me for the img/ folder and copy it with everything inside, including subfolders ; it didn't work with /img/* nor with /img/**/* (no files synced, not even the img folder itself).

I don't understand... what I don't understand -.-'

Could someone help me ?

Subsidiary question : the glob pattern /js/**/*.min.* doesn't seem to work, can we use wildcard only at start of paths ?

Thanks !

Upvotes: 1

Views: 561

Answers (1)

KamilCuk
KamilCuk

Reputation: 140880

Try first including folders themselves:

rsync -zarv \
  --include="/*.html" \
  --include="/js/" \
  --include="/js/**/" \
  --include="/js/**.min.js" \
  --include="/js/**.min.js" \
  --include="/js/**.min.js.map" \
  --include="/css/" \
  --include="/css/**/" \
  --include="/css/**.min.css" \
  --include="/css/**.min.css.map" \
  --include="/img/***" \
  --exclude="*" \
  --delete \
  ./ "$to"/

Upvotes: 1

Related Questions