Linux中的拷贝命令cp

点击打开链接

命令用途

 

cp命令用于复制文件或目录

1. 如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。

2. 若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息

命令举例



#帮助命令

[plain] view plain copy
  1. bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ cp --h  
  2. 用法:cp [选项]... [-T] 源文件 目标文件  
  3.  或:cp [选项]... 源文件... 目录  
  4.  或:cp [选项]... -t 目录 源文件...  
  5. 将源文件复制至目标文件,或将多个源文件复制至目标目录。  
  6. 长选项必须使用的参数对于短选项时也是必需使用的。  
  7. -a, --archive 等于-dR --preserve=all  
  8. --attributes-only 仅复制属性而不复制数据 --backup[=CONTROL 为每个已存在的目标文件创建备份  
  9. -b 类似--backup 但不接受参数  
  10. --copy-contents 在递归处理是复制特殊文件内容  
  11. -d 等于--no-dereference --preserve=links  
  12. -f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项  
  13. 存在时则不需再选此项)  
  14. -i, --interactive 覆盖前询问(使前面的 -n 选项失效)  
  15. -H 跟随源文件中的命令行符号链接  
  16. -l, --link hard link files instead of copying  
  17. -L, --dereference always follow symbolic links in SOURCE  
  18. -n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)  
  19. -P, --no-dereference 不跟随源文件中的符号链接  
  20. -p 等于--preserve=模式,所有权,时间戳  
  21. --preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果  
  22. 可能保持附加属性:环境、链接、xattr 等  
  23. --sno-preserve=属性列表 不保留指定的文件属性  
  24. --parents 复制前在目标目录创建来源文件路径中的所有目录  
  25. -R, -r, --recursive 递归复制目录及其子目录内的所有内容  
  26. --reflink[=WHEN] 控制克隆/CoW 副本。请查看下面的内如。  
  27. --remove-destination 尝试打开目标文件前先删除已存在的目的地  
  28. 文件 (相对于 --force 选项)  
  29. --sparse=WHEN 控制创建稀疏文件的方式  
  30. --strip-trailing-slashes 删除参数中所有源文件/目录末端的斜杠  
  31. -s, --symbolic-link 只创建符号链接而不复制文件  
  32. -S, --suffix=后缀 自行指定备份文件的后缀  
  33. -t, --target-directory=目录 将所有参数指定的源文件/目录  
  34. 复制至目标目录  
  35. -T, --no-target-directory 将目标目录视作普通文件  
  36. -u, --update 只在源文件比目标文件新,或目标文件  
  37. 不存在时才进行复制  
  38. -v, --verbose 显示详细的进行步骤  
  39. -x, --one-file-system 不跨越文件系统进行操作  
  40. --help 显示此帮助信息并退出  
  41. --version 显示版本信息并退出  
  42. 默认情况下,源文件的稀疏性仅仅通过简单的方法判断,对应的目标文件目标文件也  
  43. 被为稀疏。这是因为默认情况下使用了--sparse=auto 参数。如果明确使用  
  44. --sparse=always 参数则不论源文件是否包含足够长的0 序列也将目标文件创文  
  45. 建为稀疏件。  
  46. 使用--sparse=never 参数禁止创建稀疏文件。  
  47. 当指定了--reflink[=always] 参数时执行轻量化的复制,即只在数据块被修改的  
  48. 情况下才复制。如果复制失败或者同时指定了--reflink=auto,则返回标准复制模式。  
  49. The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.  
  50. The version control method may be selected via the --backup option or through  
  51. the VERSION_CONTROL environment variable. Here are the values:  
  52. none, off 不进行备份(即使使用了--backup 选项)  
  53. numbered, t 备份文件加上数字进行排序  
  54. existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份  
  55. simple, never 永远使用普通方式备份  
  56. 有一个特别情况:如果同时指定--force 和--backup 选项,而源文件和目标文件  
  57. 是同一个已存在的一般文件的话,cp 会将源文件备份。  

#环境准备

[plain] view plain copy
  1. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir1  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir2  
  3. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ touch dir1/f1.txt  
  4. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ touch dir2/f2.txt  
  5. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cd dir1  
  6. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al  
  7. total 0  
  8. drwxr-xr-x  3 bixiaopeng  staff  102  9 18 16:15 .  
  9. drwxr-xr-x  4 bixiaopeng  staff  136  9 18 16:14 ..  
  10. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f1.txt  

#拷贝两个文件

[plain] view plain copy
  1. # 拷贝f1.txt为cpf1.txt  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp f1.txt cpf1.txt  
  3. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al  
  4. total 0  
  5. drwxr-xr-x  4 bixiaopeng  staff  136  9 18 16:23 .  
  6. drwxr-xr-x  4 bixiaopeng  staff  136  9 18 16:14 ..  
  7. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:23 cpf1.txt  
  8. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f1.txt  

#采用交互方式拷贝文件

