Linux:复制并创建目标目录(如果不存在)

发布于 2024-08-07 03:20:17 字数 137 浏览 7 评论 0 原文

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

例子:

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

I want a command (or probably an option to cp) that creates the destination directory if it does not exist.

Example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

静若繁花 2024-08-14 03:20:18

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 的样式,但如果您使用cp 的样式,

。我认为“DEST”在这种用法中没有尾部斜杠是不明确的(即目标目录尚不存在),这也许就是为什么 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
}

cp has multiple usages:

$ 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's answer works for the

cp SOURCE DEST

style of cp, but does the wrong thing if you use the

cp SOURCE... DIRECTORY/

style of cp.

I think that "DEST" is ambiguous without a trailing slash in this usage (i.e. where the target directory doesn't yet exist), which is perhaps why cp has never added an option for this.

So here's my version of this function which enforces a trailing slash on the dest dir:

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
}
沫尐诺 2024-08-14 03:20:18

刚刚有同样的问题。我的方法是将文件压缩到存档中,如下所示:

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

tar 自动将文件存储在适当的位置档案内的结构。如果运行

tar xf your_archive.tar

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

Just had the same issue. My approach was to just tar the files into an archive like so:

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

tar automatically stores the files in the appropriate structure within the archive. If you run

tar xf your_archive.tar

the files are extracted into the desired directory structure.

零度℉ 2024-08-14 03:20:18

从源复制到不存在的路径

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

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

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

$_作为最后创建的目标命令

Copy from source to an non existing path

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

NOTE: this command copies all the files

cp –r for copying all folders and its content

$_ work as destination which is created in last command

半﹌身腐败 2024-08-14 03:20:18

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

set +H; echo -e "#!/bin/sh\nmkdir -p \$(目录名 \"\$2\"); cp \"\$1\" \"$2\"\;" > 〜/本地/bin/cpmkdir; chmod +x ~/local/bin/cpmkdir

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

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

Oneliner to create a small script that can be used as subcommand, in find for instance:

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

You can then use it like:

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

围归者 2024-08-14 03:20:18
rsync file /path/to/copy/file/to/is/very/deep/there

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

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

This might work, if you have the right kind of rsync.

蓝咒 2024-08-14 03:20:18

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

You can use find with Perl. Command will be like this:

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$_");'

This command will create directory $t if it doesn't exist. And than copy file into $t only unless file exists inside $t.

给妤﹃绝世温柔 2024-08-14 03:20:18

这适用于 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

尝试一下。

This works on GNU /bin/bash version 3.2 on MacOS (tested on both Catalina and Big Sur)

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

the "v" option is for verbose.

And I think of the "-R" option as "Recursive".

man's full description of -R is:

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.

In the example below, I'm using a "/" at the end of existingfolder so that it copies all the contents of existingfolder (and not the folder itself) into newfolder:

cp -Rv existingfolder/  newfolder

Try it.

凹づ凸ル 2024-08-14 03:20:18

许多其他解决方案不适用于需要转义的文件或文件夹。这是一个适用于文件和文件夹的解决方案,并转义空格和其他特殊字符。在 busybox ash shell 中进行了测试,该 shell 无法访问某些更高级的选项。

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 也应该有效。

Many of the other solutions don't work on files or folders which need escaping. Here is a solution which works for files and folders, and escapes spaces and other special characters. Tested in a busybox ash shell which doesn't have access to some of the fancier options.

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"

This should also work if you omit bar.txt if you need the recursive copy of a whole folder.

喜你已久 2024-08-14 03:20:18

假设你正在做类似的事情

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

希望有所帮助!

Let's say you are doing something like

cp file1.txt A/B/C/D/file.txt

where A/B/C/D are directories which do not exist yet

A possible solution is as follows

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

hope that helps!

留蓝 2024-08-14 03:20:18

仅适用于 macOS

rsync -R <source file path> destination_folder

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

Only for macOS

rsync -R <source file path> destination_folder

For macOS --parents option of cp doesn't work

洒一地阳光 2024-08-14 03:20:18

(如果我的答案没有在这里被阻止...)这对 rsync 没有帮助吗:

rsync --mkpath sourcedir/ destdir

如果不存在,则创建 destdir。

