我想要一个命令(或者可能是 cp 的一个选项)来创建目标目录(如果它不存在)。
例子:
cp -? file /path/to/copy/file/to/is/very/deep/there
原文由 flybywire 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果以下两个都为真:
cp
的 GNU 版本(而不是例如 Mac 版本),并且然后你可以用 --parents
标志 cp
来做到这一点。从信息页面(可在 http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation 或使用 info cp
或 man cp
):
> --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. > > ``` 例子:
/tmp \( mkdir foo /tmp \) mkdir foo/foo /tmp \( touch foo/foo/foo.txt /tmp \) mkdir bar /tmp \( cp --parents foo/foo/foo.txt bar /tmp \) ls bar/foo/foo foo.txt
”`
原文由 Paul Whipp 发布,翻译遵循 CC BY-SA 3.0 许可协议
7 回答5.3k 阅读
4 回答4k 阅读
2 回答5.9k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
1 回答2.3k 阅读✓ 已解决
2 回答795 阅读✓ 已解决
2 回答3.2k 阅读
(
cp
没有这样的选项)。