Recursive cp copies folder contents instead of folder on OS X

In Terminal (using bash and tcsh) cp -r copies the contents of a folder instead of the folder if the folder argument has a / at the end.

Given the following structure:

folderA
  |-fileA
  |-fileB

The following command copies fileA and fileB to destination instead of copying folderA to the destination:

cp -r folderA/ destination

So it acts as

cp -r folderA/* destination

This is super annoying because by default completion in tcsh adds a / at the end of the folder, so I always end up having to clean up if I forget to delete the / at the end. This behavior is different from every other *nix I've worked on.

Is there any way to change this and make cp -r folderA/ copy the folder instead of its contents?


There is a case to be made that this behavior makes more sense (and I believe it's used by rsync, even on GNU/Linux). Consider the following case:

 mydir
   |-.fileA
   |-fileB

The the semantics of these three specifiers are all different:

 cp -r mydir   dest/   # creates dest/mydir, containing both files
 cp -r mydir/  dest/   # copies both files directly into dest
 cp -r mydir/* dest/   # copies fileB, but not .fileA, directly into dest

I believe this is an example of a difference between BSD and GNU. You can install GNU flavored tools using fink (see coreutils package), but these might not preserve all HFS+ metadata, so proceed with caution.

You can intercept the command and strip out the trailing slash. I'll leave that as an exercise to the reader, or other answerers.

You can alter the tab-completion behavior so the slash isn't added in the first place. Add the following to your ~/.tcshrc:

 unset addsuffix

You can achieve a similar effect in bash and misc readline utilities by adding the following to your ~/.inputrc:

 set mark-directories off

Without manually deleting the '/' appended in a default installation? Unfortunately not as stated on Apple's Darwin man page for cp is "If the source_file ends in a /, the contents of the directory are copied rather than the directory itself." So it appears it's part of the design of Darwin's cp.

It is however possible to unset the addsuffix shell variable in tcsh in order to drop the '/' from being added to directories. The big downside being that it's now very difficult to tell if you're dealing with a directory or a file just from the completion. To do so type on your command line (or add to your .tcshrc):

 unset addsuffix

For bash you can use the mark-directories setting. You can type the following or add it to your .bashrc:

 bind 'set mark-directories off'

Source for use of mark-directories: MacNN Forums: Hal Itosis


"You can intercept the command and strip out the trailing slash. I'll leave that as an exercise to the reader, or other answerers." Adding

function cp() { /bin/cp $(echo "$*" | sed 's|/ | |g'); }

to ~/.bashrc seems to do the trick.