(If my answer doesn't get blocked here...) couldn't this help in rsync:

rsync --mkpath sourcedir/ destdir

The destdir is created if non-existent.

⒈起吃苦の倖褔 2024-08-14 03:20:18

将文件从 ./assets/ 复制到当前日期的其他目录

rsync -avd ./assets/ ~/backup/project_x/assets/$(date '+%Y-%m-%d')

-a                          Copy files from sub-dir of source dir
-d, --dirs                  Also copy directories
-u, --update                skip files that are newer on the receiver
    --inplace               update destination files in-place
    --append                append data onto shorter files
-l, --links                 copy symlinks as symlinks

更多信息:

rsync [Options] [Source] [Destination]

将 x 的内容复制到 y 目录 x/a.txt y/a.txt :

rsync -av x/ y

仅复制 a.txt 和 b.txt从 x 到 y 目录:

rsync -av 'x/a.txt x/b.txt' y

本地到远程:

rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

远程到本地:

rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

从文件中忽略:

rsync -avd --delete --exclude-from=.resyncignore
  • man rsync - 列出 rsync 的所有可用选项
  • man date - 有关其他日期格式的更多信息

Copy files from ./assets/ to other dir with current date

rsync -avd ./assets/ ~/backup/project_x/assets/$(date '+%Y-%m-%d')

-a                          Copy files from sub-dir of source dir
-d, --dirs                  Also copy directories
-u, --update                skip files that are newer on the receiver
    --inplace               update destination files in-place
    --append                append data onto shorter files
-l, --links                 copy symlinks as symlinks

More Information:

rsync [Options] [Source] [Destination]

Copy content of x to y directory x/a.txt y/a.txt :

rsync -av x/ y

Copy a.txt and b.txt only from x to y dir :

rsync -av 'x/a.txt x/b.txt' y

Local to remote :

rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

Remote to local :

rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

Ignore from a file :

rsync -avd --delete --exclude-from=.resyncignore
  • man rsync - To list all the available options for rsync
  • man date - for more information on other date format
烟─花易冷 2024-08-14 03:20:18

简单

cp -a * /path/to/dst/

应该可以解决问题。

Simple

cp -a * /path/to/dst/

should do the trick.

红衣飘飘貌似仙 2024-08-14 03:20:17
mkdir -p "$d" && cp file "$d"

cp 没有这样的选项)。

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

