macOS and Linux Treat 'cp' Differently
I’m using an NPM script for building a project. I develop on macOS, but my production builds are run by Jenkins on a Linux server.
The build script copies the contents of one directory to another. When I run it locally,
the cp
command copies the contents but not the directory itself,
which is what I want to happen.
However, when Jenkins creates a build the directory itself is copied. Here’s an example:
# macOS. The contents of the folder are copied.
$ ls source
one two three
$ cp -a source/ destination/
$ ls destination
one two three
# Linux. The folder itself is copied.
$ ls source
one two three
$ cp -a source/ destination/
$ ls destination
source/
Weird! Fortunately the solution is very simple, just add a .
:
# macOS. The contents of the folder are copied.
$ ls source
one two three
$ cp -a source/. destination/
$ ls destination
one two three
# Linux. The contents of the folder are copied.
$ ls source
one two three
$ cp -a source/. destination/
$ ls destination
one two thee