用于将文件从源复制到目标的 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
Bash Shell Script to copy files from source to destination
提问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. find
will display an errormessage if SOURCEPATH does not exist (1) and mkdir -p
will create the directories.
只需替换 SOURCEPATH 和 TARGETPATH 就可以了。find
如果 SOURCEPATH 不存在 (1) 将显示错误消息并mkdir -p
创建目录。
回答by steffen
This script creates the whole directory structure in $2
including 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 rsync
is available:
如果应该跳过空目录,则需要更多逻辑;除非rsync
可用:
[[ -d $src_dir ]] || { echo "source dir not exist"; exit 1 ; }
Explanation:
解释:
- The line with
-z
verifies that the second argument is not empty - The line with
! -d
verifies that the directory from first argument exists cp -r
copies recursivelyrsync -mr
copies recursively but skips empty directories&&
and||
are widely used substitutions for conditional statements (because they're short)
回答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 find
command, try this:
如果要使用该find
命令,请尝试以下操作:
Each
-exec
flag begins a block of shell code to execute and\;
terminates it. Getting multiple commands into one-exec
has proven problematic for me in the past, so I include two, one for each task.{}
isfind
's syntax for the variable that indicates the current pathfind
is traversing.Note: The
--parents
flag is on the Linux/GNU variation ofcp
, but is not available on all versions ofcp
, so the script below is less platform dependent. Further, your question referred specifically to using a script.Note:
SRCPATH
andDSTPATH
represent the paths of interest. If any directory name of the source or destination contains spaces, you will have to enclose them in" "
每个
-exec
标志开始一个 shell 代码块来执行并\;
终止它。-exec
在过去,将多个命令合二为一已经证明对我来说是有问题的,所以我包含了两个,每个任务一个。{}
isfind
表示当前路径find
正在遍历的变量的语法。注意:该
--parents
标志位于 的 Linux/GNU 变体上cp
,但并非在 的所有版本上都可用cp
,因此下面的脚本与平台无关。此外,您的问题专门提到使用脚本。注意:
SRCPATH
和DSTPATH
代表感兴趣的路径。如果源或目标的任何目录名称包含空格,则必须将它们括在" "
Alternativelyyou can make a bash
script.
或者,您可以制作bash
脚本。
The
-d
flag in theif
statement 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 operatorsThe
-p
flag on mkdirtells it to fill in missing components of the path as necessary. Without the-p
,mkdir
expects each path component, except the final component of the path, to be present or it will fail.The
-r
flag on cptellscp
to recursively copy all the subdirectories, as well as the files indicated by*
to the destination, creating those subdirectories as necessary.Note: The " " around the paths are to protect against errors if any of the path components contain spaces.
将
-d
在标志if
的语句是标志测试的是/否存在的目录。以下是有关 bash文件测试运算符和其他 bash 比较运算符的一些信息mkdir
-p
上的标志告诉它根据需要填充路径的缺失组件。如果没有, 则希望每个路径组件(路径的最后一个组件除外)都存在,否则它将失败。-p
mkdir
cp
-r
上的标志告诉递归复制所有子目录,以及由 指示的文件到目标,根据需要创建这些子目录。cp
*
注意:如果任何路径组件包含空格,路径周围的“ ”是为了防止出错。