6

I have the following code to copy an entire folder:

#!/bin/bash

B="H:\pdf2txt"

DST_DIR="H:\Test_Folder"

cp -r "$B" "$DST_DIR"

This works as expected on a windows machine but when I change it to this for my Mac:

#!/bin/bash

B="/Volumes/Mac_Storage/pdf2txt"

DST_DIR="/Volumes/Mac_Storage/Test_folder"

cp -r "$B" "$DST_DIR"

it copies the folder but then adds another copy within that one with the same content and then same again till the file path is too long for the mac to handle. folder structure like this:

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

etc etc..

Any idea why it works different from windows to Mac ?

  • Is this the entirety of the code? I can see some unused variables. I wonder if there is some kind of recursion? You can also debug with echo, e.g. repeat the cp… line with echo cp… above it, then see if the variables are as expected, and if it repeats unexpectedly. – Sparhawk Feb 13 at 11:35
  • @Sparhawk Sorry I was only using that one variable to test it, have edited post. It was part of a larger code but have deleted everything to eleminate problems but even with just the code you see above I am having the same re-occuring copy problem. – madmiddle Feb 13 at 11:40
  • Is there a symlinks? – Giacomo Catenazzi Feb 13 at 11:45
  • @roaima apologies typo, now corrected – madmiddle Feb 13 at 12:40
8

I think telcoM's answer sniffs around the primary cause: It's likely to involve a symlink, just a different one.

Based on my experimentation, there's one set of circumstances that fits the bill:

  • Test_folder is a symlink to pdf2txt, and
  • the OP is using stock macOS cp, which has a BSD heritage

This sort of symlink defeats macOS cp's cycle detection. (GNU cp, as would've been installed by Homebrew or some other package manager, doesn't follow symlinks by default, even when copying recursively.)

Here's a demo, tested on 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
  • Thats basically what I am getting, it just keeps going and going and going till that 'name too long' error. I shall try the command and update the post tonight – madmiddle Feb 13 at 12:46
5

Is there a symbolic link /Volumes/Mac_Storage/pdf2txt/pdf2txt pointing to .?

Run ls -l /Volumes/Mac_Storage/pdf2txt to check.

If you see a line like this:

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

... then that is the cause.

The command to create such a link would be ln -s . pdf2txt, which might be fairly easy to happen accidentally as a result of a typo.

  • I'll have to read up on symbolic link is as I have no idea what that means. I shall run it tonight and get back to you. – madmiddle Feb 13 at 11:50
  • 1
    Highly unlikely to be the case. macOS stock (BSD) cp stops the copy dead with the error cp: pdf2txt/pdf2txt: directory causes a cycle, which a Homebrew-installed (GNU) cp copies the directory correctly because it doesn't follow symlinks by default. – Adrian Feb 13 at 12:00
1

I was really bored and i found this on the cp man page on my mbp runnin mojave:

COMPATIBILITY Historic versions of the cp utility had a -r option. This implementation supports that option; however, its use is strongly discouraged, as it does not correctly copy special files, symbolic links, or fifo's.

So yeah, cp -r will not work properly on macOS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.