Linux:如果目标目录不存在,则复制并创建它

Posted

技术标签:

【中文标题】Linux:如果目标目录不存在,则复制并创建它【英文标题】:Linux: copy and create destination dir if it does not exist 【发布时间】:2010-12-04 12:52:12 【问题描述】:

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

例子:

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

【问题讨论】:

How to have the cp command create any necessary folders for copying a file to a destination 的可能重复项 接受的答案是错误的(cp有这样的选项)。请参阅 Paul Whipp 的第二个答案。 @Ronenz,我同意在 GNU cp 中有一个值得一提的选项(由 Paul Whipp 回答),但它不像 OP 要求的那样通用。它可以将a/foo/bar/file 复制到b/foo/bar/file,但不能将file 复制到b/foo/bar/file。 (即它只能创建在第一个文件的情况下已经存在的父目录,但它不能创建任意目录) 这看起来像是对 cp 的一个不错的功能请求。我们有可能在不久的将来获得这样的命令行开关吗? 【参考方案1】:
mkdir -p "$d" && cp file "$d"

cp 没有这样的选项)。

【讨论】:

我认为你不需要test -d:即使目录已经存在,mkdir -p 仍然会成功。 哎呀,对。但是,test 可能是内置的 bash(特别是如果写成 [[ -d "$d" ]]),而 mkdir 不能;-) mkdir 是 BusyBox 中的内置函数;) @holms, $d 是一个变量,可能包含有问题的目录。我的意思是,如果您必须重复三次,您可能会先将其分配给变量。 @ssasi 不起作用,如果您提供尾随 /,则会说“目录不存在”,如果您不提供并且父目录存在,则复制为预期目录。是递归复制目录,不是创建目录。【参考方案2】:

如果以下两个都为真:

    您使用的是 GNU 版本的 cp(而不是例如 Mac 版本),并且 您正在从一些现有的目录结构复制,您只需要重新创建它

