我需要一个命令(可能是cp的一个选项)来创建目标目录(如果目标目录不存在)。

例子:

cp -? file /path/to/copy/file/to/is/very/deep/there

当前回答

install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there

其他回答

rsync file /path/to/copy/file/to/is/very/deep/there

如果您有正确的rsync类型,这可能会起作用。

比如说你在做

cp file1.txt A/B/C/D/文件txt

其中A/B/C/D是还不存在的目录

一个可能的解决方案如下

DIR=$(dirname A/B/C/D/file.txt)
# DIR= "A/B/C/D"
mkdir -p $DIR
cp file1.txt A/B/C/D/file.txt

希望有帮助!

仅适用于macOS

rsync -R <source file path> destination_folder

对于macOS -父母cp选项不工作

我强烈建议你也这么做。 只是工作。

同样,my/location/poo .txt this/doesn /exist/yet/poo .txt

Oneliner创建一个小脚本,可以用作子命令,在find例如:

组+ H;Echo -e "#!/bin/sh\nmkdir -p \$(dirname \"\$2\");Cp \"$ 1\" \"$2\"\;"> ~ /地方/ bin / cpmkdir;Chmod +x ~/local/bin/cpmkdir . sh

然后你可以这样使用它:

查找name files_you_re_lookin_for。* -exec cpmkdir {} ./ extracted_copy / {} \;