
2つのディレクトリがあり、ターゲットディレクトリにすでに存在するサブディレクトリのコンテンツのみを同期する必要があります。たとえば、
ソースコードディレクトリ:
- フォルダA
- フォルダB
- フォルダC
- フォルダD
- フォルダE
ターゲットディレクトリ:
- フォルダB
- フォルダD
- フォルダ
フォルダBとフォルダDの一意のコンテンツをソースからターゲットに同期する必要があります(フォルダZはソースに存在しないため無視する必要があります)。同様に、フォルダA、C、Eをコピーするためにターゲットディレクトリは必要ありません。
デフォルトでは、「対象のすべてのサブディレクトリに対して同じサブディレクトリがソースに存在する場合、ソースはそのサブディレクトリの内容を同期します。」
役に立つならこれはローカルディレクトリです。
これが意味があることを願っています。ご協力ありがとうございます!
答え1
このようなスクリプトを使用できます。
(
cd destination &&
for d in *
do
[ -d "$d" -a -d source/"$d" ] && rsync -a source/"$d" .
done
)
スタンドアロンの場合、角括弧は( ... )
ディレクトリの変更をローカライズするためにのみ使用されるため、必要ありません。
ファイルがソースに存在しなくなったときにターゲットのファイルを削除するには、に追加します--delete
。rsync
答え2
次のbash
スクリプトを作成し、ソースディレクトリとターゲットディレクトリのパスを変更して実行します。
#!/bin/bash
source=/path_to/source_dir
destination=/path_to/destination_dir
shopt -s nullglob
{
for dir in "$destination/"*/; do
dirname=$(basename "$dir")
if [ -d "$source/$dirname" ]; then
printf '+ /%s/***\n' "$dirname"
fi
done
printf -- '- *\n'
} > "$source/filter.rules"
#rsync -av --filter="merge $source/filter.rules" "$source"/ "$destination"
filter.rules
これにより、ソースディレクトリに次の内容を含むファイルが作成されます。
+ /folder B/***
+ /folder D/***
- *
最初の行+ /folder B/***
は短い構文です。
+ /folder B/
ディレクトリを含める+ /folder B/**
ファイルとサブディレクトリを含める
- *
ルートディレクトリのファイルとディレクトリを除外します。
ルールが期待どおりに表示されたら、最後の行のコメントを外し、rsync
ディレクトリにマージフィルタを使用してスクリプトを再実行します。
答え3
フラグは--existing
あなたが探しているものです。マニュアルページから:
--existing, --ignore-non-existing
This tells rsync to skip creating files (including
directories) that do not exist yet on the destination. If this option is combined
with the --ignore-existing option, no files will be updated (which can be useful
if all you want to do is delete extraneous files).
This option is a transfer rule, not an exclude, so it doesn’t affect the data that
goes into the file-lists, and thus it doesn’t affect deletions. It just limits
the files that the receiver requests to be transferred.