然后您可以使用cp--parents 标志来执行此操作。从信息页面(可通过http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation 或info cpman 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

【讨论】:

这在 Mac OS X 上不起作用,所以我猜它是特定于 Linux 的。 我会说 gnu 特定的。如果你有 macports,你可以安装 coreutils 和它的 gcp 伙计,Linux 拥有一切 我觉得这是一个稍微不同的问题的答案,尽管它是我正在寻找的答案(因此也是我赞成的答案)。我想这是假设您希望目标父目录与原始父目录相同的解决方案,这可能是大多数阅读本文的人感兴趣的用例。 这假设目录'bar'已经存在,但最初的问题暗示了当它作为目标提供并且它不存在时如何自动创建'bar'。 @MichaelKrelin-hacker 提供了这个问题的答案。【参考方案3】:

简答

要将myfile.txt 复制到/foo/bar/myfile.txt,请使用:

mkdir -p /foo/bar && cp myfile.txt $_

这是如何工作的?

这有几个组成部分,所以我将逐步介绍所有语法。

mkdir 实用程序as specified in the POSIX standard 创建目录。根据文档,-p 参数将导致 mkdir

创建任何缺少的中间路径名组件

意味着当调用mkdir -p /foo/bar 时,mkdir 将创建/foo 并且 如果/foo 不存在,则/foo/bar。 (没有-p,它会抛出一个错误。

&& 列表运算符,如POSIX standard(或Bash manual,如果您愿意)中所述,其效果是cp myfile.txt $_ 只有在mkdir -p /foo/bar 成功执行时才会执行。这意味着如果mkdir 对one of the many reasons it might fail 失败,cp 命令将不会尝试执行。

最后,我们作为第二个参数传递给cp$_ 是一个“特殊参数”,它可以方便地避免重复长参数(如文件路径),而无需将它们存储在变量中。根据the Bash manual,它:

扩展到上一个命令的最后一个参数

在这种情况下,这就是我们传递给mkdir/foo/bar。因此cp 命令扩展为cp myfile.txt /foo/bar,它将myfile.txt 复制到新创建的/foo/bar 目录中。

注意$_not part of the POSIX standard,所以理论上一个 Unix 变体可能有一个不支持这种结构的 shell。但是,我不知道任何不支持$_ 的现代shell;当然 Bash、Dash 和 zsh 都可以。


最后一点:我在此答案开头给出的命令假定您的目录名称中没有空格。如果您正在处理带有空格的名称,则需要引用它们以便不同的词不会被视为mkdircp 的不同参数。所以你的命令实际上看起来像:

mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"

【讨论】:

旁白:我通常对 Stack Overflow 上有关 Bash 或 Unix shell 命令的问题缺乏详细信息感到失望,这些命令经常抛出一个复杂且极其密集的单行代码,如果你很难解开'还不是 Unix 向导。我在这里尝试了相反的极端,并且都解释了语法的每个细节并链接到适当的位置以查找有关这些东西如何工作的文档。我希望这至少对某些人有所帮助。另外,应得的功劳:我从这个回答重复的问题中剔除了这种方法:***.com/a/14085147/1709587 我以前从未见过 $_。它指的是什么?最后一个命令中使用的文件夹? @thebunnyrules 但是...但是...在我的回答中有一个 “这是如何工作的?” 部分,其中包含多个专门针对您刚才提出的问题的段落问。感觉被忽视和不被爱。 :( 我刚刚做了mkdir -p /foo/bar && touch $_/myfile.txt 在一个不存在的目录中创建一个新文件。像魅力一样工作。 次要细节:也可以:mkdir -p /foo/bar && cp ../from/another/dir/myfile.txt $_/my-file-with-new-name跨度> 【参考方案4】:

这么老的问题,但也许我可以提出一个替代解决方案。

您可以使用install 程序来复制您的文件并“即时”创建目标路径。

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

不过,有一些方面需要考虑:

    您还需要指定目标文件名,而不仅仅是目标路径 目标文件将是可执行的(至少,就我的测试而言)

您可以通过添加-m 选项来轻松修改#2 以设置目标文件的权限(例如:-m 664 将创建具有rw-rw-r-- 权限的目标文件,就像使用@987654327 创建一个新文件一样@)。


这里是shameless link to the answer I was inspired by =)

【讨论】:

请注意,此命令创建具有755 (rwxr-xr-x) 权限的目标文件,这可能不是我们所希望的。您可以使用-m 开关指定其他内容,但我找不到仅保留文件权限的方法:( 确保正确的文件所有权/掩码:install -D /path/to/source /path/to/dest/dir -o some-user -m 0754【参考方案5】:

Shell 函数可以做你想做的事,称它为“埋葬”副本,因为它为文件挖了一个洞:

bury_copy()  mkdir -p `dirname $2` && cp "$1" "$2"; 

【讨论】:

dirname $2 也应该有引号 @Kalmi,对于正确的引用,您还需要引用 $2 作为 dirname 的参数。喜欢mkdir -p "$(dirname "$2")"。如果没有在cp 中引用$2 是没用的;) 请注意,此答案假设 $2 还不是目标目录,否则您只需要 "mkdir -p "$2" && cp "$1" "$2" 作为函数体 我认为这有一个错误,正如@user467257 指出的那样。我认为这是不可修复的,因为在这种情况下,目标目录和目标文件之间的 $2 是不明确的。我已经发布了一个替代答案来解决这个问题。 @Rich,我不同意这是不可修复的。以下对文件和目录都适用: mkdir -p dirname "$2" && cp -r "$1" "$2";请注意,dirname $2 周围的反引号不会显示,因为 SO 将它们解析为代码标记。【参考方案6】:

这是一种方法:

mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \
   && cp -r file /path/to/copy/file/to/is/very/deep/there

dirname 将为您提供目标目录或文件的父级。然后 mkdir -p `dirname ...` 将创建该目录,以确保在您调用 cp -r 时正确的基本目录就位。

与 --parents 相比,它的优势在于它适用于目标路径中的最后一个元素是文件名的情况。

它可以在 OS X 上运行。

【讨论】:

【参考方案7】:

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

【讨论】:

其实一点都不一样。他要求您将文件名传递两次(因此是“-t”标志)并在文件上设置执行位(因此是“-m”示例)。他应该是最佳答案,因为它实际上并没有回答问题 - 而我的答案。 这适用于单个文件。请注意 there 是最终文件而不是最终子目录。【参考方案8】:

尽管我很尊重上述答案,但我更喜欢使用 rsync,如下所示:

$  rsync -a directory_name /path_where_to_inject_your_directory/

示例:

$ rsync -a test /usr/local/lib/

【讨论】:

我试过了,结果如下: rsync -a Bull /some/hoop/ti/doop/ploop RESULT rsync: mkdir "/some/hoop/ti/doop/ploop" failed: No such文件或目录 (2) rsync 错误:main.c(675) [Receiver=3.1.2] 处的文件 IO(代码 11)中的错误 [Receiver=3.1.2] @thebunnyrules 您必须使用 pwd 命令检查路径(查看:[link] (access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/…))并将其正确插入到您的指令中 如果我正确理解您的建议,您建议我在我的命令中指定源代码的完整路径,也就是使用:“$(pwd)/bull”,而不仅仅是文件名:bull?问题是,错误在于 rsync 创建目标目录,而不是源(公牛)。在我看来 rsync 使用简单的 mkdir 而不是 mkdir -p。 rsynccp 更容易预测。例如,如果我希望cp /from/somedir /to-dir,那么如果/to-dir 已经存在,cp 的行为会有所不同:如果/to-dir 存在,那么cp 将创建/to-dir/some-dir,否则只有/from/somedir 的内容被放入@987654332 @ 。但是,rsync 的行为相同,即 /to-dir/some-dir总是被创建。【参考方案9】:

只需在一行中恢复并提供完整的工作解决方案。 如果你想重命名你的文件要小心,你应该包括一种方法来为 mkdir 提供一个干净的 dir 路径。 $fdst 可以是文件或目录。 在任何情况下,下一个代码都应该可以工作。

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p $(dirname $fdst) && cp -p $fsrc $fdst

或 bash 特定

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p $fdst%/* && cp -p $fsrc $fdst

【讨论】:

谢谢!这就是我需要的。在我的情况下,我不知道目标是否有目录以及目录是否存在,所以这段代码给了我父目录,如果它不存在,我可以安全地创建它【参考方案10】:

如上面 help_asap 和海绵人建议的那样,您可以使用“安装”命令将文件复制到现有目录或创建新的目标目录(如果它们尚不存在)。

选项 1 install -D filename some/deep/directory/filename 将文件复制到新目录或现有目录并赋予文件名默认 755 权限

选项 2 install -D filename -m640 some/deep/directory/filename 根据选项 1,但提供文件名 640 权限。

选项 3 install -D filename -m640 -t some/deep/directory/ 根据选项 2,但将文件名定位到目标目录,因此文件名不需要同时写入源和目标。

选项 4 install -D filena* -m640 -t some/deep/directory/ 根据选项 3,但对多个文件使用通配符。

它在 Ubuntu 中运行良好,并将两个步骤(创建目录然后复制文件)合并为一个步骤。

【讨论】:

【参考方案11】:

只需在您的 .bashrc 中添加以下内容,如果您需要进行调整。在 Ubuntu 中工作。

mkcp() 
    test -d "$2" || mkdir -p "$2"
    cp -r "$1" "$2"

例如 如果要将“test”文件复制到目标目录“d” 使用,

mkcp test a/b/c/d

mkcp 将首先检查目标目录是否存在,如果不存在则创建并复制源文件/目录。

【讨论】:

正如其他人对其他答案的评论,您无需在调用mkdir -p 之前测试目录是否存在。即使它已经存在,它也会成功。【参考方案12】:

这对我有用

cp -vaR ./from ./to

【讨论】:

同意。在man cp-R If source_file designates a directory, cp copies the directory and the entire subtree connected at that point.【参考方案13】:

我为 cp 编写了一个支持脚本,称为 CP(注意大写字母),旨在完成此操作。脚本将检查您输入的路径中的错误(最后一个是目标路径除外),如果一切正常,它将执行 mkdir -p 步骤以在开始复制之前创建目标路径。此时,常规 cp 实用程序将接管,并且您与 CP 一起使用的任何开关(如 -r、-p、-rpL 都直接通过管道传输到 cp)。在您使用我的脚本之前,您需要了解一些事项。

这里的所有信息都可以通过 CP --help 访问。 cp --help-all 包含cp的开关。 如果找不到目标路径,常规 cp 将不会进行复制。对于 CP 的错别字,您没有这样的安全网。您的目的地将被创建,因此如果您将目的地拼错为 /usr/share/icons 或 /usr/share/icon ,那么这就是将要创建的内容。 常规 cp 倾向于在现有路径上对其行为进行建模:cp /a/b /c/d 将根据 d 是否存在而有所不同。如果 d 是现有文件夹,则 cp 会将 b 复制到其中,生成 /c/d/b。如果 d 不存在,则 b 将被复制到 c 中并重命名为 d。如果 d 存在但是是一个文件并且 b 是一个文件,它将被 b 的副本覆盖。如果 c 不存在,则 cp 不进行复制并退出。

CP 没有从现有路径中获取线索的奢侈,因此它必须有一些非常坚定的行为模式。 CP 假定您正在复制的项目被放置在目标路径中,而不是目标本身(也就是源文件/文件夹的重命名副本)。含义:

如果 d 是文件夹,“CP /a/b /c/d”将导致 /c/d/b 如果 /c/b 中的 b 是文件夹,“CP /a/b /c/b”将导致 /c/b/b。 如果 b 和 d 都是文件:CP /a/b /c/d 将导致 /c/d(其中 d 是 b 的副本)。 CP /a/b /c/b 在同样的情况下也是如此。

可以使用“--rename”开关更改此默认 CP 行为。在这种情况下,假设

“CP --rename /a/b /c/d”将 b 复制到 /c 并将副本重命名为 d。

一些结束说明:与 cp 一样,CP 可以一次复制多个项目,并假定列出的最后一个路径是目标。只要您使用引号,它也可以处理带有空格的路径。

CP 将检查您输入的路径并确保它们在复制之前存在。在严格模式下(可通过 --strict 开关获得),所有被复制的文件/文件夹必须存在,否则不会发生复制。在放松模式 (--relaxed) 中,如果您列出的至少一项存在,则复制将继续。放松模式是默认模式,您可以通过开关临时更改模式,也可以通过在脚本开头设置变量 easy_going 来永久更改模式。

安装方法如下:

在非 root 终端中,执行:

sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP 

在 gedit 中,粘贴 CP 实用程序并保存:

#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.

#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.

#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d. 

#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"

err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"


  help="===============================================================================\
    \n         CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
    \n===============================================================================\n
    \n This script (CP, note capital letters) is intended to supplement \
    \n your system's regular cp command (note uncapped letters). \n
    \n Script's function is to check if the destination path exists \
    \n before starting the copy. If it doesn't it will be created.\n    
    \n To make this happen, CP assumes that the item you're copying is \
    \n being dropped in the destination path and is not the destination\
    \n itself (aka, a renamed copy of the source file/folder). Meaning:\n 
    \n * \"CP /a/b /c/d\" will result in /c/d/b \
    \n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
    \n   resulting in /c/b/b. \n
    \n Of course, if /c/b or /c/d are existing files and /a/b is also a\
    \n file, the existing destination file will simply be overwritten. \
    \n This behavior can be changed with the \"--rename\" switch. In this\
    \n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c  \
    \n and renaming the copy to d.\n
    \n===============================================================================\
    \n        CP specific help: Switches and their Usages \
    \n===============================================================================\n
    \
    \n  --rename\tSee above. Ignored if copying more than one item. \n
    \n  --quiet\tCP is verbose by default. This quiets it.\n
    \n  --strict\tIf one+ of your files was not found, CP exits if\
    \n\t\tyou use --rename switch with multiple items, CP \
    \n\t\texits.\n
    \n  --relaxed\tIgnores bad paths unless they're all bad but warns\
    \n\t\tyou about them. Ignores in-appropriate rename switch\
    \n\t\twithout exiting. This is default behavior. You can \
    \n\t\tmake strict the default behavior by editing the \
    \n\t\tCP script and setting: \n
    \n\t\teasy_going=false.\n
    \n  --help-all\tShows help specific to cp (in addition to CP)."

cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
    \nHere is the help out of the original cp command... \
    \n\n===============================================================================\
    \n          cp specific help: \
    \n===============================================================================\n"

outro1="\n******************************************************************************\
    \n******************************************************************************\
    \n******************************************************************************\
    \n        USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
    \n******************************************************************************\
    \n******************************* HIT q TO EXIT ********************************\
    \n******************************************************************************"


#count and classify arguments that were inputed into script, output help message if needed
while true; do
    eval input="\$$n"
    in_=$input::1

    if [ -z "$input" -a $n = 1 ]; then input="--help"; fi 

    if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
        if [ "$input" = "--help-all" ]; then 
            echo -e "$help"$cp_hlp > /tmp/cp.hlp 
            cp --help >> /tmp/cp.hlp
            echo -e "$outro1" >> /tmp/cp.hlp
            cat /tmp/cp.hlp|less
            cat /tmp/cp.hlp
            rm /tmp/cp.hlp
        else
            echo -e "$help" "$outro1"|less
            echo -e "$help" "$outro1"
        fi
        exit
    fi

    if [ -z "$input" ]; then
        count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
        break 
    elif [ "$in_" = "-" ]; then
        count_s=$(expr $count_s + 1 )
    else
        count_i=$(expr $count_i + 1 )
    fi
    n=$(expr $n + 1)
done

#error condition: no items to copy or no destination
    if [ $count_i -lt 0 ]; then 
            echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
    elif [ $count_i -lt 1 ]; then
            echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
    fi

#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
        eval input="\$$n" #input=$1,$2,$3,etc...
        in_=$input::1 #first letter of $input

        if [ "$in_" = "-" ]; then
            if [ "$input" = "--rename" ]; then 
                rename=true #my custom switches
            elif [ "$input" = "--strict" ]; then 
                easy_going=false #exit script if even one of the non-destinations item is not found
            elif [ "$input" = "--relaxed" ]; then 
                easy_going=true #continue script if at least one of the non-destination items is found
            elif [ "$input" = "--quiet" ]; then 
                verbal=""
            else
                #m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
                switch_list="$switch_list \"$input\""
            fi                                  
        elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
                i=$(expr $i + 1)
                if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then 
                    err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
                else
                    if [ "$i" -le "$count_i" ]; then 
                        eval item$i="$input" 
                        item_list="$item_list \"$input\""
                    else
                        destination="$input" #destination is last items entered
                    fi
                fi
        else
            i=0
            m=0
            n=1                     
            break
        fi      
        n=$(expr $n + 1)
done

#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then 
    echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
    echo -e "Bad Paths: $err $error_list"
    exit
fi

if [ $err = $count_i ]; then
    echo "ALL THE PATHS you have entered are incorrect! Exiting."
    echo -e "Bad Paths: $err $error_list"
fi

#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.

#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.

#ERROR CONDITIONS: 
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.

if [ -f "$destination" ]; then
    if [ $count_i -gt 1 ]; then 
        echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
    elif [ -d "$item1" ]; then
        echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
    fi
fi
if [ "$rename" = true ]; then
    if [ $count_i -gt 1 ]; then
        if [ $easy_going = true ]; then
            echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
        else
            echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
        fi
    elif [ -d "$destination" -a -f "$item1" ]; then
        echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
        if [ $easy_going = true ]; then
            echo "Ignoring Rename option. Continuing."
        else
            echo "Script running in strict mode. Exiting."; exit
        fi
    else
        dest_jr=$(dirname "$destination")
        if [ -d "$destination" ]; then item_list="$item1/*";fi
        mkdir -p "$dest_jr"
    fi
else
    mkdir -p "$destination"
fi

eval cp $switch_list $verbal $item_list "$destination"

cp_err="$?"
if [ "$cp_err" != 0 ]; then 
    echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else 
    echo "Copy operation exited with no errors."
fi

exit

【讨论】:

这可能有点矫枉过正,但效率很高,按预期工作,谢谢您的好先生。 过度工程是最好的。【参考方案14】:

cp 有多种用法:

$ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

@AndyRoss 的回答适用于

cp SOURCE DEST

cp 的风格,但是如果你使用

cp SOURCE... DIRECTORY/

cp的风格。

我认为“DEST”在这种用法中没有尾随斜杠是模棱两可的(即目标目录尚不存在的地方),这也许是cp从未为此添加选项的原因。

所以这是我的这个函数的版本,它在 dest 目录上强制使用斜杠:

cp-p() 
  last=$@: -1

  if [[ $# -ge 2 && "$last" == */ ]] ; then
    # cp SOURCE... DEST/
    mkdir -p "$last" && cp "$@"
  else
    echo "cp-p: (copy, creating parent dirs)"
    echo "cp-p: Usage: cp-p SOURCE... DEST/"
  fi

【讨论】:

【参考方案15】:

刚刚遇到同样的问题。我的方法是将文件压缩成这样的存档:

tar cf your_archive.tar file1 /path/to/file2 path/to/even/deeper/file3

tar 自动将文件存储在归档中的适当结构中。如果你运行

tar xf your_archive.tar

文件被提取到所需的目录结构中。

【讨论】:

【参考方案16】:

这已经很晚了,但它可能会对某个地方的新手有所帮助。如果您需要“自动”创建文件夹,rsync 应该是您最好的朋友。 rsync /path/to/sourcefile /path/to/tragetdir/thatdoestexist/

【讨论】:

【参考方案17】:

我强烈建议ditto。 刚刚好。

ditto my/location/poop.txt this/doesnt/exist/yet/poop.txt

【讨论】:

应该指出,大多数 Linux 发行版都没有提供同上。 我不知道为什么这个回复只有 3 票 atm,而不是 500+ 谢谢@ulf-gjerdingen 的简单直接的回答。【参考方案18】:

无需创建脚本和简单命令即可...

mkdir -p /destination-folder/ && cp file-name /destination-folder/

【讨论】:

【参考方案19】:

从源复制到不存在的路径

mkdir –p /destination && cp –r /source/ $_

注意:此命令复制所有文件

