简介

cp 命令,主要用来复制文件和目录,同时借助某些选项,还可以实现复制整个目录,以及比对两文件的新旧而予以升级等功能。

命令格式

cp [OPTION]… [-T] SOURCE DEST cp [OPTION]… SOURCE… DIRECTORY cp [OPTION]… -t DIRECTORY SOURCE…

常用参数

-r 递归复制(若给出的源文件是一个目录文件,复制该目录下所有的子目录和文件)。
-i 如果目标文件已经存在,则会询问是否覆盖
-f 覆盖已经存在的目标文件而不给出提示
-l 把目标文件建立为源文件的硬链接文件,而不是复制源文件
-s 把目标文件建立为源文件的软链接文件,而不是复制源文件
-d 如果源文件为软链接(对硬链接无效),则复制出的目标文件也为软链接
-p 复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间
-a 相当于 -dpr 选项的集合,通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容

复制的几种场景

  1. cp SRC DEST
  2. SRC是文件
  3. DEST不存在
  4. 新建 DEST 并将 SRC 中的内容填充到 DEST 中。
  5. DEST存在
  6. DEST为文件 :将 SRC 中的内容覆盖到 DEST 中。建议使用 -i 选项
  7. DEST为目录 :在 DEST 下新建与原文件同名的文件,并将 SRC 的内容填充到新文件中。
  8. cp SRC... DEST
  9. SRC... 多个文件
  10. DEST必须存在,且为目录。其他情况均会出错。
  11. cp SRC DEST
  12. SRC是目录 :此时使用选项 -r
  13. DEST不存在
  14. 创建指定目录,复制 SRC 目录中所有的文件到 DEST 中。
  15. DEST存在
  16. DEST是文件 : 报错
  17. DEST是目录 : 复制 SRC 文件夹到 DEST 中。
  1. ## 创建3个复制用的文件
  2. [root@localhost ~]# touch file_{a,b,c}.txt
  3. [root@localhost ~]# ls
  4. file_a.txt file_b.txt file_c.txt
  5. [root@localhost ~]# echo aaa > file_a.txt
  6. [root@localhost ~]# cat file_a.txt
  7. aaa
  8. ## 复制 file_a.txt 到一个并不存在的文件中
  9. [root@localhost ~]# cp file_a.txt a
  10. [root@localhost ~]# ll
  11. total 0
  12. -rw-r--r--. 1 root root 0 May 13 01:01 a
  13. -rw-r--r--. 1 root root 0 May 13 01:01 file_a.txt
  14. -rw-r--r--. 1 root root 0 May 13 01:01 file_b.txt
  15. -rw-r--r--. 1 root root 0 May 13 01:01 file_c.txt
  16. [root@localhost ~]# cat a
  17. aaa
  18. ## 复制文件到一个已经存在的文件中
  19. [root@localhost ~]# echo bbb > b
  20. [root@localhost ~]# cat b
  21. bbb
  22. [root@localhost ~]# cp file_a.txt b
  23. cp: overwrite b’? y ## 因为已经存在,所以会询问是否覆盖,后续会讲解强制覆盖
  24. [root@localhost ~]# cat b
  25. aaa
  26. ## 复制文件到一个目录中
  27. [root@localhost ~]# mkdir dir_a
  28. [root@localhost ~]# cp file_a.txt dir_a
  29. [root@localhost ~]# ll dir_a
  30. total 4
  31. -rw-r--r--. 1 root root 4 May 13 01:08 file_a.txt
  32. ## 复制多个文件,那么 test 必须是一个目录
  33. [root@localhost ~]# cp file_a.txt file_b.txt file_c.txt test
  34. cp: target test is not a directory ## test 不存在,所以报错
  35. [root@localhost ~]# mkdir test
  36. [root@localhost ~]# cp file_a.txt file_b.txt file_c.txt test
  37. [root@localhost ~]# ll test
  38. total 0
  39. -rw-r--r--. 1 root root 0 May 13 01:02 file_a.txt
  40. -rw-r--r--. 1 root root 0 May 13 01:02 file_b.txt
  41. -rw-r--r--. 1 root root 0 May 13 01:02 file_c.txt
  42. ## 复制目录到一个不存在的目录
  43. [root@localhost ~]# ll -d test
  44. drwxr-xr-x. 2 root root 57 May 13 01:02 test
  45. [root@localhost ~]# ll test
  46. total 0
  47. -rw-r--r--. 1 root root 0 May 13 01:02 file_a.txt
  48. -rw-r--r--. 1 root root 0 May 13 01:02 file_b.txt
  49. -rw-r--r--. 1 root root 0 May 13 01:02 file_c.txt
  50. [root@localhost ~]# cp test test2 ## 如果直接复制会报错,因为test是一个目录,所以需要使用 -r 选项
  51. cp: omitting directory test
  52. [root@localhost ~]# cp -r test test2
  53. [root@localhost ~]# ll test2
  54. total 0
  55. -rw-r--r--. 1 root root 0 May 13 01:10 file_a.txt
  56. -rw-r--r--. 1 root root 0 May 13 01:10 file_b.txt
  57. -rw-r--r--. 1 root root 0 May 13 01:10 file_c.txt
  58. ## 复制文件到一个已经存在的目录
  59. [root@localhost ~]# mkdir test3
  60. [root@localhost ~]# cp -r test test3
  61. [root@localhost ~]# ll -R test3
  62. test3:
  63. total 0
  64. drwxr-xr-x. 2 root root 57 May 13 01:12 test
  65. test3/test:
  66. total 0
  67. -rw-r--r--. 1 root root 0 May 13 01:12 file_a.txt
  68. -rw-r--r--. 1 root root 0 May 13 01:12 file_b.txt
  69. -rw-r--r--. 1 root root 0 May 13 01:12 file_c.txt

