2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

rsync --exclude dirname、子階層のdirnameまで除外されるので注意

Last updated at Posted at 2021-08-17

経緯

  • rsyncの --include --exclude には癖があると過去の経験で感じつつも
  • 急いでスクリプトを書くと、配慮が行き届かず、
  • 思わぬ事故になる(なった)ので、簡単ではあるがメモを残しておく

環境

  • WSL (Ubuntu 20.04)
  • rsync version 3.1.3 protocol version 31

フォルダ構成

.
├── 01_rsync_test.bash
├── dir_tree.txt
├── dst
└── src
    ├── exclude_me
    │   └── exclude_me.txt
    └── sub
        └── exclude_me
            └── exclude_me.txt

テスト概要

#!/bin/bash

tree . > dir_tree.txt

rsync -avn src/ dst/ > test01.txt
rsync -avn --exclude exclude_me src/ dst/ > test02.txt
rsync -avn --exclude exclude_me/ src/ dst/ > test03.txt
rsync -avn --exclude ./exclude_me src/ dst/ > test04.txt
rsync -avn --exclude ./exclude_me/ src/ dst/ > test05.txt
rsync -avn --exclude src/exclude_me src/ dst/ > test11.txt
rsync -avn --exclude src/exclude_me/ src/ dst/ > test12.txt
rsync -avn --exclude ./src/exclude_me src/ dst/ > test13.txt
rsync -avn --exclude ./src/exclude_me/ src/ dst/ > test14.txt
rsync -avn --exclude /home/user/rsync/src/exclude_me/ src/ dst/ > test21.txt
rsync -avn --exclude /home/user/rsync/src/exclude_me/ src/ dst/ > test22.txt
rsync -avn --exclude sub/exclude_me src/ dst/ > test31.txt
rsync -avn --include sub/exclude_me --exclude exclude_me src/ dst/ > test41.txt
rsync -avn --exclude exclude_me --include sub/exclude_me src/ dst/ > test42.txt

テスト結果

Test01

  • --exclude を指定しない
  • すべて同期される
rsync -avn src/ dst/ > test01.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test02

  • --exclude でディレクトリ名指定
  • 一致するディレクトリ名がすべて除外される
rsync -avn --exclude exclude_me src/ dst/ > test02.txt
sending incremental file list
./
sub/

Test03

  • --exclude でsrcからの相対パスで指定、/を末尾に付与
  • 一致するディレクトリ名がすべて除外される
rsync -avn --exclude exclude_me/ src/ dst/ > test03.txt
sending incremental file list
./
sub/

Test04

  • --exclude でsrcからの相対パスで指定、./を先頭に付与
  • 除外されない
rsync -avn --exclude ./exclude_me src/ dst/ > test04.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test05

  • --exclude でsrcからの相対パスで指定、./を先頭に付与、/を末尾に付与
  • 除外されない
rsync -avn --exclude ./exclude_me/ src/ dst/ > test05.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test11-14

  • --exclude でカレントディレクトリから指定
  • 除外されない
rsync -avn --exclude src/exclude_me src/ dst/ > test11.txt
rsync -avn --exclude src/exclude_me/ src/ dst/ > test12.txt
rsync -avn --exclude ./src/exclude_me src/ dst/ > test13.txt
rsync -avn --exclude ./src/exclude_me/ src/ dst/ > test14.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test21-22

  • --exclude で絶対パスで指定
  • 除外されない
rsync -avn --exclude /home/user/rsync/src/exclude_me src/ dst/ > test21.txt
rsync -avn --exclude /home/user/rsync/src/exclude_me/ src/ dst/ > test22.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test31

  • --exclude でsubディレクトリ以下を指定
  • subディレクトリのみが除外されて、親階層は除外されない
rsync -avn --exclude sub/exclude_me src/ dst/ > test31.txt
sending incremental file list
./
exclude_me/
exclude_me/exclude_me.txt
sub/

Test41

  • --exclude でsubディレクトリを指定
  • --exclude より --include を先に指定する
  • 親階層のみ除外される
rsync -avn --include sub/exclude_me --exclude exclude_me src/ dst/ > test41.txt
sending incremental file list
./
sub/
sub/exclude_me/
sub/exclude_me/exclude_me.txt

Test42

  • --include でsubディレクトリを指定
  • --exlucdeの後に--includeを指定
  • すべて除外される( --include が効かない)
rsync -avn --exclude exclude_me --include sub/exclude_me src/ dst/ > test42.txt
sending incremental file list
./
sub/
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?