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

例子:

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

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

(cp没有这样的选项)。


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

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

如果以下两个条件都成立:

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

然后你可以用cp的——parents标志来做。从信息页面(可以在http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation或info cp或man cp查看):

——父母 通过追加到目标文件来形成每个目标文件的名称 目录中包含斜杠和源文件的指定名称。的 给' cp'的最后一个参数必须是现有的 目录中。例如,命令: Cp——parents a/b/c existing_dir 复制文件' a/b/c'到' existing_dir/a/b/c',创建any 缺少中间目录。

例子:

/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

这里有一种方法:

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上运行。


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

您可以使用安装程序复制您的文件,并“在飞行中”创建目标路径。

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

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

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

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


这里有一个无耻的链接,链接到我受到启发的答案=)


简短的回答

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

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

这是如何工作的呢?

这里有几个组件,所以我将一步一步地介绍所有语法。

POSIX标准中指定的mkdir实用程序创建目录。根据文档,-p参数将导致mkdir

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

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

&&列表操作符,如POSIX标准(或Bash手册,如果你喜欢)中所述,其效果是只有在mkdir -p /foo/bar成功执行时才执行cp myfile.txt $_。这意味着如果mkdir失败,cp命令将不会尝试执行,原因有很多。

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

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

在本例中,这是我们传递给mkdir的/foo/bar。因此cp命令展开为cp myfile.txt /foo/bar,将myfile.txt复制到新创建的/foo/bar目录中。

注意$_不是POSIX标准的一部分,所以理论上Unix变体可能有一个不支持这个构造的shell。然而,我不知道任何现代shell不支持$_;当然Bash, Dash和zsh都喜欢。


最后注意:我在回答开头给出的命令假设目录名中没有空格。如果你正在处理带有空格的名称,你需要引用它们,这样不同的单词就不会被视为mkdir或cp的不同参数。所以你的命令实际上是这样的:

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

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

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


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 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
}

只需在你的.bashrc中添加以下代码,如果需要可以调整。适用于Ubuntu。

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

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

mkcp test a/b/c/d

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


从源复制到不存在的路径

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

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

Cp -r用于复制所有文件夹及其内容

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


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


比如说你在做

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

希望有帮助!


我尊重上面的答案,我更喜欢使用rsync如下所示:

$  rsync -a directory_name /path_where_to_inject_your_directory/

例子:

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

这对我来说很有用

cp -vaR ./from ./to

I wrote a support script for cp, called CP (note capital letters) that's intended to do exactly this. Script will check for errors in the path you've put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.

all the info here can be accessed by doing CP --help. CP --help-all include's cp's switches. regular cp won't do the copy if it doesn't find the destination path. You don't have such a safety net for typos with CP. You're destination will be created, so if you misspell your destination as /usrr/share/icons or /usr/share/icon well that's what's going to be created. regular cp tends to model it's behavior on the existing path: cp /a/b /c/d will vary on whether d exists or not. if d is an existing folder, cp will copy b into it, making /c/d/b. If d doesn't exist, b will be copied into c and renamed to d. If d exists but is a file and b is a file, it will be overwritten by b's copy. If c doesn't exist, cp doesn't do the copy and exits.

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也是如此。

这种默认CP行为可以通过“——rename”开关改变。在这种情况下,我们假设

"CP——rename /a/b /c/d"是将b复制到/c,并将副本重命名为d。

最后几点注意事项:与cp一样,cp可以一次复制多个项,最后列出的路径被假定为目的地。只要使用引号,它还可以处理带有空格的路径。

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

下面是如何安装它:

在非根终端中,执行以下操作:

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

只是恢复和给出一个完整的工作解决方案,在一行。 如果您想要重命名文件,请注意,您应该包括一种方法来提供到mkdir的干净dir路径。$fdst可以是file或dir。 下一段代码在任何情况下都可以工作。

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}

我也有同样的问题。我的方法是把这些文件压缩成一个存档,就像这样:

tar cf你档案。tar file1 /path/to/file2路径/to/even/deeper/file3

Tar自动将文件存储在归档文件中的适当结构中。如果你跑了

Tar xf your_archive.tar

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


简单的

cp -a * /path/to/dst/

应该能行。


正如上面help_asap和sponge所建议的,你可以使用'install'命令将文件复制到现有目录中,或者如果目标目录不存在,则创建新的目标目录。

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

选项2 安装-D filename -m640 some/deep/directory/filename 按照选项1,但给文件名640权限。

选项3 安装-D filename -m640 -t some/deep/directory/ 根据选项2,但目标文件名到目标目录,所以文件名不需要写在源和目标。

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

它在Ubuntu中工作得很好,将两个步骤(创建目录和复制文件)合并为一个步骤。


虽然已经很晚了,但对新手还是有帮助的。如果你需要自动创建文件夹,rsync应该是你最好的朋友。

rsync /path/to/sourcefile /path/to/tragetdir/thatdoestexist/

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

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


可以在Perl中使用find。命令如下所示:

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不存在,该命令将创建该目录。然后只将文件复制到$t中,除非文件存在于$t中。


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

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

“v”选项表示冗长。

我认为"-R"选项是"递归"

人类对-R的完整描述是:

If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. This option also causes symbolic links to be copied, rather than indirected through, and for cp to create special files rather than copying them as normal files. Created directories have the same mode as the corresponding source directory, unmodified by the process' umask. In -R mode, cp will continue copying even if errors are detected. Note that cp copies hard-linked files as separate files. If you need to preserve hard links, consider using tar(1), cpio(1), or pax(1) instead.

在下面的例子中,我在existingfolder的末尾使用了一个“/”,这样它就会将existingfolder的所有内容(而不是文件夹本身)复制到newfolder中:

cp -Rv existingfolder/  newfolder

试一试。


仅适用于macOS

rsync -R <source file path> destination_folder

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


简单地没有创建脚本和简单的命令…

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

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 / {} \;


许多其他解决方案都不能用于需要转义的文件或文件夹。下面是一个解决方案,它适用于文件和文件夹,并转义空格和其他特殊字符。测试在一个繁忙箱灰壳,没有访问一些花哨的选项。

export file="annoying folder/bar.txt"
export new_parent="/tmp/"

# Creates /tmp/annoying folder/
mkdir -p "$(dirname "$new_folder/$file")"

# Copies file to /tmp/annoying folder/bar.txt
cp -r "$file" "$new_folder/$file"

如果您需要整个文件夹的递归副本而省略了bar.txt,这也可以工作。