gpt4 book ai didi

linux - linux下将文件从一个路径复制到另一个路径

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:13 25 4
gpt4 key购买 nike

我是 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

上面的代码仅复制文本中的最后一个文件名,而不是全部复制到目标路径。

请帮忙。

最佳答案

循环一组输入行的正确方法是

while read i; do
: something with "$i"
done <filenames.txt

请注意在 "$i" 周围使用双引号以及字符串包含文件名组件的一般变量插值。仅当您特别要求 shell 进行分词和通配符解析时,不带引号的值才适用。

关于linux - linux下将文件从一个路径复制到另一个路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22934396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com