Linux vs OSX - the cp command

Something I recently stubbed my toe on floating between Linux and OSX environments is the usage of cp to move files around.

This is one very major, yet subtle difference between the BSD (OSX) and GNU (Linux) implementation of the cp command when using the recursive (-R) flag.

Example

$> mkdir original
$> cd original 
$> touch file1
$> touch file2
$> touch file3
$> cd ..
$> mkdir target
$> cp -R original/ target/

GNU (Linux)

$> ls target
original/

BSD (OSX)

$> ls target
file1 file2 file3

As you can see from the examples GNU ignores the trailing slash on the original.  It treats original and original/ the same, in both situations copying the original directory into the target.  While in a BSD system, the trailing slash is significant.  It alters the behavior, copying the directory itself (without the slash) or the contents of the directory (with the slash).

Make sure you take this into account especially when scripting for multiple environments.  cp does not ask permission to overwrite files, and this subtle difference could lead to unexpected results.