Linux下cp命令

  • 命令概述

cp(copy ;拷贝,复制)命令主要用于复制文件或目录。命令可以用于:

  1. 复制文件或目录;
  2. 建立链接文件;
  3. 对比两文件的新旧;
  4. 文件重命名等;
  • 命令语法 

cp [OPTION]... [-T] SOURCE DEST                --cp [选项]...[-T] 源目标
cp [OPTION]... SOURCE... DIRECTORY        --cp [选项]...源...目录
cp [OPTION]... -t DIRECTORY SOURCE...     --cp [选项]...-t 目录源... 

  • 参数说明

-a 通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容。作用等于dpR参数组合
-d 复制时保留链接,相当于 Windows 系统的快捷方式
-f 覆盖已经存在的目标文件而不给出提示
-i 与 -f 选项相反,在覆盖目标文件之前给出提示,要求用户确认是否覆盖,回答 y 时目标文件将被覆盖
-p 除复制文件的内容外,还把修改时间和访问权限也复制到新文件中
-r 若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件
-l 不复制文件,只是生成链接文件
  • 命令示例

1. cp :复制源文件到目标目录下;

[root@devops data]# cp a.sh po
[root@devops data]# cd po
[root@devops po]# ll
total 0
-rw-r--r-- 1 root root  0 Mar 22 05:25 a.sh

2. cp :复制多个或所有(用 * 代替)源文件到目标目录下;

[root@devops psp]# cp * /data/po/
[root@devops psp]# ll /data/po/
total 0
-rw-r--r-- 1 root root  0 Mar 22 06:26 1.txt
-rw-r--r-- 1 root root  0 Mar 22 06:26 2.txt

3. cp :复制源文件并重命名;

[root@devops psp]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 22 06:06 1.txt
[root@devops psp]# cp 1.txt 2.txt
[root@devops psp]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 22 06:06 1.txt
-rw-r--r-- 1 root root 0 Mar 22 06:07 2.txt

4. cp -v :复制源文件到上一级(..)目录并显示详细信息;

[root@devops psp]# cp -v 1.txt 2.txt ..
‘1.txt’ -> ‘../1.txt’
‘2.txt’ -> ‘../2.txt’
[root@devops psp]# cd ..
[root@devops pc]# ll
total 0
-rw-r--r-- 1 root root  0 Mar 22 06:10 1.txt
-rw-r--r-- 1 root root  0 Mar 22 06:10 2.txt
drwxr-xr-x 2 root root 32 Mar 22 06:07 psp

5. cp -vr :(也可以写成:cp -r -v) 递归复制,复制文件夹以及下面所有文件到目标目录下;

[root@devops psp]# ll
total 0
drwxr-xr-x 2 root root 32 Mar 22 06:41 test1
drwxr-xr-x 2 root root  6 Mar 22 06:41 test2
[root@devops psp]# cp -vr test1 test2
‘test1’ -> ‘test2/test1’
‘test1/1.txt’ -> ‘test2/test1/1.txt’
‘test1/2.txt’ -> ‘test2/test1/2.txt’
[root@devops psp]# ll test2
total 0
drwxr-xr-x 2 root root 32 Mar 22 06:41 test1
[root@devops test1]# ll test2/test1
total 0
-rw-r--r-- 1 root root 0 Mar 22 06:34 1.txt
-rw-r--r-- 1 root root 0 Mar 22 06:34 2.txt

6. cp -i :将询问是否覆盖目标路径上的相同文件(回复用:y/n);

[root@devops test1]# cp -i 1.txt /test2
cp: overwrite ‘/test2’? y

7. cp -f : cp -i 正好相反,将直接复制覆盖目标路径上的相同文件,不会询问;

[root@devops psp]# cp -f 1.txt /test1

8. cp -l :创建硬链接(Hard Link),不复制文件,只创建该源文件的硬链接文件;

[root@devops p1]# cp -l /data/p1/3.txt /data/po
[root@devops p1]# ll /data/p1
total 0
-rw-r--r-- 2 root root 0 Mar 22 07:17 3.txt

9. cp -s :创建软链接(Symbolic Link),不复制文件,只创建该源文件的软链接文件;

扫描二维码关注公众号,回复: 14726890 查看本文章
[root@devops test1]# cp -s 1.txt ./test1
[root@devops test1]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 22 06:34 1.txt
-rw-r--r-- 1 root root 0 Mar 22 06:34 2.txt
lrwxrwxrwx 1 root root 5 Mar 22 10:47 test1 -> 1.txt

10. cp -p :复制源文件到目标目录且保留所有文件属性(包括所有者、所属组、权限与时间);

[root@devops test1]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 22 06:41 1.txt
[root@devops test1]# cp -p 1.txt ..
[root@devops test1]# ll ..
total 0
-rw-r--r-- 1 root root  0 Mar 22 06:41 1.txt
drwxr-xr-x 2 root root 19 Mar 22 10:53 test1

11. cp -u :比较两个文件的新旧,当前文件的比后文件新时,实现内容(文件名不复制)复制;

[root@devops psp]# cp -u -v 1.txt 2.txt 
cp: overwrite ‘2.txt’? y
‘1.txt’ -> ‘2.txt’

猜你喜欢

转载自blog.csdn.net/m0_57719465/article/details/123646485