Shell 脚本复制所有文件和子目录 - 带空格的文件夹名称

Shell Script Copy All Files and Subdirectories - Folder Names With Spaces

我正在尝试使用 shell 脚本复制所有文件和子目录。问题是一些文件夹名称有空格。

cp -r "//shrdata/Legal/test location/test folder/*" "//shrdata/Legal/open access/"

我得到的错误是"The System Cannot Find the File Specified"

尝试:

cp -r "/shrdata/Legal/test location/test folder"/* "/shrdata/Legal/open access/"

要使 * 正常工作,它必须在引号之外。

例子

假设我们有一个包含两个文件的test folder

$ ls -1 test\ folder/
file1
file2

现在,让我们尝试将 * 放在引号中:

$ echo "test folder/*"
test folder/*

因为 * 在引号中,所以它没有展开为文件名列表。相反,它仅被视为文字字符。因此,如果我们尝试以这种方式复制文件,我们将得到一个文件未找到的错误,因为没有文件被命名为 *:

$ cp "test folder/*" target
cp: cannot stat ‘test folder/*’: No such file or directory

如果我们将 * 放在引号外,那么 路径名扩展 将被执行:

$ echo "test folder"/*
test folder/file1 test folder/file2

这意味着此表单在与 cp 一起使用时将正常工作。