異なる名前のRsyncファイルのディレクトリ

異なる名前のRsyncファイルのディレクトリ

rsyncディレクトリから次incomingのディレクトリにファイルをコピーしようとしています。outgoing

/testcopy/folder1/incoming/test1.txt

到着

/testdest/folder1/outgoing/

ディレクトリソース:

/testcopy/folder1/incoming/test1.txt
/testcopy/folder1/incoming/test2.txt
/testcopy/folder2/incoming/test1.txt
/testcopy/folder2/incoming/test2.txt
/testcopy/folder3/incoming/test1.txt
/testcopy/folder3/incoming/test2.txt

ディレクトリの宛先:

/testdest/folder1/outgoing/
/testdest/folder2/outgoing/
/testdest/folder3/outgoing/

私が望む目的地は次のとおりです。

/testdest/folder1/outgoing/test1.txt
/testdest/folder1/outgoing/test2.txt
/testdest/folder2/outgoing/test1.txt
/testdest/folder2/outgoing/test2.txt
/testdest/folder3/outgoing/test1.txt
/testdest/folder3/outgoing/test2.txt

私が試したスクリプトrsync

touch /testcopy/folder3/incoming/test4.txt

私が見ると予想されるのはtest4.txt次のファイルです/testdest/folder3/outgoing/

# rsync -av /testcopy/*/incoming/* /testdest/*/outgoing/
sending incremental file list

sent 520 bytes  received 12 bytes  1,064.00 bytes/sec
total size is 0  speedup is 0.00

上記のスクリプトを何度も繰り返してみましたが、うまく動作しないようです。

ベストアンサー1

rsyncソースと宛先の間のパスを書き換えることはできません。

rsyncあなたができることは、/testcopy/*/incoming各ディレクトリに対してこれを一度呼び出すことです:

for srcdir in /testcopy/*/incoming/; do
    [ ! -d "$srcdir" ] && break

    destdir=/testdest/${srcdir#/testcopy/}   # replace /testcopy/ with /testdest/
    destdir=${destdir%/incoming/}/outgoing/  # replace /incoming/ with /outgoing/

    mkdir -p "$destdir" &&
    rsync -av "$srcdir" "$destdir"
done

各ディレクトリのパスプレフィックスをに置き換え、パスサフィックスをに置き換えてターゲットパスを作成しますincoming。これは2つの標準パラメータ置換を使用して行われます。/testcopy//testdest//incoming//outgoing/

ループは、ターゲットディレクトリがまだ存在しない場合、ターゲットディレクトリも作成します。

[ ! -d "$srcdir" ] && breakループの始まりは、パターンが何も一致しない場合とmkdir実行されないことを保証しますrsync(デフォルトでは、シェルは内部にない限り、パターンは拡張されていませんzsh)。では、ループの前に使用bashしたいかもしれません。shopt -s nullglob

おすすめ記事