简体   繁体   English

bash shell使用break将文件从一个位置复制到另一个位置并继续

[英]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.

相关问题 用于将文件从一个位置复制到另一个位置并重命名的Shell脚本将当前日期添加到每个文件 - Shell script to copy files from one location to another location and rename add the current date to every file 使用输入文件将文件从一个位置复制并重命名到另一位置 - Copy and Rename files from one location to another using an input file 使用bash将一个文件中的行内容复制到另一文件中的特定字符位置 - Using bash to copy contents of row in one file to specific character location in another file bash程序可将所有文件从一个位置复制到另一位置 - bash program to copy all files from one location to another Shell 将文件从一个位置复制到另一个位置并重命名的脚本,将当前日期和时间戳添加到 YYYYMMDDHHmmSS 格式的每个文件 - Shell script to copy files from one location to another location and rename add the current date and time stamp to every file in YYYYMMDDHHmmSSformat 使用 Bash 将单词从 1 个文件复制到另一个文件 - Copy word from 1 file to another using Bash 如何使用 shell 脚本将文本文件从一个位置复制到另一个位置? [等候接听] - How to copy text file from one location to other using shell script? [on hold] Shell 脚本将文件从一个目录复制到另一个目录 - Shell script copy file from one dir to another Bash脚本将内容从一个文件复制到另一个文件 - Bash Script to copy contents from one file to another bash命令将文件从一台计算机复制到另一台计算机 - bash command to copy file from one computer to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM