🗂

スラッシュの有無によるrsyncの挙動の違いを理解する

2022/03/24に公開

次のような構造のディレクトリがあります。

$ tree source
source
└── 0
    ├── 1
    │   └── 2.txt
    └── 1.txt

2 directories, 2 files

source ディレクトリの中身を、そのまま destination ディレクトリに複製したい場合、次のコマンドを実行します。

$ rsync -a source/0 destination

source と同じ内容で作成されました。

$ tree destination
destination
└── 0
    ├── 1
    │   └── 2.txt
    └── 1.txt

2 directories, 2 files

コマンドラインのタブ補完で、 source/0/ と末尾に / が入ってしまうことがあると思います。

$ rsync -a source/0/ destination

この場合意味が変わり、 0 階層が消失し、 1 階層がルートに来ます。

$ tree destination
destination
├── 1
│   └── 2.txt
└── 1.txt

1 directory, 2 files

/ を付けることで、箱から中身を取り出すと考えるとよさそうです。

スラッシュの有無には、両方とも用途があると思います。

アーカイブを展開して、不要な親ディレクトリを省きたい場合、 / ありで実行すると一手間省けそうです。

親ディレクトリも含めまったくのクローンを作成したい場合、コピー元に / を付けないことで、親ディレクトリも保持することができます。

Discussion

ログインするとコメントできます