macos 如何在 OS X 上使用同上,像 Linux 上的 cp -a 一样工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/160204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:55:58  来源:igfitidea点击:

How to use ditto on OS X to work like cp -a on Linux

macos

提问by Trenton

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

我是一个 Linux 人,我习惯于使用cp -a. OS X 没有-a选项cp...但它确实有ditto命令。我现在正在阅读这个人ditto,但有什么我应该特别注意的吗?

回答by Simurr

According to the cpman page cp -ais the same as cp -dpRwhich is

根据cp手册页cp -acp -dpR相同,即

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

The OS X equivalent would be

OS X 等价物是

cp -pPR

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 -dwhich I think is the default behavior but I'm not positive.

唯一缺少的是-d,我认为这是默认行为,但我并不积极。

I've never messed with ditto

我从来没有搞过同上

Edit -- @SoloBold

编辑——@SoloBold

-Lfollows symbolic links. -pdoes NOT follow symbolic links. OS X (10.4 at least) has no -doption.

-L跟随符号链接。 -p不遵循符号链接。OS X(至少 10.4)没有-d选项。

that is a huge difference.

这是一个巨大的差异。

回答by joelhardi

Personally I use rsync -a(or whatever rsyncparams 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 rsyncis more efficient than cp.

我个人使用rsync -a(或任何需要的rsync参数)。我的两个原因是:我已经知道如何做到这一点,并且我需要我的脚本能够在 Linux/BSD/Solaris 之间移植。还有一些文件系统rsynccp.

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

对不起,这不是一个直接的答案,我已经ditto在 BSD 上使用过,但没有任何不在手册页中的问题。

回答by Liyan Chang

If you're using ditto, you should be aware that it moves the contents a bit differently from cp -awhen it comes to folders:

如果您使用的是同上,您应该知道它移动内容的方式与cp -a涉及文件夹时的方式略有不同:

ditto foo bar

will move the contents of foo into bar (resulting in bar/file1, bar/file2 .. )

将 foo 的内容移动到 bar (导致 bar/file1, bar/file2 .. )

cp -a foo bar

will move foo/ into bar/ (resulting in bar/foo/file1, bar/foo/file2, .. )

将 foo/ 移动到 bar/ (导致 bar/foo/file1, bar/foo/file2, .. )

Also: OSX cp now support cp -a.

另外:OSX cp 现在支持cp -a.

回答by Liyan Chang

From Linux cp(1):

来自 Linux cp(1):

-a, --archive same as -dpR

-a, --archive 与 -dpR 相同

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

这令人困惑,因为-d 似乎等同于-p。无论如何,OSX 有 -p 和 -R,所以你可以使用它。

回答by wprl

As j04t pointed out that should be cp -pR

正如 j04t 指出的那样,应该是 cp -pR

On OS X, you would do

在 OS X 上,你会做

cp -dRL src target
cp -dRL src target

cp preserves resources in newer version of OS X (was it 10.3 when that happened?)

cp 在较新版本的 OS X 中保留资源(发生这种情况时是 10.3 吗?)

Hey d is kinda like an upsidedown p, right ;)

嘿 d 有点像倒置的 p,对;)

回答by wprl

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!

同上和 cp 之间的区别在于,当源是一个目录时,cp 在目标上创建一个具有该名称的目录,但同上只是复制内容。谨防!