Linux系统:复制和创建目标目录。如果它不存在(Linux: copy and create de

2019-07-21 05:25发布

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

例:

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

Answer 1:

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

(有没有为这样的选项cp )。



Answer 2:

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

  1. 您正在使用的GNU版本的cp (而不是,例如,Mac版本),以及
  2. 你是从一些现有的目录结构复制,你只需要重建

然后你可以用做--parents的标志cp 。 从信息页面(在可视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


Answer 3:

简答

要复制myfile.txt/foo/bar/myfile.txt ,使用方法:

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

这是如何运作的?

有几个组件到这一点,所以我将一步的所有语法一步。

MKDIR工具, 如POSIX标准规定 ,使得目录。 该-p参数,每次的文档,会造成对MKDIR

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

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

&&列表操作,如记录在POSIX标准 (或猛砸手动 ,如果你喜欢),有效果cp myfile.txt $_只有得到被执行mkdir -p /foo/bar成功执行。 这意味着cp命令不会尝试,如果执行mkdir失败了的原因有很多,可能会失败一个 。

最后, $_我们通过作为第二个参数,以cp为“特殊参数”,它可以很方便用于避免重复长参数(像文件路径),而不必把它们存储在变量中。 每Bash的手册 ,它:

扩展到最后一个参数前一个命令

在这种情况下,这就是/foo/bar ,我们传递给mkdir 。 所以cp命令将扩展为cp myfile.txt /foo/bar ,这副本myfile.txt到新创建的/foo/bar目录。

请注意, $_不是 POSIX标准的一部分 ,所以理论上Unix的变种可能具有不支持此构建物的外壳。 不过,我不知道,不支持任何现代炮弹$_ ; 当然猛砸,短跑,和所有的zsh做。


最后要注意的:我已经在这个答案开始给出的命令假定您的目录名没有空格如果你正在处理与空格的名称,你需要引用它们从而使不同的词不被视为不同参数mkdircp 。 所以,你的命令实际上是这样的:

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


Answer 4:

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

您可以使用install程序“对飞”复制文件并创建目标路径。

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

还有一些方面需要考虑,虽然:

  1. 需要指定目标文件名 ,不仅目标路径
  2. 目标文件将是可执行文件 (至少,据我从我的测试中看到的)

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


在这里,这是无耻的链接,我的灵感来自于答案 =)



Answer 5:

壳牌函数,你想要做什么,打电话,是因为它为挖住在文件中的孔它“埋葬”副本:

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


Answer 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目录名`...`然后将创建一个目录,确保当你调用CP -r正确的基本目录很到位。

该上--parents的优点是,它适用于其中的目标路径的最后一个元素是文件名的情况。

它会在OS X上运行



Answer 7:

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



Answer 8:

用我所有的答案以上方面,我更喜欢使用rsync如下:

$  rsync -a directory_name /path_where_to_inject_your_directory/

例:

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


Answer 9:

只需添加下面的输入您的.bashrc,调整如果你需要。 工作在Ubuntu。

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

例如,如果你想复制“测试”文件到目标目录“D”使用,

mkcp test a/b/c/d

mkcp首先会检查目标目录存在与否,如果没有的话让与复制源文件/目录。



Answer 10:

这确实对我来说

cp -vaR ./from ./to


Answer 11:

我写了一个CP支持脚本,称为CP(注意大写字母)就是打算这样做正是这一点。 脚本将检查你已经把(除了最后一个是目的地)的路径错误,如果一切顺利,它会做一个的mkdir -p步开始复制前创建目标路径。 此时规则CP实用接管任何开关您CP(如-r,-p,-rpL被直接输送到CP)的使用。 在您使用我的剧本,有你需要了解几件事情。

  • 这里所有的信息可以通过做CP --help访问。 CP --help,包括所有的CP的开关。
  • 规则CP不会做的副本,如果没有找到目标路径。 你不必这样与CP错别字的安全网。 你的目标将被创建,所以如果你拼错您的目的地/ usrr /共享/图标或/ usr /共享/图标嗯,这是发生了什么事情被创建。
  • 规则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” 将导致/ C / d / B如果d是一个文件夹
  • “CP / A / B / C / B” 将导致/ C / B / B若B在/ C / B是一个文件夹。
  • 如果两个b和d文件:CP / A / B / C / d将导致/ C / d(其中d是B的副本)。 同样对于CP / A / B / C /在同一环境湾

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

  • “CP --rename / A / B / C / d” 是复制b分成/ c和重命名拷贝到d。

一些收注:与CP一样,CP可以一次复制多个项目与正在上市假定最后的路径是目标。 它也可以,只要你使用引号处理与空格的路径。

CP将检查你放的路径,并确保他们在做副本之前就已存在。 在严格模式(通过--strict开关可用),所有的文件/文件夹复制必须存在,否则没有复制发生。 在宽松的模式(--relaxed),如果你列出的项目中至少存在一个副本将继续。 宽松模式是默认的,你可以暂时通过交换机或永久通过在脚本的开头设置变量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


Answer 12:

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

我认为,“目标”是模糊的没有这种用法尾部斜杠(即在目标目录尚不存在),这也许是为什么cp从来没有加入这个选项。

因此,这里是我的这个功能,强制在目标目录结尾的斜线的版本:

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
}


Answer 13:

刚刚恢复,并给一个完整的工作方案,在一行。 要小心,如果要重命名文件,你应该包括一种方式来提供的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}


Answer 14:

刚刚有同样的问题。 我的方法是只是将文件tar进像这样的档案:

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

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

tar xf your_archive.tar

提取文件成所需的目录结构。



Answer 15:

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

这可能会实现,如果你有正确的那种rsync



Answer 16:

从源复制到一个不存在的路径

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

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

cp –r拷贝所有文件夹及其内容

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



Answer 17:

比方说,你正在做的事情一样

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

希望帮助!



文章来源: Linux: copy and create destination dir if it does not exist