当前位置: 首页 > news >正文

linux命令中的cp,【整理】Linux命令中:rsync和cp之间的区别

【背景】

折腾:

期间,通过:

而了解到了rsync的功能:

大概就是,相当于,从源,到目的,拷贝文件,并且可以设置参数,保持很多文件和文件夹的属性。

感觉和cp很类似。

所以想要搞清楚,rsync和cp的区别。

【折腾过程】

1.参考:

的解释是:

其中一人的解释:

对于拷贝文件,实现备份的话,rsync,比cp更好。

因为rsync只拷贝那些改动了的内容。

2.另外的解释,貌似更合理:

rsync未必是最好。取决于实际情况。

比如:

当拷贝的源和目标,内容很大,而差异很小,则用cp -u

会更有效。

3.所以先去看看cp的用法:CLi@PC-CLI-1 ~/develop/buildroot/buildroot-2013.05

$ cp --help

Usage: cp [OPTION]... [-T] SOURCE DEST

or: cp [OPTION]... SOURCE... DIRECTORY

or: cp [OPTION]... -t DIRECTORY SOURCE...

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.

-a, --archive same as -dR --preserve=all

--attributes-only don't copy the file data, just the attributes

--backup[=CONTROL] make a backup of each existing destination file

-b like --backup but does not accept an argument

--copy-contents copy contents of special files when recursive

-d same as --no-dereference --preserve=links

-f, --force if an existing destination file cannot be

opened, remove it and try again (redundant if

the -n option is used)

-i, --interactive prompt before overwrite (overrides a previous -n

option)

-H follow command-line symbolic links in SOURCE

-l, --link hard link files instead of copying

-L, --dereference always follow symbolic links in SOURCE

-n, --no-clobber do not overwrite an existing file (overrides

a previous -i option)

-P, --no-dereference never follow symbolic links in SOURCE

-p same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST] preserve the specified attributes (default:

mode,ownership,timestamps), if possible

additional attributes: context, links, xattr,

all

--no-preserve=ATTR_LIST don't preserve the specified attributes

--parents use full source file name under DIRECTORY

-R, -r, --recursive copy directories recursively

--reflink[=WHEN] control clone/CoW copies. See below

--remove-destination remove each existing destination file before

attempting to open it (contrast with --force)

--sparse=WHEN control creation of sparse files. See below

--strip-trailing-slashes remove any trailing slashes from each SOURCE

argument

-s, --symbolic-link make symbolic links instead of copying

-S, --suffix=SUFFIX override the usual backup suffix

-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY

-T, --no-target-directory treat DEST as a normal file

-u, --update copy only when the SOURCE file is newer

than the destination file or when the

destination file is missing

-v, --verbose explain what is being done

-x, --one-file-system stay on this file system

--help display this help and exit

--version output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the

corresponding DEST file is made sparse as well. That is the behavior

selected by --sparse=auto. Specify --sparse=always to create a sparse DEST

file whenever the SOURCE file contains a long enough sequence of zero bytes.

Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the

data blocks are copied only when modified. If this is not possible the copy

fails, or if --reflink=auto is specified, fall back to a standard copy.

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.

The version control method may be selected via the --backup option or through

the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)

numbered, t make numbered backups

existing, nil numbered if numbered backups exist, simple otherwise

simple, never always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup

options are given and SOURCE and DEST are the same name for an existing,

regular file.

Report cp bugs to bug-coreutils@gnu.org

GNU coreutils home page:

General help using GNU software:

For complete documentation, run: info coreutils 'cp invocation'

CLi@PC-CLI-1 ~/develop/buildroot/buildroot-2013.05

$

得知-u是update,更新的意思,只更新SOURCE中更新的内容,或者是目标中缺失的文件:

对于文件备份来说,已经足够用了:

每次备份,的确只要备份:

要么是内容更新

要么是原先没有(缺失的)文件

即可。

4.再参考了:

后,更加推荐用rsync了:

rsync可以显示进度条(虽然不是很完美):对于拷贝大量文件,还是很有用的。

其举了个例子:rsync -a --stats --progress /home/tim/stuff /home/tim/documents

而还支持ssh:rsync -a --stats --progress /home/tim/stuff tim@foo.example.com:/home/tim/

或:rsync -a --stats --progress tim@foo.example.com:/home/tim/stuff /home/tim/

5.另外有空也可以参考:

【总结】

rsync:只拷贝那些更新的文件;

cp -u:也可以实现类似效果;

两者都基本可以满足备份的需求;

只是一般情况下,用rsync做这类备份之类的事情,更多见;

总体来说:

备份文件,更推荐用rsync。

相关文章:

  • linux命令grpck,Linux基础命令---验证组文件grpck
  • linux文件夹按日期排序,linux – 如何按日期对文件内容进行排序?
  • linux源码编译rpm,Linux RPM 命令详解及源码编译安装
  • 企业 linux安装软件有哪些,RedHat 6.0 Enterprise企业版如何使用yum安装软件
  • linux应用参数 冒号,Lua-面向对象中函数使用时冒号(:)和点(.)的区别
  • linux执行计划步骤,linux下定时执行计划任务(Scheduled execution tasks under Linux).doc...
  • linux下跑酷游戏编程,【Unity3D开发小游戏】《跑酷小游戏》Unity开发教程
  • c语言中实型变量的三种类型,在C语言中实型变量分为两种类型.doc
  • 如何用c语言输出1 11 111,C语言练习1-11.doc
  • c语言 出库入库软件,终于把栈的实现库遍好了(C语言也可以用哦)
  • 数列极差问题c语言源代码,数列极差问题
  • c语言 输出链表函数,大神帮我看一下怎么输入输出一个链表,我输入了但是没输出啊...
  • android编程歌词显示,Android 音乐播放器实现歌词显示
  • android 代码 drawable,Android 通过DrawableInflater加载自定义Drawable
  • Android ui 单元测试 覆盖率,Android单元测试/Ui测试+JaCoCo覆盖率统计
  • centos安装java运行环境jdk+tomcat
  • GitUp, 你不可错过的秀外慧中的git工具
  • Java基本数据类型之Number
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • vue 配置sass、scss全局变量
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 基于Mobx的多页面小程序的全局共享状态管理实践
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 手写一个CommonJS打包工具(一)
  • 数据仓库的几种建模方法
  • #mysql 8.0 踩坑日记
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (arch)linux 转换文件编码格式
  • (C语言)球球大作战
  • (k8s中)docker netty OOM问题记录
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (六)vue-router+UI组件库
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (十八)三元表达式和列表解析
  • (一)Java算法:二分查找
  • (一)spring cloud微服务分布式云架构 - Spring Cloud简介
  • (转)VC++中ondraw在什么时候调用的
  • (转)如何上传第三方jar包至Maven私服让maven项目可以使用第三方jar包
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NET 常见的偏门问题
  • .Net 路由处理厉害了
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • @synthesize和@dynamic分别有什么作用?
  • [ vulhub漏洞复现篇 ] Hadoop-yarn-RPC 未授权访问漏洞复现
  • []Telit UC864E 拨号上网
  • [1204 寻找子串位置] 解题报告
  • [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存...
  • [Angular] 笔记 21:@ViewChild
  • [BZOJ4566][HAOI2016]找相同字符(SAM)
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [C#小技巧]如何捕捉上升沿和下降沿