Bash – Recursive cp copies folder contents instead of folder on OS X

bashcommand-line-interfacemac-osx

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?

Best Answer

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