用于将文件从源复制到目标的 Bash Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29518818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:45:05  来源:igfitidea点击:

Bash Shell Script to copy files from source to destination

linuxbashshellunixcp

提问by MrQuartz

I have a question related to my bash shell script. Basically, I need to develop a script that copies certain files from one directory to another. Sounds easy, however, its quite a challenge for me and I hope some of you might help me. So script should work like this:

我有一个与我的 bash shell 脚本相关的问题。基本上,我需要开发一个脚本,将某些文件从一个目录复制到另一个目录。听起来很简单,然而,这对我来说是一个很大的挑战,我希望你们中的一些人可以帮助我。所以脚本应该像这样工作:

Scriptname Source_path Destination_path

脚本名 Source_path Destination_path

1) 1st Problem - the source_path might not exist and I need to echo an error message.

1) 第一个问题 - source_path 可能不存在,我需要回显错误消息。

2) 2nd Problem - the destination path might not exist, however, I need to create some or all of the directories and then copy the files from source. Else, if directory exists, I just need to copy.

2)第二个问题 - 目标路径可能不存在,但是,我需要创建部分或全部目录,然后从源复制文件。否则,如果目录存在,我只需要复制。

Hope this is clear and hopefully someone might help me thanks!

希望这很清楚,希望有人可以帮助我,谢谢!

回答by steffen

Have a look at this:

看看这个:

 find SOURCEPATH -type f | while read fname
 do 
 mkdir -p TARGETPATH/$(dirname "$fname")
 cp --parents $fname TARGETPATH
 done

Just substitude SOURCEPATH and TARGETPATH and you're good. findwill display an errormessage if SOURCEPATH does not exist (1) and mkdir -pwill create the directories.

只需替换 SOURCEPATH 和 TARGETPATH 就可以了。find如果 SOURCEPATH 不存在 (1) 将显示错误消息并mkdir -p创建目录。

回答by steffen

This script creates the whole directory structure in $2including empty folders from $1:

此脚本创建整个目录结构,$2包括来自$1以下位置的空文件夹:

#!/bin/sh
[ -z "" ] && { echo "Usage: 
#!/bin/sh
[ -z "" ] && { echo "Usage: 
src_dir=
dest_dir=
SOURCE DEST"; exit 1; } [ ! -d "" ] && { echo "'' does not exist or isn't a directory"; exit 1; } rsync -mr "" ""
SOURCE DEST"; exit 1; } [ ! -d "" ] && { echo "'' does not exist or isn't a directory"; exit 1; } cp -r "" ""

If empty directories should be skipped, you need some more logic; unless rsyncis available:

如果应该跳过空目录,则需要更多逻辑;除非rsync可用:

[[ -d $src_dir ]] || { echo "source dir not exist"; exit 1 ; }

Explanation:

解释:

  • The line with -zverifies that the second argument is not empty
  • The line with ! -dverifies that the directory from first argument exists
  • cp -rcopies recursively
  • rsync -mrcopies recursively but skips empty directories
  • &&and ||are widely used substitutions for conditional statements (because they're short)
  • -z验证第二个参数不为空的行
  • ! -d验证第一个参数中的目录是否存在
  • cp -r递归复制
  • rsync -mr递归复制但跳过空目录
  • &&并且||是条件语句的广泛使用的替换(因为它们很短)

回答by GL2014

Set Variables for arguments one and two, source and dest respectively..

分别为参数一和参数设置变量,源和目标。

mkdir -p $dest_dir

Check if directory exists if not exit

不退出则检查目录是否存在

find SRCPATH -exec mkdir -p DSTPATH/$(dirname "{}") \; -exec cp --parents "{}" DSTPATH \;

Create all dirs needed in path

创建路径中需要的所有目录

#!/bin/bash
srcpath=
dstpath=
if [ ! -d "$srcpath" ]; then
    echo "Source path: $srcpath doesn't exist"
    exit 1
fi
mkdir -p "$dstpath"
cp -r "$srcpath/*" "$dstpath"

回答by clearlight

If you want to use the findcommand, try this:

如果要使用该find命令,请尝试以下操作:

##代码##
  1. Each -execflag begins a block of shell code to execute and \;terminates it. Getting multiple commands into one -exechas proven problematic for me in the past, so I include two, one for each task.

  2. {}is find's syntax for the variable that indicates the current path findis traversing.

  3. Note: The --parentsflag is on the Linux/GNU variation of cp, but is not available on all versions of cp, so the script below is less platform dependent. Further, your question referred specifically to using a script.

  4. Note: SRCPATHand DSTPATHrepresent the paths of interest. If any directory name of the source or destination contains spaces, you will have to enclose them in " "

  1. 每个-exec标志开始一个 shell 代码块来执行并\;终止它。-exec在过去,将多个命令合二为一已经证明对我来说是有问题的,所以我包含了两个,每个任务一个。

  2. {}isfind表示当前路径find正在遍历的变量的语法。

  3. 注意:该--parents标志位于 的 Linux/GNU 变体上cp,但并非在 的所有版本上都可用cp,因此下面脚本与平台无关。此外,您的问题专门提到使用脚本。

  4. 注意:SRCPATHDSTPATH代表感兴趣的路径。如果源或目标的任何目录名称包含空格,则必须将它们括在" "



Alternativelyyou can make a bashscript.

或者,您可以制作bash脚本。

##代码##
  1. The -dflag in the ifstatement is the flag to test for the yes/no existence of the directory. Here's some information on bash file test operatorsand other bash comparison operators

  2. The -pflag on mkdirtells it to fill in missing components of the path as necessary. Without the -p, mkdirexpects each path component, except the final component of the path, to be present or it will fail.

  3. The -rflag on cptells cpto recursively copy all the subdirectories, as well as the files indicated by *to the destination, creating those subdirectories as necessary.

  4. Note: The " " around the paths are to protect against errors if any of the path components contain spaces.

  1. -d在标志if的语句是标志测试的是/否存在的目录。以下是有关 bash文件测试运算符其他 bash 比较运算符的一些信息

  2. mkdir-p上的标志告诉它根据需要填充路径的缺失组件。如果没有, 则希望每个路径组件(路径的最后一个组件除外)都存在,否则它将失败。-pmkdir

  3. cp-r上的标志告诉递归复制所有子目录,以及由 指示的文件到目标,根据需要创建这些子目录。cp*

  4. 注意:如果任何路径组件包含空格,路径周围的“ ”是为了防止出错。