cp 在 Mac 上的工作方式有所不同

cp 在 Mac 上的工作方式有所不同

我有以下代码来复制整个文件夹:

#!/bin/bash

B="H:\pdf2txt"

DST_DIR="H:\Test_Folder"

cp -r "$B" "$DST_DIR"

这在 Windows 机器上按预期工作,但当我将其更改为我的 Mac 时:

#!/bin/bash

B="/Volumes/Mac_Storage/pdf2txt"

DST_DIR="/Volumes/Mac_Storage/Test_folder"

cp -r "$B" "$DST_DIR"

它会复制该文件夹,然后在该文件夹中添加具有相同内容的另一个副本,然后再次相同,直到文件路径太长而无法处理。文件夹结构如下:

Test_Folder
|   └── pdf2txt 
|       ├── test1.txt
|       ├── test2.txt
|       └── pdf2txt
|           ├── test1.txt
|           ├── test2.txt
|           └── pdf2txt
|               ├── test1.txt
|               ├── test2.txt
|               └── pdf2txt
|                   ├── test1.txt
|                   ├── test2.txt
|                   └── pdf2txt
|                       ├── test1.txt
|                       ├── test2.txt
|                       └── pdf2txt
|                           ├── test1.txt
|                           ├── test2.txt
|                           └── pdf2txt

等等等等..

知道为什么它在 Windows 和 Mac 上的工作方式不同吗?

答案1

我认为电信的答复嗅探主要原因:它可能涉及符号链接,只是一个不同的符号链接。

根据我的实验,有一组情况符合要求:

  • Test_folder是 的符号链接pdf2txt,并且
  • OP 使用的是原生 macOS cp,它具有 BSD 传统

这种符号链接会破坏 macOScp的循环检测。 (GNU cp,就像已经安装的那样自制或其他一些包管理器,默认情况下不遵循符号链接,即使在递归复制时也是如此。)

这是在 macOS High Sierra 上测试的演示:

$ ln -s pdf2txt Test_folder

$ ls -lAF Test_folder
lrwxr-xr-x 1 aho staff 7 Feb 13 20:13 Test_folder -> pdf2txt/

$ /bin/cp -r pdf2txt Test_folder
cp: Test_folder/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt: name too long (not copied)
cp: Test_folder/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt: name too long (not copied)

# Let's just look at the first 30 lines of a recursive ls
# (And yes, we're looking at pdf2txt)
$ ls -lRAF pdf2txt | head -n 30
pdf2txt:
total 0
drwxr-xr-x+ 5 aho staff 170 Feb 13 20:13 pdf2txt/
-rw-r--r--+ 1 aho staff   0 Feb 13 19:43 test1.txt
-rw-r--r--+ 1 aho staff   0 Feb 13 19:43 test2.txt

pdf2txt/pdf2txt:
total 0
drwxr-xr-x+ 3 aho staff 102 Feb 13 20:13 pdf2txt/
-rw-r--r--+ 1 aho staff   0 Feb 13 20:13 test1.txt
-rw-r--r--+ 1 aho staff   0 Feb 13 20:13 test2.txt

pdf2txt/pdf2txt/pdf2txt:
total 0
drwxr-xr-x+ 3 aho staff 102 Feb 13 20:13 pdf2txt/

pdf2txt/pdf2txt/pdf2txt/pdf2txt:
total 0
drwxr-xr-x+ 3 aho staff 102 Feb 13 20:13 pdf2txt/

pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt:
total 0
drwxr-xr-x+ 3 aho staff 102 Feb 13 20:13 pdf2txt/

pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt:
total 0
drwxr-xr-x+ 3 aho staff 102 Feb 13 20:13 pdf2txt/

pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt/pdf2txt:
total 0

答案2

/Volumes/Mac_Storage/pdf2txt/pdf2txt是否存在指向 的符号链接.

跑去ls -l /Volumes/Mac_Storage/pdf2txt检查一下。

如果你看到这样的一行:

lrwxrwxrwx 1 madmiddle madmiddle 1 Feb 12 12:34 pdf2txt -> .

……那么这就是原因。

创建此类链接的命令是ln -s . pdf2txt,这可能很容易由于拼写错误而意外发生。

答案3

我真的很无聊,我在我的 mbp runnin mojave 上的 cp 手册页上找到了这个:

兼容性 cp 实用程序的历史版本有一个 -r 选项。此实现支持该选项;但是,强烈建议不要使用它,因为它不能正确复制特殊文件、符号链接或 fifo。

所以是的, cp -r 在 macOS 上无法正常工作

相关内容