rsync パターン一致名

rsync パターン一致名

複数のファイルをrsyncして同期ターゲットを一致させるときは、名前の大文字と小文字、スペース、ピリオド、ダッシュ、またはアンダースコアの違いを無視したいと思います。

したがって、極端な例として、「TheFilename.zip」は「__THE- - -File---nam-e....._.zip」と一致します(サイズと時間が一致すると仮定)。

私はこれを行う方法を考えることができません。

ベストアンサー1

このスクリプトはあなたが望むことをすることができます

#!/bin/bash
#
ritem="$1"              # [ user@ ] remotehost : [ / ] remotepath_to_directory
shift

rhost="${ritem/:*}"     # Can be remotehost or user@remotehost
rpath="${ritem/*:}"     # Can be absolute or relative path to a directory

# Get list of files on remote
#
echo "Looking in $rpath on $rhost" >&2
ssh -n "$rhost" find "$rpath" -maxdepth 1 -type f -print0 |
    while IFS= read -r -d $'\0' rfile
    do
        rkey=$(printf "%s" "$rfile" | tr -d '[:space:]_. -' | tr '[:upper:]' '[:lower:]')
        list[${rkey/*\/}]="$rfile"
    done


# Get list of files from local and copy to remote
#
ss=0

echo "Considering $*" >&2
for lpath in "$@"
do
    test -f "$lpath" || continue

    lfile="${lpath/*\/}"
    lkey=$(printf "%s" "$lfile" | tr -d '[:space:]_. -' | tr '[:upper:]' '[:lower:]')

    # Do we have a match in the map
    rfile="${list[$lkey]}"
    test -z "$rfile" && rfile="$lfile"

    # Copy across to the remote system
    echo "Copying $lpath to $rhost:$rpath/$rfile" >&2
    rsync --dry-run -av "$lpath" "$rhost":"$rpath/$rfile" || ss=$((ss+1))
done


# All done. Exit with the number of failed copies
#
exit $ss

使用例

375871.sh remotehost:remotepath localpath/*

--dry-run期待どおりに機能すると満足している場合は削除してください。

おすすめ記事