Linux(Centos 7.6)命令详解:cp

1.命令作用

拷贝/复制源文件/目录到目标文件/目录,或者拷贝多个源文件/目录到目标目录(Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.)。

2.命令语法

Usage: cp [OPTION]... [-T] SOURCE DEST
       or:  cp [OPTION]... SOURCE... DIRECTORY
       or:  cp [OPTION]... -t DIRECTORY SOURCE...

3.参数详解

OPTION:

  • -a, --archive,与-dR --preserve=all相同,目录拷贝时常用,会归档拷贝目录下所有内容
  • --attributes-only,不要复制文件数据,只复制属性
  • --backup[=CONTROL],对每个已存在的目标文件进行备份,CONTROL可设置如下参数:
    • none, off,永远不要做备份(即使提供了--backup参数)
    • numbered, t,进行编号备份
    • existing, nil,如果存在编号备份,则编号,否则简单
    • simple, never,总是做简单的备份
    • 备份名后缀,默认为'~'符号,除非使用--suffix进行设置(实际测试无明显效果)
  • -b,类似于--backup,但-b不接受任何参数,即只能进行简单备份
  • -f, --force,如果无法打开现有的目标文件,请将其删除并重试(当还使用-n选项时,此选项将被忽略)(无明显效果)
  • -i, --interactive,覆盖前提示(覆盖前面的-n选项)
  • -H,单独符号链接复制时是复制其链接的原始文件,添加-r参数递归复制目录是复制符号链接本身
  • -l, --link,为源文件创建硬链接文件,而不是复制源文件
  • -L, --dereference,单独符号链接复制与-H相同,添加-r参数递归复制目录还是复制的其链接原始文件
  • -n, --no-clobber,如果复制目标文件存在,不要覆盖现有目标文件,即复制目标文件存在时执行无效果(覆盖之前的-i选项)
  • -P, --no-dereference,始终复制符号链接本身而不是链接的原始文件,可能会出现空连接情况与-L相反
  • -p,与--preserve=mode,ownership,timestamps相同
  • --preserve[=ATTR_LIST],复制时保留原文件的指定属性,可以指定mode(默认),ownership,timestamps,还可以指定context, links, xattr,all
  • -c,已弃用,与--preserve=context相同
  • --no-preserve=ATTR_LIST,复制时不要保留的指定属性,参考--preserve参数
  • --parents,复制文件或目录时保持原路径结构‌(无明显效果)
  • -R, -r, --recursive,递归地拷贝目录
  • --reflink[=WHEN],控制克隆复制,当指定--reflink[=always]时,执行轻量级复制,其中数据块仅在被修改时进行复制,如果无法做到这一点,则复制失败;或者指定了--reflink=auto,则返回到标准复制
  • --remove-destination,在尝试打开每个现有的目标文件之前删除它(与--force相反)(无明显效果)
  • --sparse=WHEN,控制稀疏文件的创建,稀疏文件是内容大多为空时更有效率地使用文件系统的空间,WHEN可取never、auto、always
  • --strip-trailing-slashes,从每个SOURCE参数中删除任何尾随斜杠
  • -s, --symbolic-link,制作符号链接而不是复制,即将源文件创建一个符号链接
  • -S, --suffix=SUFFIX,与--backup命令一起使用时,指定/覆盖默认的备份名后缀(无明显效果)
  • -t, --target-directory=DIRECTORY,将所有原文件复制到目标目录中
  • -T, --no-target-directory,将DEST视为普通文件(无明显效果)
  • -u, --update,仅在源文件比目标文件版本更新,或目标文件缺失/不存在时复制
  • -v, --verbose,解释正在做什么
  • -x, --one-file-system,留在这个文件系统上(无明显效果)
  • -Z,将目标文件的SELinux安全上下文设置为默认类型(无明显效果)
  • --context[=CTX],类似-Z,或者如果指定了CTX,则将SELinux或SMACK安全上下文设置为CTX(无明显效果)

4.常用用例

4.1.文件复制

[root@node2 Desktop]# ll
total 4
drwxr-xr-x. 2 root root  6 Feb 28 21:33 dir2
-rw-r--r--. 1 root root 50 Feb 28 21:31 file1.txt
[root@node2 Desktop]# 
[root@node2 Desktop]# cp file1.txt file2.txt 
[root@node2 Desktop]# cp file1.txt file2.txt dir2/   ## 拷贝多个文件到目录
[root@node2 Desktop]# 
[root@node2 Desktop]# ll
total 8
drwxr-xr-x. 2 root root 40 Feb 28 21:33 dir2
-rw-r--r--. 1 root root 50 Feb 28 21:31 file1.txt
-rw-r--r--. 1 root root 50 Feb 28 21:33 file2.txt
[root@node2 Desktop]# ll dir2/
total 8
-rw-r--r--. 1 root root 50 Feb 28 21:33 file1.txt
-rw-r--r--. 1 root root 50 Feb 28 21:33 file2.txt
[root@node2 Desktop]# 

4.2.目录递归复制

[root@node2 ~]# tree Desktop/    ## 该目录下有文件和目录
Desktop/
├── dir2
│   ├── file1.txt
│   └── file2.txt
├── file1.txt
└── file2.txt

1 directory, 4 files
[root@node2 ~]# tree dir111/     ## 该目录为空目录
dir111/

0 directories, 0 files
[root@node2 ~]# 
[root@node2 ~]# cp Desktop/ dir111      ## 直接拷贝目录会报错
cp: omitting directory ‘Desktop/’
[root@node2 ~]# cp Desktop/* dir111
cp: omitting directory ‘Desktop/dir2’   ## 间接拷贝目录也会报错
[root@node2 ~]# 
[root@node2 ~]# 
[root@node2 ~]# rm -rf dir111/*
[root@node2 ~]# cp -r Desktop/ dir111/  ## 添加-r参数递归拷贝复制,目录复制拷贝时一般使用-r参数
[root@node2 ~]# tree dir111/
dir111/
└── Desktop
    ├── dir2
    │   ├── file1.txt
    │   └── file2.txt
    ├── file1.txt
    └── file2.txt

2 directories, 4 files
[root@node2 ~]# 

4.3.保留属性复制

[root@node2 Desktop]# ll
total 8
drwxr-xr-x. 2 root root 40 Feb 28 21:33 dir2
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:31 file1.txt    ## 文件为qwer用户及用户组
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:33 file2.txt    ## 文件为qwer用户及用户组
[root@node2 Desktop]# 
[root@node2 Desktop]# cp file1.txt file3.txt
[root@node2 Desktop]# cp -p ownership file2.txt file4.txt
cp: target ‘file4.txt’ is not a directory
[root@node2 Desktop]# cp --preserve=ownership file2.txt file4.txt   ## 是执行用户属性
[root@node2 Desktop]# ll
total 16
drwxr-xr-x. 2 root root 40 Feb 28 21:33 dir2
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:31 file1.txt
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:33 file2.txt   ## 默认拷贝时不会保留用户属性的
-rw-r--r--. 1 root root 50 Feb 28 21:47 file3.txt
-rw-r--r--. 1 qwer qwer 50 Feb 28 21:49 file4.txt   ## 保留了拷贝源文件的用户属性
[root@node2 Desktop]# 

4.4.备份及目标名存在情况处理

参考《Linux(Centos 7.6)命令详解:mv》博文参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆是浪个

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值