LoginSignup
16
14

More than 5 years have passed since last update.

rsyncの-R(--relative)オプション

Posted at

rsync で -R オプションを使ってたら、同僚から「なんぞそれ」と聞かれたので、私の -R オプションの使い方を書いておきます。


ソースのディレクトリ構成は次のようになっていたとします。

/tmp/source/
  aaa/
    aaa.txt
  bbb/
    bbb.txt
  ccc/
    ccc.txt
  xxx/
    xxx.txt
  yyy/
    yyy.txt
  zzz/
    zzz.txt
  kuso.txt
  miso.txt
  tech.txt

これを別のサーバに次のように転送したいとします。

/tmp/target/
  aaa/
    aaa.txt
  bbb/
    bbb.txt
  ccc/
    ccc.txt

ポイントは・・・

  • aaa と bbb と ccc だけ転送したい
  • 転送先の基準ディレクトリが異なる(/tmp/source/ != /tmp/target/)

普通にやると次のようになるでしょうか

$ rsync -azvn /tmp/source/ remote:/tmp/target/ --include /aaa/ --include /bbb/ --include /ccc/ --exclude /\*
  • --include /aaa/
  • --include /bbb/
  • --include /ccc/
    • 必要なディレクトリを含める
  • --exclude /\*
    • その他のファイル/ディレクトリを除外

--include や --exclude が沢山出てきてうっとうしいです。


そこで -R オプションを使います。

$ cd /tmp/source/
$ rsync -Razvn aaa/ bbb/ ccc/ remote:/tmp/target/

-R オプションを使うと転送元に指定したディレクトリ/ファイルの名前がそのまま転送先のディレクトリの下に展開されます。なので aaa/ のようなディレクトリ名の指定で remote:/tmp/target/aaa/ のように転送されます。

カレントディレクトリを変更するのが気持ち悪ければ次のようにもできます。

$ rsync -Razvn /tmp/source/./{aaa,bbb,ccc}/ remote:/tmp/target/

転送元のディレクトリの途中に /./ があれば、それ以降のパスが転送先のディレクトリの下に展開されます。

16
14
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
16
14