(there's no such option for cp).

懒的傷心 2024-08-14 03:20:17

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

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

那么您可以使用 cp--parents 标志来完成此操作。从信息页面(可在 http://www.gnu.org 查看/software/coreutils/manual/html_node/cp-in Vocation.html#cp-inspiration 或使用 info cpman cp):

--parents
     通过附加到目标来形成每个目标文件的名称
     目录是一个斜杠和指定的源文件名。这
     赋予“cp”的最后一个参数必须是现有的名称
     目录。例如,命令:

          cp --parents a/b/c 现有目录

     将文件“a/b/c”复制到“existing_dir/a/b/c”,创建任何
     缺少中间目录。

例子:

/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

If both of the following are true:

  1. You are using the GNU version of cp (and not, for instance, the Mac version), and
  2. You are copying from some existing directory structure and you just need it recreated

then you can do this with the --parents flag of cp. From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or 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.

Example:

/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
孤檠 2024-08-14 03:20:17

简短回答

要将 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 标准(或Bash 手册(如果您愿意),其效果是仅当 mkdir -p /foo/bar 执行时才执行 cp myfile.txt $_这意味着如果 mkdir 失败,则 cp 命令不会尝试执行。 functions/mkdir.html#tag_03_371_05">它可能失败的众多原因之一

最后,我们作为第二个参数传递给 cp$_ 是一个“特殊参数”,可以方便地避免重复长参数(如文件路径),而不必将它们存储在变量中。 html_node/Special-Parameters.html">Bash 手册,它:

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

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

请注意,$_ 不是的一部分POSIX 标准,因此理论上 Unix 变体可能有不支持此构造的 shell。但是,我不知道有哪个现代 shell 不支持 $_;当然,Bash、Dash 和 zsh 都可以。


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

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

Short Answer

To copy myfile.txt to /foo/bar/myfile.txt, use:

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

How does this work?

There's a few components to this, so I'll cover all the syntax step by step.

The mkdir utility, as specified in the POSIX standard, makes directories. The -p argument, per the docs, will cause mkdir to

Create any missing intermediate pathname components

meaning that when calling mkdir -p /foo/bar, mkdir will create /foo and /foo/bar if /foo doesn't already exist. (Without -p, it will instead throw an error.

The && list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. This means the cp command won't try to execute if mkdir fails for one of the many reasons it might fail.

Finally, the $_ we pass as the second argument to cp is a "special parameter" which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable. Per the Bash manual, it:

expands to the last argument to the previous command

In this case, that's the /foo/bar we passed to mkdir. So the cp command expands to cp myfile.txt /foo/bar, which copies myfile.txt into the newly created /foo/bar directory.

Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn't support this construct. However, I don't know of any modern shells that don't support $_; certainly Bash, Dash, and zsh all do.


A final note: the command I've given at the start of this answer assumes that your directory names don't have spaces in. If you're dealing with names with spaces, you'll need to quote them so that the different words aren't treated as different arguments to mkdir or cp. So your command would actually look like:

mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"
万劫不复 2024-08-14 03:20:17

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

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

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

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

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

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


这是无耻的链接到我受到启发的答案 =)

Such an old question, but maybe I can propose an alternative solution.

You can use the install programme to copy your file and create the destination path "on the fly".

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

There are some aspects to take in consideration, though:

  1. you need to specify also the destination file name, not only the destination path
  2. the destination file will be executable (at least, as far as I saw from my tests)

You can easily amend the #2 by adding the -m option to set permissions on the destination file (example: -m 664 will create the destination file with permissions rw-rw-r--, just like creating a new file with touch).


And here it is the shameless link to the answer I was inspired by =)

深海夜未眠 2024-08-14 03:20:17

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

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

Shell function that does what you want, calling it a "bury" copy because it digs a hole for the file to live in:

bury_copy() { mkdir -p `dirname $2` && cp "$1" "$2"; }
红玫瑰 2024-08-14 03:20:17

这是一种方法:

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

Here's one way to do it:

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 will give you the parent of the destination directory or file. mkdir -p `dirname ...` will then create that directory ensuring that when you call cp -r the correct base directory is in place.

The advantage of this over --parents is that it works for the case where the last element in the destination path is a filename.

And it'll work on OS X.

甜味拾荒者 2024-08-14 03:20:17

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

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

This is very late but it may help a rookie somewhere. If you need to AUTO create folders rsync should be your best friend.

rsync /path/to/sourcefile /path/to/tragetdir/thatdoestexist/
虫児飞 2024-08-14 03:20:17

出于对上述答案的尊重,我更喜欢使用 rsync,如下所示:

$  rsync -a directory_name /path_where_to_inject_your_directory/

示例:

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

with all my respect for answers above, I prefer to use rsync as follow:

$  rsync -a directory_name /path_where_to_inject_your_directory/

example:

$ rsync -a test /usr/local/lib/
夏末染殇 2024-08-14 03:20:17

安装-D文件-m 644 -t /path/to/copy/file/to/is/very/deep/there

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

半岛未凉 2024-08-14 03:20:17

这对我有用

cp -vaR ./from ./to

This does it for me

cp -vaR ./from ./to
栖竹 2024-08-14 03:20:17

正如上面 help_asap 和 Spongeman 所建议的,您可以使用“install”命令将文件复制到现有目录,或者创建新的目标目录(如果它们尚不存在)。

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

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

选项3
安装 -D 文件名 -m640 -t some/deep/directory/
按照选项 2,但将文件名定位到目标目录中,因此不需要在源和目标中都写入文件名。

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

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

As suggested above by help_asap and spongeman you can use the 'install' command to copy files to existing directories or create create new destination directories if they don't already exist.

Option 1
install -D filename some/deep/directory/filename
copies file to a new or existing directory and gives filename default 755 permissions

Option 2
install -D filename -m640 some/deep/directory/filename
as per Option 1 but gives filename 640 permissions.

Option 3
install -D filename -m640 -t some/deep/directory/
as per Option 2 but targets filename into target directory so filename does not need to be written in both source and target.

Option 4
install -D filena* -m640 -t some/deep/directory/
as per Option 3 but uses a wildcard for multiple files.

It works nicely in Ubuntu and combines two steps (directory creation then file copy) into one single step.

童话里做英雄 2024-08-14 03:20:17

只需在 .bashrc 中添加以下内容,并根据需要进行调整。在 Ubuntu 中工作。

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

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

mkcp test a/b/c/d

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

Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.

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

E.g
If you want to copy 'test' file to destination directory 'd'
Use,

mkcp test a/b/c/d

mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.

临风闻羌笛 2024-08-14 03:20:17

只是为了恢复并在一行中提供完整的工作解决方案。
如果您想重命名文件,请小心,您应该提供一种为 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}

Just to resume and give a complete working solution, in one line.
Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir.
Next code should work in any case.

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

or bash specific

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst}
深居我梦 2024-08-14 03:20:17

无需创建脚本并使用简单的命令...

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

Simply without creating script and with simple command ...

mkdir -p /destination-folder/ && cp file-name /destination-folder/
捶死心动 2024-08-14 03:20:17

我强烈建议同上
就可以了。

同上 my/location/poop.txt this/doesnt/exist/yet/poop.txt

i strongly suggest ditto.
just works.

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

羁客 2024-08-14 03:20:17

我为 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
  • 如果 b 在“CP /a/b /c/b”中,则生成 /c/b/b /c/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

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 doesn't have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. 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 if d is a folder
  • "CP /a/b /c/b" will result in /c/b/b if b in /c/b is a folder.
  • If both b and d are files: CP /a/b /c/d will result in /c/d (where d is a copy of b). Same for CP /a/b /c/b in the same circumstance.

This default CP 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.

A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.

CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through --strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (--relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.

Here's how to install it:

In a non-root terminal, do:

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

In gedit, paste CP utility and save:

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文