微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cp 不适用于带有空格的文件路径的源变量

如何解决cp 不适用于带有空格的文件路径的源变量

我想将所有内容从源目录(目录名称包含空格)复制到目标。

我的工作目录中 ls -l 命令的输出如下:

drwxrwxr-x 2 ubuntu ubuntu 4096 Apr  5 06:39  destination
drwxrwxr-x 2 ubuntu ubuntu 4096 Apr  5 06:08 'source directory with spaces'

当我这样做时 - cp -r ./source\ directory\ with\ spaces/. destination/ 命令运行良好,并且成功复制了“带空格的源目录”文件夹中的文件(A.txt、B.txt 和 C.txt)。

我想让它动态化,所以我将源位置存储在一个变量中 -

fileVar="./source\ directory\ with\ spaces/."

现在,当我使用以下任一命令点击 cp 命令时:

cp -r $fileVar destination/

错误

cp: cannot stat './source\': No such file or directory
cp: cannot stat 'directory\': No such file or directory
cp: cannot stat 'with\': No such file or directory
cp: cannot stat 'spaces/A.txt': No such file or directory

cp -r "$fileVar" destination/

错误

cp: cannot stat './source\ directory\ with\ spaces/A.txt': No such file or directory

我该如何解决?提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。