Home

PSA: cp -r behaves differently on linux and macOS

Today I found out (the hard way) that the GNU (available on linux) and BSD (available on macOS) implementations of cp behave differently when used to recursively copy.

The differing behaviour comes when running cp with the -r flag on a source directory that ends with a slash (/):

While this is documented behaviour (see man pages below), it can lead to unwanted behaviour if you develop on a mac and build on a linux-based CI system.

Man pages

GNU (linux):

-R, -r, --recursive
          copy directories recursively

BSD (macOS)

-R      If source_file designates a directory, cp copies the directory and the
        entire subtree connected at that point.

        If the source_file ends in a /, the contents of the directory are
        copied rather than the directory itself.

        [...]