Skip to content

Instantly share code, notes, and snippets.

@turtlemonvh
Created March 7, 2019 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlemonvh/c007580c372027489e2325e684cad427 to your computer and use it in GitHub Desktop.
Save turtlemonvh/c007580c372027489e2325e684cad427 to your computer and use it in GitHub Desktop.
Different `cp -r` behavior on Linux and Mac

Different cp -r behavior on Linux and Mac

When running the sript above, we see different behavior on Mac and Linux.

On Linux, dir a is copied into dir b

$ bash dircptest.sh
total 0
drwxrwxr-x. 2 vagrant vagrant 21 Mar  7 19:46 a

On Mac, the contents of dir a are copied into dir b

$ bash dircptest.sh
total 0
-rw-r--r--  1 timothy  wheel  0 Mar  7 14:55 thing.1

Resolution

To have the same effect on Mac and Linux:

Copy contents

Change line 5 to cp -r a/. b/.

Copy dir

Change line 5 to cp -r a b/.

cd /tmp
mkdir a
mkdir b
touch a/thing.1
cp -r a/ b/
ls -l b/
rm -rf /tmp/a/ /tmp/b/
@turtlemonvh
Copy link
Author

An alternative to the . syntax is

cp -r a/* b/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment