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

时间:2009-10-07 06:55:27

标签: linux bash shell unix cp

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

示例:

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

23 个答案:

答案 0 :(得分:233)

mkdir -p "$d" && cp file "$d"

cp)没有这样的选项。

答案 1 :(得分:202)

如果同时满足以下两个条件:

  1. 您使用的是cp的GNU版本(例如,不是Mac版本),
  2. 您正在从一些现有的目录结构中复制,只需重新创建
  3. 然后您可以使用--parents的{​​{1}}标记执行此操作。从信息页面(可在http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocationcpinfo 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.
    

答案 2 :(得分:40)

简答

要将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/bar如果/foo没有&#39 ; t已经存在。 (如果没有-p,则会抛出错误。

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

最后,我们作为$_的第二个参数传递的cp是"特殊参数"这对于避免重复长参数(如文件路径)而不必将它们存储在变量中非常方便。每the Bash manual,它:

  

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

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

请注意/foo/barnot part of the POSIX standard,因此从理论上讲,Unix变体可能有一个不支持此构造的shell。但是,我不知道任何不支持$_的现代贝壳;当然Bash,Dash和zsh都这样做。

最后一点:我在本回答开头给出的命令假设您的目录名称中没有空格。如果您要处理带空格的名称,那么您就是&#39 ;我需要引用它们,以便不同的单词不被视为$_mkdir的不同参数。所以你的命令实际上看起来像:

cp

答案 3 :(得分:32)

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

您可以使用install程序复制文件并快速创建目标路径"。

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

但有些方面需要考虑:

  1. 您需要同时指定目标文件名,而不仅是目标路径
  2. 目标文件将是可执行的(至少,就我从测试中看到的那样)
  3. 您可以通过添加-m选项轻松修改#2以设置目标文件的权限(例如:-m 664将创建具有权限rw-rw-r--的目标文件,就像创建包含touch)的新文件。


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

答案 4 :(得分:15)

Shell函数可以执行您想要的操作,将其称为“埋葬”副本,因为它为文件挖掘了一个漏洞:

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

答案 5 :(得分: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时,正确的基本目录就位。

这种过分父母的优势在于,它适用于目标路径中最后一个元素是文件名的情况。

它可以在OS X上运行。

答案 6 :(得分:5)

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

答案 7 :(得分:2)

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

答案 8 :(得分:2)

我尊重上述答案,我更喜欢使用rsync:

$  rsync -a directory_name /path_where_to_inject_your_directory/

示例:

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

答案 9 :(得分:1)

只需在.bashrc中添加以下内容,根据需要进行调整。适用于Ubuntu。

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

E.g 如果要将'test'文件复制到目标目录'd' 使用,

mkcp test a/b/c/d

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

答案 10 :(得分:1)

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
}

答案 11 :(得分:1)

这适合我

cp -vaR ./from ./to

答案 12 :(得分:1)

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

  • 这里的所有信息都可以通过CP --help来访问。 CP --help-all包含了cp的开关。
  • 如果没有找到目的地路径,则常规cp不会复制该副本。你没有CP的拼写错误的安全网。您的目的地将被创建,因此如果您错误地将目的地拼写为/ usrr / 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假定您正在复制的项目正在目标路径中被删除,而不是目标本身(也就是源文件/文件夹的重命名副本)。含义:

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

可以使用" - 重命名"更改此默认CP行为。开关。在这种情况下,它假定

  • " CP --rename / a / b / c / d"正在将b复制到/ c并将副本重命名为d。

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

CP将检查您输入的路径,并确保它们在执行复制之前存在。在严格模式下(通过--strict开关可用),所有要复制的文件/文件夹必须存在或不进行复制。在放松模式( - 放松)下,如果您列出的项目中至少有一个存在,则复制将继续。轻松模式是默认模式,您可以通过开关临时更改模式,也可以通过在脚本开头设置变量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

答案 13 :(得分:1)

只需一行即可恢复并给出完整的工作解决方案。 如果要重命名文件,请当心,应该包括一种提供干净目录路径到mkdir的方法。 $ 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}

答案 14 :(得分:1)

只是有同样的问题。我的方法是将文件像这样存档:

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

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

tar xf your_archive.tar

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

答案 15 :(得分:0)

您可以将 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 中。

答案 16 :(得分:0)

我强烈建议ditto。 可以。

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

答案 17 :(得分:0)

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

MySQL

“v”选项用于详细。

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

man 对 -R 的完整描述是:

<块引用>

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

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

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

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

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

试试吧。

答案 18 :(得分:0)

如上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中运行良好,将两个步骤(目录创建然后是文件复制)组合为一个步骤。

答案 19 :(得分:0)

简单

cp -a * /path/to/dst/

应该可以解决问题。

答案 20 :(得分:0)

从源复制到非现有路径

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

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

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

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

答案 21 :(得分:0)

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

如果你有合适的rsync

,这可能会有用

答案 22 :(得分:-1)

让我们说你正在做类似

的事情
  

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
希望有所帮助!

相关问题