Regular-Expression

rsync 模式匹配名稱

  • August 31, 2017

我想 rsync 一堆文件,並在匹配要同步的內容時忽略名稱中的任何大小寫、空格、句點、破折號或下劃線差異。

因此,作為一個極端範例,“TheFilename.zip”將匹配“__THE- - -File—nam-e…._.zip”(假設大小和時間匹配)。

我想不出辦法來做到這一點。

這個腳本可能會做你想做的事

#!/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當您滿意它按預期工作時刪除。

引用自:https://unix.stackexchange.com/questions/375871