3

What command can reproduce this function in bash?

In Finder select File then press ⇧⌥ to make the command visible (⇧⌥⌘D).

In AppleScript it's available as

tell application "Finder" to duplicate selection with exact copy

UPDATE. I checked the birthtimes with stat.

commands comparison

1.61803
  • 1,241
  • Is that copying files? I don't know OS X at all. If so you can try cp in bash to make a copy of the file as in cp <src file> <dest name> – Eric Renouf May 14 '15 at 12:48
  • You know OS/X is a Unix, right? What other Unix are you interested in? Or are you looking for things like apple-script-interpreter -c 'tell application "Finder" to duplicate selection with exact copy' – Stéphane Chazelas May 14 '15 at 12:56
  • @StéphaneChazelas FYI the real command for your hypothetical apple-script-interpreter -c ... is osascript -e .... – Random832 May 14 '15 at 13:20
  • I asked for the command equivalent of a GUI function for exact copy (duplicate) of a file. – 1.61803 May 14 '15 at 19:39

1 Answers1

5

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.

Random832
  • 10,666
  • cp -RPp may not preserve anything beyond ownership and permissions (like acls, extended attributes, hardlink relationship...). pax -rwpe is the POSIX command to preserve everything. – Stéphane Chazelas May 14 '15 at 13:03
  • @StéphaneChazelas does pax exist on OSX? I can replace my tar example with this if so. Also, does this create an archive that has to be extracted in another step? Do you know if any of these commands preserve resource forks? – Random832 May 14 '15 at 13:04
  • It would seem OS/X has a pax (though it doesn't say anything about preserving other things than ownership, hardlink and permissions).. pax -rw, like cpio -p is the copy mode, it doesn't generate an archive. Here's the POSIX spec – Stéphane Chazelas May 14 '15 at 13:09
  • @StéphaneChazelas I've updated my answer to use ditto, which is an OSX-specific command that takes care of this. I left in the other information for people using other Unix systems. – Random832 May 14 '15 at 13:12
  • 1
    Note that pax -rwpe src dst creates a dst/src. – Stéphane Chazelas May 14 '15 at 13:23
  • Another thing that GNU cp -a tries to preserve is sparseness. (native OS/X file systems don't support sparse files though AFAIK) – Stéphane Chazelas May 14 '15 at 13:27
  • 1
    @Random832 Note: cp and pax will preserve resource forks and extended attributes when you copy to a HFS file system. ACL's are lost when copying to a FAT32 file system but resource forks and metadata are preserved. – fd0 May 14 '15 at 14:53
  • @fd0 do you have a citation for this? In researching this question I found several articles asserting the opposite. Even if it works now I'd like to know what version it was added in. – Random832 May 14 '15 at 14:56
  • @StéphaneChazelas This is the typical behavior of Unix commands in this situation (for existing directory dst) and I simply felt I should explain ditto as an exception to the rule. Does pax even do it for non-existing dst? – Random832 May 14 '15 at 14:59
  • The thing is that dst has to exist or it will fail and pax -rwpe foo/bar dst creates dst/foo/bar (while cp -a foo/bar dst would create dst/bar). You may need to do cd -P -- "$src" && pax -rwpe . /full/path/of/dst. See also http://unix.stackexchange.com/a/202435 – Stéphane Chazelas May 14 '15 at 15:02
  • 1
    @Random832 (https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/cp.1.html) I tested both pax and cp with OS X 10.6. – fd0 May 14 '15 at 15:38
  • Neither sudo cp -a src dst (which implies -p) nor sudo ditto src dst copy the creation time. Finder's Duplicate preserves it though. – 1.61803 May 14 '15 at 19:39
  • @1.6.1803 ctime changes with the creation of the inode (CNID on HFS). pax, ditto, and cp preserve birthtime with OSX 10.6. I should mention that birthtime is only available when running the 64 bit kernel. – fd0 May 17 '15 at 11:28