cp –r 用于复制所有文件夹及其内容

$_ 作为在最后一个命令中创建的目标工作

【讨论】:

【参考方案20】:
rsync file /path/to/copy/file/to/is/very/deep/there

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

【讨论】:

这对我不起作用——我在 dest dir 上得到“没有这样的文件或目录”【参考方案21】:

您可以将findPerl 一起使用。命令会是这样的:

find file | perl -lne '$t = "/path/to/copy/file/to/is/very/deep/there/"; /^(.+)\/.+$/; `mkdir -p $t$1` unless(-d "$t$1"); `cp $_ $t$_` unless(-f "$t$_");'

如果目录$t 不存在,此命令将创建它。而不是将file 复制到$t 中,除非file 存在于$t 中。

【讨论】:

【参考方案22】:

这适用于 MacOS 上的 GNU /bin/bash 3.2 版(在 Catalina 和 Big Sur 上测试)

cp -Rv <existing-source-folder>/   <non-existing-2becreated-destination-folder>

“v”选项用于详细说明。

我认为“-R”选项是“递归”。

man对-R的完整描述是:

如果 source_file 指定了一个目录,则 cp 复制该目录和在该点连接的整个子树。如果 source_file 以 / 结尾,则复制目录的内容而不是目录本身。此选项还导致复制符号链接,而不是间接通过,并让 cp 创建特殊文件而不是将它们复制为普通文件。创建的目录与对应的源目录具有相同的模式,不被进程'umask修改。

