tags:

views:

2139

answers:

5

I'm a Linux guy and I'm used to copying directory trees with cp -a. OS X doesn't have -a option on cp... but it does have the ditto command. I'm reading the man on ditto now, but is there anything I should specifically be looking out for?

A: 
SoloBold
+1  A: 

From Linux cp(1):

-a, --archive same as -dpR

which is confusing, since -d appears to be equivalent to -p. Anyway, OSX has -p and -R so you could just use that.

From the OS X man pages: "Symbolic links are always followed unless the -R flag is set, in which case symbolic links are not followed." Which is opposite to the way Linux works.
SoloBold
+1  A: 

Personally I use rsync -a (or whatever rsync params are called for). My two reasons are: I already know how to do this, and I need my scripts to be portable across Linux/BSD/Solaris. There are also some filesystems where rsync is more efficient than cp.

Sorry that's not a direct answer, I have used ditto on BSDs but don't have any gotchas for you that aren't in the man page.

joelhardi
+3  A: 

According to the cp man page cp -a is the same as cp -dpR which is

-p = preserve mode,ownership,timestamps
-R = recursive
-d = no dereference and preserve links

The OS X equivalent would be

cp -pPR

-p = preserve
-R = recursive
-P = no symbolic links are followed -- can be added but this is the default behavior

The only thing missing is -d which I think is the default behavior but I'm not positive.

I've never messed with ditto

Edit -- @SoloBold

-L follows symbolic links. -p does NOT follow symbolic links. OS X (10.4 at least) has no -d option.

that is a huge difference.

Simurr
oh shit i read that wrong :(
SoloBold
cp -pPR should do the job, on 10.5 at least.
Andrew Medico
It is the same in 10.4
Simurr
A: 

there is a difference between ditto and cp which is that when source is a directory, cp creates a directory with that name on the destination, but ditto just copies the contents. Beware!