[英]bash shell copy the file from one location to another using break and continue
I have small script to copy the all the files from one directory (SRC) to another directory (DES). 我有一个小脚本,可以将所有文件从一个目录(SRC)复制到另一个目录(DES)。 This below script is running perfectly. 下面的脚本运行良好。
#!/bin/bash
SRC="/home/user/dir1/*"
DES="/home/user/dir2/"
for file in "$SRC"
do
if [ -f "$file" ]
then
cp "$file" "$DES"
echo "$file -----> file copied"
fi
done
Now what i am thinking while copying files from one directory to another directory, how to skip the copying file if that file has already exist in (DES) directory with same name of (SRC) directory and continue the remaining file as usual from source to destination? 现在我在将文件从一个目录复制到另一目录时的想法是,如果该文件已经存在于(SRC)目录同名的(DES)目录中,并且如何像往常一样从源到目录继续其余文件,如何跳过复制文件目的地?
Here how to i use break and continue looping to perform this action? 在这里,我如何使用break并继续循环以执行此操作?
Thanks, 谢谢,
I recommend to use rsync
: 我建议使用rsync
:
src="/home/user/dir1/"
dst="/home/user/dir2/"
rsync -rav --ignore-existing "${src}" "${dst}"
The switch --ignore-existing
tells rsync
to skip files which exist at the destination. 开关--ignore-existing
告诉rsync
跳过目标中存在的文件。
Why not just reduce the entire script to a oneliner? 为什么不将整个脚本简化为一个单行?
cp -n /home/user/dir1/* /home/user/dir2/
The -n
flag ( --no-clobber
) prevents cp
from overwriting existing files. -n
标志( --no-clobber
)防止cp
覆盖现有文件。
If your real situation is more complicated, you can also take a look at rsync
. 如果您的实际情况更加复杂,则还可以查看rsync
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.