在-R模式下,即使检测到错误,cp也会继续复制。

请注意,cp 将硬链接文件复制为单独的文件。如果您需要保留硬链接,请考虑使用 tar(1)、cpio(1) 或 pax(1)。

在下面的示例中,我在现有文件夹的末尾使用了“/”,以便它将现有文件夹的所有内容(而不是文件夹本身)复制到新文件夹中:

cp -Rv existingfolder/  newfolder

试试看。

【讨论】:

【参考方案23】:

仅适用于 macOS

rsync -R <source file path> destination_folder

对于 ma​​cOS --parents 选项 cp 不起作用

【讨论】:

【参考方案24】:

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

set +H; echo -e "#!/bin/sh\nmkdir -p \$(dirname \"\$2\"); cp \"\$1\" \"$2\"\;" &gt; ~/local/bin/cpmkdir; chmod +x ~/local/bin/cpmkdir

然后你可以像这样使用它:

find -name files_you_re_lookin_for.* -exec cpmkdir ../extracted_copy/ \;

【讨论】:

【参考方案25】:

假设你正在做类似的事情

cp file1.txt A/B/C/D/file.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

希望有帮助!

【讨论】:

【参考方案26】:

简单

cp -a * /path/to/dst/

应该可以解决问题。

【讨论】:

它没有。 '/path/to/dst/' 必须事先存在。 这是不好的做法,不应该使用。

以上是关于Linux:如果目标目录不存在,则复制并创建它的主要内容,如果未能解决你的问题,请参考以下文章

linux学习13 Linux运维常用文件管理命令及系统变量基础

将源目录复制到两个目标目录的 Bash 脚本,如果成功,则验证并删除源

linux学习

Linux复制文件到某路径并重命名

Linux复制文件到某路径并重命名

如果 PHP 中不存在,则复制文件并创建目录 [重复]