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:
The the semantics of these three specifiers are all different:
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
:You can achieve a similar effect in bash and misc readline utilities by adding the following to your
~/.inputrc
: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 intcsh
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):For
bash
you can use themark-directories
setting. You can type the following or add it to your .bashrc: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
to ~/.bashrc seems to do the trick.