According to this article, the main difference between this and the normal duplicate (⌘D) function is that file ownership is retained. The normal duplicate function preserves file permissions but not ownership.
The best equivalent to this behavior on OSX is the ditto
command. You can simply use sudo ditto src dst
and it will preserve everything by default. Sudo is necessary if file ownership must be preserved. If you are copying a directory and dst
already exists, you should be aware it will be merged rather than creating a new directory dst/src
. Ditto is also capable of creating cpio or zip format archives, explained in the manpage.
The below answers are mostly relevant to other UNIX systems, and may not preserve the resource fork or other HFS-specific attributes on some versions of OSX.
First, I should mention that any of these commands can only preserve ownership when run as root (e.g. with sudo
or su
depending on the system. The sudo
command exists on OSX.). When not run as root, most of these will preserve any attributes they can, but the file will be owned by your user ID.
The cp -a
command preserves as much as possible, including permissions, ownership (if root), timestamps, and symbolic links. While -a
is supported on many systems (it also preserves hardlinks on GNU, but not on OSX), it is not in POSIX. The nearest equivalent POSIX command is cp -pPR
- the relevant standard option for preserving permissions and ownership is p
, but P
is required to copy symbolic links and R
to copy directories.
The best way to copy while also preserving hardlinks and other extended attributes that cp may not handle is to use the pax command: pax -rwpe src dstdir
. This doesn't let you change the name of the source file, and the destination directory must already exist. It will create a copy as dstdir/src, so for ideal results you must execute this from the directory where the source file exists.
On some systems, pax may not exist, in which case you can use tar: tar cf - srcfiles | (cd dstdir; [sudo] tar xf -)
. The same advice about the filename and source and destination directories applies as for pax.
cp
in bash to make a copy of the file as incp <src file> <dest name>
– Eric Renouf May 14 '15 at 12:48apple-script-interpreter -c 'tell application "Finder" to duplicate selection with exact copy'
– Stéphane Chazelas May 14 '15 at 12:56apple-script-interpreter -c ...
isosascript -e ...
. – Random832 May 14 '15 at 13:20