Linux 如何让 cp 命令创建任何必要的文件夹以将文件复制到目标

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

How to have the cp command create any necessary folders for copying a file to a destination

linuxbashcp

提问by omg

When copying a file using cpto a folder that may or may not exist, how do I get cpto create the folder if necessary? Here is what I have tried:

将文件复制cp到可能存在或可能不存在cp的文件夹时,如果需要,如何创建文件夹?这是我尝试过的:

[root@file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory

回答by Christian

I didn't know you could do that with cp.

我不知道你可以用 cp 做到这一点。

You can do it with mkdir ..

你可以用 mkdir ..

mkdir -p /var/path/to/your/dir

EDITSee lhunath's answerfor incorporating cp.

编辑请参阅 lhunath对合并 cp的回答

回答by victor hugo

There is no such option. What you can do is to run mkdir -pbefore copying the file

没有这样的选择。您可以做的是在复制文件之前运行mkdir -p

I made a very coolscript you can use to copy files in locations that doesn't exist

我制作了一个非常酷的脚本,您可以使用它在不存在的位置复制文件

#!/bin/bash
if [ ! -d "" ]; then
    mkdir -p ""
fi
cp -R "" ""

Now just save it, give it permissions and run it using

现在只需保存它,授予它权限并使用它运行它

./cp-improved SOURCE DEST

I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you

我放了 -R 选项,但它只是一个草稿,我知道它可以,你会在很多方面改进它。希望对你有帮助

回答by Jeff King

One can also use the command find:

还可以使用以下命令find

find ./ -depth -print | cpio -pvd newdirpathname

回答by eythort

For those that are on Mac OSX, perhaps the easiest way to work around this is to use ditto(only on the mac, AFAIK, though). It will create the directory structure that is missing in the destination.

对于那些在 Mac OSX 上的人来说,解决这个问题的最简单方法可能是使用同上(但仅在 mac 上,AFAIK)。它将创建目标中缺少的目录结构。

For instance, I did this

例如,我做了这个

ditto 6.3.2/6.3.2/macosx/bin/mybinary ~/work/binaries/macosx/6.3.2/

where ~/workdid not contain the binaries directory before I ran the command.

~/work在我运行命令之前, where不包含二进制文件目录。

I thought rsync should work similarly, but it seems it only works for one level of missing directories. That is,

我认为 rsync 应该类似地工作,但它似乎只适用于一级丢失的目录。那是,

rsync 6.3.3/6.3.3/macosx/bin/mybinary ~/work/binaries/macosx/6.3.3/

worked, because ~/work/binaries/macosx existed but not ~/work/binaries/macosx/6.3.2/

工作,因为 ~/work/binaries/macosx 存在但不存在 ~/work/binaries/macosx/6.3.2/

回答by lhunath

To expand upon Christian's answer, the only reliable way to do this would be to combine mkdirand cp:

为了扩展 Christian 的答案,唯一可靠的方法是结合mkdircp

mkdir -p /foo/bar && cp myfile "$_"

As an aside, when you only need to create a single directory in an existing hierarchy, rsynccan do it in one operation. I'm quite a fan of rsyncas a much more versatile cpreplacement, in fact:

顺便说一句,当您只需要在现有层次结构中创建单个目录时,rsync可以通过一个操作来完成。事实上,我非常喜欢rsync作为一个更通用的cp替代品:

rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't.  bar is created.

回答by pedro

cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory

It will create no existing paths in destination, if path have a source file inside. This dont create empty directories.

如果路径中有源文件,它将不会在目标中创建现有路径。这不会创建空目录。

A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.

刚才我看到 xxxxxxxx: No such file or directory, 因为我用完了可用空间。没有错误信息。

with ditto:

同上:

ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device

once freed space cp -Rvn /source/path/* /destination/path/works as expected

一旦释放空间cp -Rvn /source/path/* /destination/path/按预期工作

回答by Chu-Siang Lai

rsync is work!

rsync 是工作!

#file:
rsync -aqz _vimrc ~/.vimrc

#directory:
rsync -aqz _vim/ ~/.vim

回答by Jamie McCrindle

 mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt