macos mac 上的 cp --parents 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11246070/
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 09:17:08  来源:igfitidea点击:

cp --parents option on mac

macoscommand-linecommand-line-arguments

提问by dmonopoly

On Linux, I have a --parents option available for the cp command so I can do

在 Linux 上,我有一个可用于 cp 命令的 --parents 选项,所以我可以这样做

cp --parents test/withintest/go.rb test2

http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html

http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html

On Mac, I do not have this option available. Is there a way to do this on Mac? Why is this option not available?

在 Mac 上,我没有此选项可用。有没有办法在 Mac 上做到这一点?为什么这个选项不可用?

PS. The purpose of --parents is the following:

附注。--parents 的目的如下:

‘--parents' Form the name of each destination file by appending to the target directory a slash and the specified name of the source file.

The last argument given to cp must be the name of an existing directory.

For example, the command:

      cp --parents a/b/c existing_dir 

copies the file a/b/c to existing_dir/a/b/c, creating any missing intermediate directories.

'--parents' 通过将斜杠和源文件的指定名称附加到目标目录来形成每个目标文件的名称。

给 cp 的最后一个参数必须是现有目录的名称。

例如,命令:

      cp --parents a/b/c existing_dir 

将文件 a/b/c 复制到 existing_dir/a/b/c,创建任何缺少的中间目录。

采纳答案by Echelon

You can use the dittocommand on Mac OS X:

您可以ditto在 Mac OS X 上使用该命令:

The basic form

基本形式

ditto <src-path> <dst-path>

does what you want. There's a lot more options too - check out the man page.

做你想做的。还有更多选项 - 查看手册页

回答by Stephan

This bothered me quite a lot as well. A workaround for this could be to use rsync.

这也让我非常困扰。对此的解决方法可能是使用 rsync。

rsync -R test/withintest/go.rb test2

has the same effect as cp --parentsand OS X comes standard with rsync.

具有cp --parents与 OS X 标配 rsync相同的效果。

回答by Kara

You can install the GNU version of cpusing MacPorts.

您可以cp使用MacPorts安装 GNU 版本。

After MacPorts is installed you can install the coreutils packages:

安装 MacPorts 后,您可以安装 coreutils 包:

sudo port install coreutils

Then you will be able to use the GNU version cpand other core utilitites (ls, date, cat, etc.) by prefixing the command with a g:

然后,你就可以使用GNU版本cp和其他核心utilitites( ,,通过附加了一个命令等):lsdatecatg

gcp --parents test/withintest/go.rb test2

If you want these GNU versions to be used by default you can add the GNU bin update your path. Add the following to your ~/.bash_profile:

如果您希望默认使用这些 GNU 版本,您可以添加 GNU bin 更新您的路径。将以下内容添加到您的~/.bash_profile:

export PATH="/opt/local/libexec/gnubin:$PATH"

回答by vikrantt

I would not replace mac cp with GNU cp. I would also not used ditto because it is not cross-platform. Instead use cross-platform tools, such as rsync:

我不会用 GNU cp 替换 mac cp。我也不会使用同上,因为它不是跨平台的。而是使用跨平台工具,例如 rsync:

rsync <srcDir/srcFile> <dst>

Result: dst/srcDir/srcFile

结果:dst/srcDir/srcFile

回答by Martin Peter

The Homebrew way:

自制方式:

Install coreutils

安装 coreutils

brew install coreutils

Use the GNUg- prefixed command

使用GNUg- 前缀命令

gcp --parents test/withintest/go.rb test2