選択したサブディレクトリのみ rsync

選択したサブディレクトリのみ rsync

私の音楽ライブラリの一部を外部ドライブから私のオーディオプレーヤーに同期したいと思います。

一般的には、例えば次のようになります。$ソースそして$targetディレクトリとテキストファイル$サブディレクトリ以下に関連するサブディレクトリパスのリストが含まれています。$ソース具体的には、rsyncを(--削除)したいと思います。$targetディレクトリツリー構造(--relative)を維持します。

私のコードはmy_sync.sh次のとおりです

rsync -rR --include-from=$subdirs --exclude="/*" --delete --delete-excluded $source/./ $target

しかし、問題は、これがすぐ下のサブディレクトリでのみ機能することです。$ソース(/dir/*.mp3)、ネストされたサブディレクトリ (例: /dir1/dir2/...) は省略されます。

ベストアンサー1

私は次の解決策を思いついた。

# exclude system created files like .DS_Store, desktop.ini...
arg="-C
"

# project each "$path" to "/$path/**" and concat 'em all
while read line || [[ -n $line ]]; do
    arg+="+ /$line/**
"
done < $subdirs

# */ - includes each folder so that rsync traverses the whole tree,
# *  - excludes everything else
arg+="+ */
- *"

# -m - excludes "empty" dirs that we used to traverse the tree
echo "$arg" | rsync -rmhvn -f ". -" --delete-excluded --info=PROGRESS2 --size-only $source/./ "$target"

おすすめ記事