[plain] view plain copy
  1. #采用交互方式将文件f1.txt拷贝为cpf12.txt  
  2. #1. 如果cpf12.txt不存在,则直接复制成功  
  3. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt  
  4. #2. 如果cpf12.txt已存在,则会出现提示是否覆盖原来的,y就会覆盖  
  5. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt  
  6. overwrite cpf12.txt? (y/n [n]) y  
  7. #3. 如果cpf12.txt已存在,则会出现提示是否覆盖原来的,n就不会覆盖  
  8. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt  
  9. overwrite cpf12.txt? (y/n [n]) n  
  10. not overwritten  
  11. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al  
  12. total 0  
  13. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:40 .  
  14. drwxr-xr-x  4 bixiaopeng  staff  136  9 18 16:14 ..  
  15. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:23 cpf1.txt  
  16. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:40 cpf12.txt  
  17. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f1.txt  
 

#强制覆盖

[plain] view plain copy
  1. #如果cpf12.txt已经存在,也可以使用强制覆盖  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -f f1.txt cpf12.txt  

#拷贝目录

[plain] view plain copy
  1. #将目录dir1复制成dir11  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -R dir1 dir11  
  3. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir11  
  4. total 0  
  5. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:50 .  
  6. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:50 ..  
  7. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:50 cpf1.txt  
  8. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:50 cpf12.txt  
  9. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:50 f1.txt  
  10.    

#同时拷贝文件和目录

[plain] view plain copy
  1. #新建一个目录dir3   
  2. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir3  
  3. #将某些文件和目录复制到dir3目录下  
  4. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -R dir11/cpf1.txt dir11/cpf12.txt dir1 dir3  
  5. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir3  
  6. total 0  
  7. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:53 .  
  8. drwxr-xr-x  6 bixiaopeng  staff  204  9 18 16:52 ..  
  9. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:53 cpf1.txt  
  10. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:53 cpf12.txt  
  11. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:53 dir1  
 

#拷贝的文件保留属性

[plain] view plain copy
  1. # -p 或 --preserve    保留源文件或目录的属性,包括所有者、所属组、权限与时间  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -p dir2/f2.txt f2.txt   
  3. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al  
  4. total 0  
  5. drwxr-xr-x  7 bixiaopeng  staff  238  9 18 16:58 .  
  6. drwxr-xr-x@ 7 bixiaopeng  staff  238  9 18 15:43 ..  
  7. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:40 dir1  
  8. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:50 dir11  
  9. drwxr-xr-x  3 bixiaopeng  staff  102  9 18 16:15 dir2  
  10. drwxr-xr-x  5 bixiaopeng  staff  170  9 18 16:53 dir3  
  11. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f2.txt  
  12.    
  13. # -P 或 --parents     保留源文件或目录的路径,此路径可以是绝对路径或相对路径,且目的目录必须已经存在  
  14. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -P dir2/f2.txt f3.txt   
  15.    
  16. # -b备份,MAC上不支持该命令,linux系统上可以用  

#目录合并及文件覆盖

[plain] view plain copy
  1. #查看目录dir1  
  2. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir1  
  3. total 0  
  4. drwxr-xr-x  7 bixiaopeng  staff  238  9 18 17:27 .  
  5. drwxr-xr-x  8 bixiaopeng  staff  272  9 18 17:00 ..  
  6. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:23 cpf1.txt  
  7. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:48 cpf12.txt  
  8. drwxr-xr-x  2 bixiaopeng  staff   68  9 18 17:27 ddddir  
  9. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f1.txt  
  10. -rw-r--r--  1 bixiaopeng  staff    0  9 18 17:27 f2.txt  
  11. #查看目录dir2  
  12. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir2  
  13. total 0  
  14. drwxr-xr-x  4 bixiaopeng  staff  136  9 18 17:29 .  
  15. drwxr-xr-x  8 bixiaopeng  staff  272  9 18 17:00 ..  
  16. -rw-r--r--  1 bixiaopeng  staff    0  9 18 17:28 f1.txt  
  17. -rw-r--r--  1 bixiaopeng  staff    0  9 18 17:28 f2.txt  
  18. # 合并两个目录   
  19. # -f 强制覆盖  -r 拷贝目录和文件  -v 显示过程  -p 保留原有属性  
  20. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -frvp dir1/* dir2  
  21. dir1/cpf1.txt -> dir2/cpf1.txt  
  22. dir1/cpf12.txt -> dir2/cpf12.txt  
  23. dir1/ddddir -> dir2/ddddir  
  24. dir1/f1.txt -> dir2/f1.txt  
  25. dir1/f2.txt -> dir2/f2.txt  
  26. #再查看dir2全部都复制过来啦  
  27. bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir2  
  28. total 0  
  29. drwxr-xr-x  7 bixiaopeng  staff  238  9 18 17:31 .  
  30. drwxr-xr-x  8 bixiaopeng  staff  272  9 18 17:00 ..  
  31. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:23 cpf1.txt  
  32. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:48 cpf12.txt  
  33. drwxr-xr-x  2 bixiaopeng  staff   68  9 18 17:27 ddddir  
  34. -rw-r--r--  1 bixiaopeng  staff    0  9 18 16:15 f1.txt  
  35. -rw-r--r--  1 bixiaopeng  staff    0  9 18 17:27 f2.txt  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值