示例

  1. [root@localhost ~]# touch file_{a,b}.txt
  2. [root@localhost ~]# ll
  3. total 0
  4. -rw-r--r--. 1 root root 0 May 13 01:17 file_a.txt
  5. -rw-r--r--. 1 root root 0 May 13 01:17 file_b.txt
  6. ## -i 如果目标文件已经存在,则会询问是否覆盖
  7. [root@localhost ~]# cp file_a.txt file_b.txt
  8. [root@localhost ~]# cp -i file_a.txt file_b.txt
  9. cp: overwrite file_b.txt’? y
  10. ## -f:覆盖已经存在的目标文件而不给出提示。(默认就是 -f 不询问,强制覆盖)
  11. [root@localhost ~]# cp -f file_a.txt file_b.txt
  12. ## -l:把目标文件建立为源文件的硬链接文件,而不是复制源文件;
  13. [root@localhost ~]# ll
  14. total 4
  15. -rw-r--r--. 1 root root 3 May 13 01:22 file_a.txt
  16. -rw-r--r--. 1 root root 0 May 13 01:22 file_b.txt
  17. [root@localhost ~]# cp -l file_a.txt hard_file_a.txt
  18. [root@localhost ~]# ll -i *file_a.txt
  19. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt
  20. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt
  21. ## -s:把目标文件建立为源文件的软链接文件,而不是复制源文件;
  22. [root@localhost ~]# cp -s file_a.txt soft_file_a.txt
  23. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt
  24. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt
  25. 68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt
  26. ## -d:如果源文件为软链接(对硬链接无效),则复制出的目标文件也为软链接;
  27. ## 如果在复制软链接文件时不使用 "-d" 选项,则 cp 命令复制的是源文件,而不是软链接文件;
  28. ## 只有加入了 "-d" 选项,才会复制软链接文件。
  29. [root@localhost ~]# ll -i *file_a.txt
  30. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt
  31. 68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt
  32. 68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt
  33. [root@localhost ~]# cp soft_file_a.txt soft_1_file_a.txt
  34. [root@localhost ~]# cp -d soft_file_a.txt soft_2_file_a.txt
  35. [root@localhost ~]# ll -i soft_*file_a.txt
  36. 68216872 -rw-r--r--. 1 root root 3 May 13 01:37 soft_1_file_a.txt ## 实际复制的是软链接的源文件,而不是软链接文件
  37. 68216871 lrwxrwxrwx. 1 root root 10 May 13 01:35 soft_2_file_a.txt -> file_a.txt
  38. 68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt
  39. ## -p:复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间);
  40. [root@localhost ~]# ll file_a.txt
  41. -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt
  42. [root@localhost ~]# chown gkdaxue file_a.txt
  43. [root@localhost ~]# ll file_a.txt
  44. -rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt
  45. [root@localhost ~]# cp file_a.txt root_file_a.txt
  46. [root@localhost ~]# ll file_a.txt root_file_a.txt
  47. -rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt
  48. -rw-r--r--. 1 root root 3 May 13 01:42 root_file_a.txt ## 文件的属主和时间都发生了变化
  49. [root@localhost ~]# cp -p file_a.txt gkdaxue_file_a.txt
  50. [root@localhost ~]# ll file_a.txt root_file_a.txt gkdaxue_file_a.txt
  51. -rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt
  52. -rw-r--r--. 1 gkdaxue root 3 May 13 01:22 gkdaxue_file_a.txt ## 文件的属主和时间都被复制了下来
  53. -rw-r--r--. 1 root root 3 May 13 01:42 root_file_a.txt