rsync - シェルスクリプトを使用して、各JPG / jpgファイルを1つのハードドライブから1つのディレクトリに移動します。

rsync - シェルスクリプトを使用して、各JPG / jpgファイルを1つのハードドライブから1つのディレクトリに移動します。

私はある方向を探していました。うまくいきましたが、何もありませんでした。

ローカルハードドライブがあり、すべての.JPGファイルと.jpgファイル一つ別のローカルハードドライブのディレクトリです。

rsyncの使用 - 私の考え:(ソースハードドライブはWindowsシステムです。ファイル名にはスペースがあります)
- ソースハードドライブのすべてのディレクトリのリストを取得し、テキストファイルに入れます。
- ディレクトリリストテキストファイルを編集して、すべてのディレクトリに引用符を追加します。
- シェルスクリプトループを使用して、テキストファイルの各行でrsyncを実行します。



コマンドラインでrysncを使用してディレクトリを個別にコピーすると機能します。

rsync -r --include '*.jpg' --include '*.JPG' --exclude '*' --prune-empty-dirs /"media"/"tfrd"/"Disk06_01_M"/"Disk02_01_X"/"x_images and camera"/"vernon pics_other"/"office_PBWT4_YBP6D-7wmff_bpwg4_2vgby"/"office pro disk 2"/"BCM"/"Program Files"/"Microsoft Small Business"/"Business Contact Manager"/"SDKComponents"/"PPCRL"/ /home/tfrd/Desktop/filesync/rsync/dir2/  

シェルスクリプトの使用 - 動作しません。問題は、dir変数とスラッシュまたはスペースにあります。最も重要なことは、最後のディレクトリの前に解析を試みているようです。

私は$ IFSのバリエーションを試しました。構文解析の問題だと思ったからです。


私のスクリプト:

#!/bin/bash
file="/home/tfrd/Desktop/filesync/rsync/test_data_01.txt"

while IFS=$'\n' read -r line
#while IFS= read -r line
#while read line
do

#try 01
#rsync  --include '*.jpg' --include '*.JPG' --exclude '*/*/.' $line /home/rsync/dir2

#try 02
#rsync  --include '*.jpg' --include '*.JPG' --exclude '/*/' $line /home/rsync/dir2

#try 03
#rsync  --include '*.jpg' --include '*.JPG' --exclude '*/*/' $line /home/rsync/dir2

#try 04
rsync --protect-args --include '*.jpg' --include '*.JPG' --exclude '*/*/.' --prune-empty-dirs $line /home/rsync/dir2

done <"$file"



私のテストデータ:(txtファイルにわずか2行)

/"media"/"tfrd"/"Disk06_01_M"/"Disk02_01_X"/"x_images and camera"/"vernon pics_other"/"office_PBWT4_YBP6D-7wmff_bpwg4_2vgby"/"office pro disk 2"/"BCM"/"Program Files"/"Microsoft Small Business"/"Business Contact Manager"/"SDKComponents"/"PPCRL"/
/"media"/"tfrd"/"Disk06_01_M"/"Disk02_01_X"/"x_images and camera"/"vernon pics_other"/"office_PBWT4_YBP6D-7wmff_bpwg4_2vgby"/"office pro disk 2"/"SBA"/"program files"/"Microsoft Small Business"/"Office Accounting 2008"/"SDKComponents"/"PPCRL"/



結果:(清掃後に読むことができます)

tfrd@Beagle-Ubuntu:~/Desktop/filesync/rsync$ clear


tfrd@Beagle-Ubuntu:~/Desktop/filesync/rsync$ ./script.sh 
/media/tfrd/Disk06_01_M/Disk02_01_X/x_images and camera/vernon pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby/office pro disk 2/BCM/Program Files/Microsoft Small Business/Business Contact Manager/SDKComponents/PPCRL

rsync: link_stat "/media/tfrd/Disk06_01_M/Disk02_01_X/x_images" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/and" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//camera" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/pro" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/disk" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//2/BCM" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//Files" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/Small" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//Business" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/Contact" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//Manager/SDKComponents" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.0]


/media/tfrd/Disk06_01_M/Disk02_01_X/x_images and camera/vernon pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby/office pro disk 2/SBA/program files/Microsoft Small Business/Office Accounting 2008/SDKComponents/PPCRL

rsync: link_stat "/media/tfrd/Disk06_01_M/Disk02_01_X/x_images" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/and" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//camera" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/pro" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/disk" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//2/SBA" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//files" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/Small" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//Business" failed: No such file or directory (2)
rsync: link_stat "/home/tfrd/Desktop/filesync/rsync/Accounting" failed: No such file or directory (2)
rsync: change_dir "/home/tfrd/Desktop/filesync/rsync//2008/SDKComponents" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.0]

ベストアンサー1

すべてのパス要素を引用する必要はありません。

パスのリストを含むテキストファイルでは、引用符を使用せずにファイル名のみを使用することをお勧めします。

/media/tfrd/Disk06_01_M/Disk02_01_X/x_images and camera/vernon pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby/office pro disk 2/BCM/Program Files/Microsoft Small Business/Business Contact Manager/SDKComponents/PPCRL/
/media/tfrd/Disk06_01_M/Disk02_01_X/x_images and camera/vernon pics_other/office_PBWT4_YBP6D-7wmff_bpwg4_2vgby/office pro disk 2/SBA/program files/Microsoft Small Business/Office Accounting 2008/SDKComponents/PPCRL/

次に、次のようにスクリプトを作成します。

#!/bin/bash
file="/home/tfrd/Desktop/filesync/rsync/test_data_01.txt"

while IFS=$'\n' read -r line
do
    rsync --protect-args --include '*.jpg' --include '*.JPG' --exclude '*/*/.' --prune-empty-dirs "$line" /home/rsync/dir2
done < "$file"

それはすべてです。パスパラメータとして使用される場合は、$lineが含まれます。これにより、埋め込まれたスペースやその他の特殊文字が正しく処理されます。内部のすべてが単一の値として扱われるので動作します。"..."rsync"..."

おすすめ記事