Linuxでは、あるパスのファイルを別のパスにコピーする

Linuxでは、あるパスのファイルを別のパスにコピーする

あるパスから別のパスにファイルをコピーしようとしています。次のパターンでは、すべてのファイル名を含むテキストファイルがあります。

file-1.txt
file-2.pdf
file-3.ppt
....

.sh次のコードを使用してファイルを作成しました。

 #!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/upload/';
for i in $file;
do
 filePath=$fromPath$i
 #echo $filePath
 if [ -e $filePath ];
 then
   echo $filePath
   yes | cp -rf $filePath $toPath
 else
   echo 'no files'
 fi
done

上記のコードは、テキスト全体の最後のファイル名のみをターゲットパスにコピーします。

ベストアンサー1

file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/

cd "$fromPath" && xargs mv -t "$toPath" < "$file"

おすすめ記事