cp命令是在linux中用来复制文件或者目录最常用的命令之一。cp命令的功能很强大,也有很多的用法。

cp命令:

    cp - copy files and directories

语法:

    cp [OPTION]... [-T] SOURCE    DESTS

    cp [OPTION]... SOURCE... DIRECTORY

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

选项:

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

    效果和同时指定“-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

    类似--backup 但不接受参数

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

    不复制源文件只复制链接名

-f, --force              if  an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)

    强制复制,如果目标文件无法打开则将其移出并重试(当 -n选项存在时则不需要此选项)

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

    交互式,覆盖前询问(使前面的-n选项失效)

-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)

    不覆盖已存在的文件(使前面的 -i 选项失效)

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

    不跟随源文件中的符号链接

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

    类似与 --preserve=mode,ownership,timestamps,一起复制文件的权限,属主,属组

--preserve[=ATTR_LIST] 【属性列表】

preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

    保持指定的属性(默认:权限,属主,时间戳),也可添加附加属性(安全上下文,链接属性,文件的扩展属性等)

-c     deprecated, same as --preserve=context

    类似于--preserve=context

--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

    控制下文

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

    对源文件创建软链接而不复制文件

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

    用-b参数备份目的文件后,备份文件的字尾会被叫上一个备份字符串,默认的备份字符串是符号“~”

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

    复制源文件的所有参数到目的文件

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

    把目标文件当作普通文件

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

    复制的文件或目录存放的文件系统,必须与cp命令执行时 所处的文件系统相同,否则不复制,也不处理位于其他分区的文件

-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

    显示复制过程

--help        display this help and exit

    显示在线帮助

--version              output version information and exit

    显示版本信息




使用方法:

cp SRC(源文件)     DEST(目标文件)

SRC是文件:

如果目标不存在:新建DEST,并将SRC中内容填充至 DEST中

spacer.gifwKioL1eavSKRmQNxAAATava-Yrs627.png

如果目标存在:

如果DEST是文件:将SRC中的内容覆盖至DEST中 基于安全,建议为cp命令使用-i选项

spacer.gifwKioL1eavUaTZH2PAAATYPdaLxg284.png

如果DEST是目录:在DEST下新建与原文件同名的文 件,并将SRC中内容填充至新文件中

spacer.gifwKioL1eavW_jQar1AAAMcFZetKY478.png




cp   SRC...     DEST

SRC...:多个文件 DEST必须存在,且为目录,其它情形均会出错;

wKiom1eavZHy8ueYAAAv_Y1Ky_U973.pngspacer.gif



cp      SRC       DEST

SRC是目录:此时使用选项:-r

如果DEST不存在:则创建指定目录,复制SRC目录中所 有文件至DEST中;

spacer.gifwKioL1eavb_xT1NbAAAZ3oPq5-U116.png

如果DEST存在: 如果DEST是文件:报错 ;如果DEST是目录则复制目录及内容

spacer.gifwKiom1eaveWBvFLtAAAjZl5GDmg193.png

cp复制关系表

wKioL1eavknT8QRHAAFV7FWb4rs670.png

例:1、使用别名命令,每日将/etc/目录下所有文件, 备份到/testdir/下独立的新目录下,并要求新目录 格式为 backupYYYY-mm-dd ,备份过程可见

[root@localhost ~]# alias backup='cp -rv /etc/ /testdir/backup`date +%F` '

[root@localhost ~]# backup

spacer.gifwKioL1eavlvAELhFAAAuStx0iu4773.png

2、先创建/testdir/rootdir目录,再复制/root所有 下文件到该目录内,并要求保留原有权限

[root@localhost ~]# cp --preserve=mode -r /root /testdir/rootdir


spacer.gifwKiom1eavm3yXFaUAABInQK-00g760.png