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

Time:04-15

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 :

  • /*.html All HTML files at root (no other file types)
  • /css/**/*.min.css All built files into /css and subfolders recursively
  • /css/**/*.min.css.map All mapping files into /css and subfolders recursively
  • /js/**/*.min.js All built files into /js and subfolders recursively
  • /js/**/*.min.js.map All mapping files into /js and subfolders recursively
  • /img/**/* All files into /img and subfolders recursively

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 !

CodePudding user response:

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"/
  • Related