Linux如何复制而不覆盖?

时间:2021-05-03 00:04:32

I want to cp a directory but I do not want to overwrite any existing files even it they are older than the copied files. And I want to do it completely noniteractive as this will be a part of a Crontab Bash script. Any ideas?

我想对一个目录进行cp,但是我不想覆盖任何现有的文件,即使它们比复制的文件更老。我希望它完全是非迭代的,因为这将是Crontab Bash脚本的一部分。什么好主意吗?

6 个解决方案

#1


372  

Taken from the man page:

从手册页:

-n, --no-clobber
              do not overwrite an existing file (overrides a previous -i option)

Example:

例子:

cp -n myoldfile.txt mycopiedfile.txt

#2


52  

Consider using rsync.

考虑使用rsync。

rsync -a -v src dst

But this will update files if differ.

但如果不同,这将更新文件。


As per comments

按评论

rsync -a -v --ignore-existing src dst

will ignore existing files completely.

将完全忽略现有文件。

#3


42  

cp -n

Is what you want. See the man page.

是你想要的。查看手册页。

#4


30  

For people that find that don't have an 'n' option (like me on RedHat) you can use cp -u to only write the file if the source is newer than the existing one (or there isn't an existing one).

对于那些发现没有“n”选项的人(比如我在RedHat上),您可以使用cp -u只在源代码比现有文件更新(或者没有现有文件)时才编写文件。

[edit] As mentioned in the comments, this will overwrite older files, so isn't exactly what the OP wanted. Use ceving's answer for that.

正如评论中提到的,这将覆盖旧文件,因此这并不是OP想要的。用克韦的答案。

#5


23  

This will work on RedHat:

这将适用于RedHat:

false | cp -i source destination 2>/dev/null

Updating and not overwriting is something different.

更新和不重写是不同的。

#6


3  

Alpine linux: Below answer is only for case of single file: in alpine cp -n not working (and false | cp -i ... too) so solution working in my case that i found is:

Alpine linux:以下的答案仅适用于单个文件:在Alpine cp -n中不工作(和false | cp -i…)所以我找到的解决方案是:

if [ ! -f env.js ]; then cp env.example.js env.js; fi 

In above example we check if env.js file exists and if not we copy env.example.js as env.js.

在上面的示例中,我们检查env。js文件存在,如果没有,我们复制env.example。js js。

#1


372  

Taken from the man page:

从手册页:

-n, --no-clobber
              do not overwrite an existing file (overrides a previous -i option)

Example:

例子:

cp -n myoldfile.txt mycopiedfile.txt

#2


52  

Consider using rsync.

考虑使用rsync。

rsync -a -v src dst

But this will update files if differ.

但如果不同,这将更新文件。


As per comments

按评论

rsync -a -v --ignore-existing src dst

will ignore existing files completely.

将完全忽略现有文件。

#3


42  

cp -n

Is what you want. See the man page.

是你想要的。查看手册页。

#4


30  

For people that find that don't have an 'n' option (like me on RedHat) you can use cp -u to only write the file if the source is newer than the existing one (or there isn't an existing one).

对于那些发现没有“n”选项的人(比如我在RedHat上),您可以使用cp -u只在源代码比现有文件更新(或者没有现有文件)时才编写文件。

[edit] As mentioned in the comments, this will overwrite older files, so isn't exactly what the OP wanted. Use ceving's answer for that.

正如评论中提到的,这将覆盖旧文件,因此这并不是OP想要的。用克韦的答案。

#5


23  

This will work on RedHat:

这将适用于RedHat:

false | cp -i source destination 2>/dev/null

Updating and not overwriting is something different.

更新和不重写是不同的。

#6


3  

Alpine linux: Below answer is only for case of single file: in alpine cp -n not working (and false | cp -i ... too) so solution working in my case that i found is:

Alpine linux:以下的答案仅适用于单个文件:在Alpine cp -n中不工作(和false | cp -i…)所以我找到的解决方案是:

if [ ! -f env.js ]; then cp env.example.js env.js; fi 

In above example we check if env.js file exists and if not we copy env.example.js as env.js.

在上面的示例中,我们检查env。js文件存在,如果没有,我们复制env.example。js js。