使用包含文件名的空白文本文档在目录之间进行批复制



我在MacOS Ventura中使用终端。我想要一个纺织品文件,我将使用终端复制。当文件名中有空格时,这将不起作用。

A=~/Documents/test1
B=~/Documents/test2
while IFS= read -r file; do
cp "${A}/${file}" "${B}"
done < ~/Documents/testme.txt

这是示例文本文件。

# contents of ~/Documents/testme.txt
the first file.pdf
the second file.pdf
the third file.pdf

运行上面的代码后,我得到:

cp: /Users/userdirectoryname/Documents/test1/the first file.pdf: No such file or directory
cp: /Users/userdirectoryname/Documents/test1/the second file.pdf: No such file or directory
cp: /Users/userdirectoryname/Documents/test1/the third file.pdf: No such file or directory

我认为这是因为文件名中有空格,但我们假设我想使用一种纺织品,其中只有换行符分隔的非转义空格文件。

这是怎么做到的?

即使我试图转义use引号,也会出现以下错误。

while IFS= read -r file; do
cp ""${A}/${file}"" ""${B}/${file}""
done < ~/Documents/testme.txt
cp: "/Users/userdirectoryname/Documents/test1/the first file.pdf": No such file or directory
cp: "/Users/userdirectoryname/Documents/test1/the second file.pdf": No such file or directory
cp: "/Users/userdirectoryname/Documents/test1/the third file.pdf": No such file or directory

但是复制粘贴同一行没有问题!

cp "/Users/userdirectoryname/Documents/test1/the first file.pdf" ./test2工作!

由于变量AB可能会进行波浪形扩展,因此复制文件的命令是

cp $~A/$file $~B

最新更新