Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Relevant explanation of the difference between the BSD and GNU implementations of cp - jondavidjohn.com/blog/2012/09/linux-vs-osx-the-cp-command –  jondavidjohn Sep 26 '12 at 16:36
    
I would think that the two cp commands do not act the same as the second would not copy hidden files and directories in folderA, while the other does. –  Volker Siegel Jul 24 at 5:08

2 Answers 2

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
share|improve this answer
1  
It adds unnecessary complexity imo. In bash, i'm for shopt -s dotglob all the way. –  Ian Kelling May 23 '09 at 6:29
    
shopt doesn't work in tcsh, does it? I don't have strong feelings either way, I'd just like them to offer a "--strip-trailing-slashes" command line switch: gnu.org/software/coreutils/manual/html_node/… –  Matt G May 23 '09 at 7:05

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.