Understanding copying package files

filesystem

I am seeing some unexpected results while copying package files in a shell script. There are two interrelated issues.

  1. I have commands similar to the following in a shell script:

    cp -rp path/to/package/file destination/directory

The problem that I am seeing is that sometimes the package is copied as a whole, which is what I want. Other times the package "goes away" and just the contents of the package ends up in the destination/directory. I don't see the pattern which is causing this to happen differently. Specifically, in the shell script, the package is copied as a whole. But when I try the exact command from the terminal, I get only the contents.

  1. In the shell script, while the package seems to be copied as a whole, it takes a very long time. Like 20 minutes to copy a 17MB file to a USB drive. When I copy the same package via Finder, it copies in about a minute. Then during testing when I rerun the script, it does not take 20 minutes, but just the minute.

These two results are unexpected. I would expect the same result each time, not varying results with the package verses package contents, and the time to run the script.

Any ideas?

Best Answer

I'm not sure about #2, but I suspect #1 is due to trailing slashes in the file path. cp -Rp /path/to/Application.app /path/to/destination will copy the entire package, while cp -Rp /path/to/Application.app/ /path/to/destination (note the trailing slash after the source) will copy just the contents.

BTW, you should use cp -R instead of cp -r -- in some implementations of cp they're the same, but in some versions the lowercase option has ... oddities. According to the man page on OS X:

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.