Linux: copy and create destination dir if it does not exist
我想要一个命令(或者可能是CP的一个选项),如果目标目录不存在的话,它将创建目标目录。
例子:
1 | cp -? file /path/to/copy/file/to/is/very/deep/there |
1 | mkdir -p"$d" && cp file"$d" |
(对于
如果以下两项均为真:
然后你可以用
1
2
3
4
5
6
7
8
9
10 --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.
例子:
1 2 3 4 5 6 7 | /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 |
简短回答
要将
1 | mkdir -p /foo/bar && cp myfile.txt $_ |
这是怎么工作的?
这里有一些组件,所以我将逐步介绍所有的语法。
正如posix标准中指定的,mkdir实用程序生成目录。根据文件,
Create any missing intermediate pathname components
也就是说,在调用
在posix标准(或者bash手册,如果您愿意)中记录的
最后,我们将
expands to the last argument to the previous command
在这种情况下,这就是我们传递给
请注意,
最后一点注意:我在这个答案开头给出的命令假定您的目录名中没有空格。如果要处理带有空格的名称,则需要引用它们,这样不同的单词就不会被视为
1 | mkdir -p"/my directory/name with/spaces" && cp"my filename with spaces.txt""$_" |
这是一个古老的问题,但也许我可以提出另一种解决方案。
您可以使用
1 | install -D file /path/to/copy/file/to/is/very/deep/there/file |
尽管如此,仍有一些方面需要考虑:
您可以通过添加
在这里,这是一个无耻的链接,它指向了我灵感来源于的答案。
shell函数,执行您想要的操作,将其称为"bury"副本,因为它会为文件挖掘一个生存空间:
1 | bury_copy() { mkdir -p `dirname $2` && cp"$1""$2"; } |
有一种方法可以做到:
1 2 | mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \ && cp -r file /path/to/copy/file/to/is/very/deep/there |
与父级相比,它的优势在于它适用于目标路径中的最后一个元素是文件名的情况。
它将在OSX上工作。
出于对以上答案的尊重,我更喜欢使用rsync,如下所示:
1 | $ rsync -a directory_name /path_where_to_inject_your_directory/ |
例子:
1 | $ rsync -a test /usr/local/lib/ |
只需在.bashrc中添加以下内容,根据需要进行调整。在Ubuntu工作。
1 2 3 4 | mkcp() { test -d"$2" || mkdir -p"$2" cp -r"$1""$2" } |
例如如果要将"test"文件复制到目标目录"d"使用,
1 | mkcp test a/b/c/d |
mkcp将首先检查目标目录是否存在,如果不存在,则生成目标目录并复制源文件/目录。
1 2 3 4 5 | $ 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的答案适用于
1 | cp SOURCE DEST |
1 | cp SOURCE... DIRECTORY/ |
我认为"dest"在这个用法中不带尾随斜杠是不明确的(即目标目录还不存在的地方),这可能就是为什么
下面是我对dest dir执行尾随斜杠的函数的版本:
1 2 3 4 5 6 7 8 9 10 11 | 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 } |
只是有同样的问题。我的方法是将文件压缩成这样的归档文件:
tar自动将文件存储在归档文件中的适当结构中。如果你跑
这些文件被提取到所需的目录结构中。
这是给我的
1 | cp -vaR ./from ./to |
我为cp编写了一个支持脚本,叫做cp(注意大写字母),它的目的就是要做到这一点。脚本将检查您输入的路径中是否有错误(最后一个路径是目标路径除外),如果一切正常,它将执行mkdir-p步骤,在开始复制之前创建目标路径。此时,常规的cp实用程序将接管,您与cp一起使用的任何开关(如-r、-p、-rpl将直接连接到cp)。在使用我的脚本之前,您需要了解一些事情。
- 这里的所有信息都可以通过cp——帮助来访问。cp——帮助包括cp的交换机。
- 如果常规的CP找不到目标路径,它就不会进行复制。你没有一个这样的安全网来对付CP的打字错误。你的目的地将被创建,所以如果你把目的地拼错为/usrr/share/icon s或/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
- 如果b in/c/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--重命名/a/b/c/d"正在将b复制到/c并将副本重命名为d。
一些结束说明:和CP一样,CP可以一次复制多个项目,并假定最后一个路径是目标路径。它还可以处理带有空格的路径,只要使用引号。
CP将检查您输入的路径,并在进行复制之前确保它们存在。在严格模式下(通过--strict开关可用),要复制的所有文件/文件夹必须存在,否则将不进行复制。在放松模式(--relaxed)中,如果至少有一个列出的项存在,则复制将继续。放松模式是默认模式,您可以通过开关临时更改模式,也可以通过在脚本开头设置变量easy_来永久更改模式。
安装方法如下:
在非根终端中,执行以下操作:
1 2 | sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP gedit admin:///usr/bin/CP |
在gedit中,粘贴cp实用程序并保存:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #!/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="===============================================================================\ CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\ =============================================================================== This script (CP, note capital letters) is intended to supplement \ your system's regular cp command (note uncapped letters). Script's function is to check if the destination path exists \ before starting the copy. If it doesn't it will be created. To make this happen, CP assumes that the item you're copying is \ being dropped in the destination path and is not the destination\ itself (aka, a renamed copy of the source file/folder). Meaning: * "CP /a/b /c/d" will result in /c/d/b \ * even if you write "CP /a/b /c/b", CP will create the path /a/b, \ resulting in /c/b/b. Of course, if /c/b or /c/d are existing files and /a/b is also a\ file, the existing destination file will simply be overwritten. \ This behavior can be changed with the "--rename" switch. In this\ case, it's assumed that "CP --rename /a/b /c/d" is copying b into /c \ and renaming the copy to d. ===============================================================================\ CP specific help: Switches and their Usages \ =============================================================================== \ --rename\tSee above. Ignored if copying more than one item. --quiet\tCP is verbose by default. This quiets it. --strict\tIf one+ of your files was not found, CP exits if\ \t\tyou use --rename switch with multiple items, CP \ \t\texits. --relaxed\tIgnores bad paths unless they're all bad but warns\ \t\tyou about them. Ignores in-appropriate rename switch\ \t\twithout exiting. This is default behavior. You can \ \t\tmake strict the default behavior by editing the \ \t\tCP script and setting: \t\teasy_going=false. --help-all\tShows help specific to cp (in addition to CP)." cp_hlp=" Regular cp command's switches will still work when using CP.\ Here is the help out of the original cp command... \ ===============================================================================\ cp specific help: \ =============================================================================== " outro1=" ******************************************************************************\ ******************************************************************************\ ******************************************************************************\ USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\ ******************************************************************************\ ******************************* HIT q TO EXIT ********************************\ ******************************************************************************" #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 path 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. Exit Status: $cp_err" else echo"Copy operation exited with no errors." fi exit |
只需在一行中恢复并给出完整的工作解决方案。如果您想重命名您的文件,请小心,您应该提供一个到mkdir的干净dir路径。$fdst可以是file或dir。下一个代码在任何情况下都可以工作。
1 2 3 | fsrc=/tmp/myfile.unk fdst=/tmp/dir1/dir2/dir3/myfile.txt mkdir -p $(dirname ${fdst}) && cp -p ${fsrc} ${fdst} |
或特定于bash
1 2 3 | fsrc=/tmp/myfile.unk fdst=/tmp/dir1/dir2/dir3/myfile.txt mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst} |
1 | rsync file /path/to/copy/file/to/is/very/deep/there |
如果你有合适的
从源复制到不存在的路径
1 | mkdir –p /destination && cp –r /source/ $_ |
注意:此命令复制所有文件
用于复制所有文件夹及其内容的
假设你在做类似的事情
cp file1.txt A/B/C/D/file.txt
其中a/b/c/d是尚未存在的目录
可能的解决方案如下
1 2 3 4 | 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 |
希望有帮助!