如何拥有cp命令创建任何必要的文件夹复制文件到目的地 [英] How to have the cp command create any necessary folders for copying a file to a destination

查看:1414
本文介绍了如何拥有cp命令创建任何必要的文件夹复制文件到目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用复制文件 CP 来,可能会或可能不存在的文件夹,我怎么让 CP 如有必要,创建该文件夹?以下是我曾尝试:

  [根@文件的Nutch-0.9]#CP -f网址 - 恢复/nosuchdirectory/hi.txt
CP:不能创建常规文件`/nosuchdirectory/hi.txt':没有这样的文件或目录


解决方案

如果以下两个条件都为真:


  1. 您正在使用 CP 的GNU版本(而不是,例如,Mac版)和

  2. 您是从一些现有的目录结构复制,你只需要重新创建

CP 的父母标记 -

那么你可以使用做到这一点。从信息页面(可视范围在<一个href=\"http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation\">http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation或信息CP 男人CP


- 父母
     通过附加到目标形成每个目标文件的名称
     目录斜杠和源文件的指定名称。该
     给予'CP'最后一个参数必须是现有的名称
     目录。例如,命令:          CP --parents A / B / C existing_dir     拷贝文件'A / B / C'为'existing_dir / A / B / C,创建任何
     缺少的中间目录。


例如:

/ tmp目录$的mkdir富
/ tmp目录$的mkdir富/富
/ tmp目录$触摸富/富/ foo.txt的
/ tmp目录$的mkdir酒吧
/ tmp目录$ CP --parents富/富/ foo.txt的酒吧
/ tmp目录$ LS酒吧/富/富
foo.txt的

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

[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

解决方案

If both of the following are true:

  1. You are using the GNU version of cp (and not, for instance, the Mac version), and
  2. You are copying from some existing directory structure and you just need it recreated

then you can do this with the --parents flag of cp. From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or 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.

Example:

/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

这篇关于如何拥有cp命令创建任何必要的文件夹复制文件到目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