一、命令详解
1.命令说明
cp
是copy
的缩写,主要是用来复制文件或者目录。cp命令可以将一个或者多个文件、目录复制到一个制定的文件或目录下,也可以同时复制多个文件到一个指定的目录中。
默认情况,
cp
会被设置一个别名cp is aliased to 'cp -i'
,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。
注意:
但是如果是在shell脚本中执行cp
时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式不同。
当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。
2.语法格式
cp [OPTION]... [-T] SOURCE DEST
cp [选项] ... [-T] 源文件 目标文件cp [OPTION]... SOURCE... DIRECTORY
cp [选项] ... 源文件 ... 目录cp [OPTION]... -t DIRECTORY SOURCE...
cp [选项] ... -t 目录 源文件...
3.选项描述
DESCRIPTION-a, --archive same as -dR --preserve=all--attributes-only don’t copy the file data, just the attributes--backup[=CONTROL] make a backup of each existing destination file#此参数的效果和同时指定"-dpR"或"-dR --preserve=all"参数相同。#不复制数据,仅仅复制属性-b like --backup but does not accept an argument--copy-contents copy contents of special files when recursive#删除覆盖目标文件之前的备份,创建备份,备份文件会在字尾加上一个备份字符串~。-d same as --no-dereference --preserve=links#当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录。 -f, --force if an existing destination file cannot beopened, remove it and try again (this optionis ignored when the -n option is also used)#强行复制文件或目录,不论目标文件或目录是否已存在。 #如果目标文件无法打开则将其移除并重试(当-n 选项存在时则不需再选此项)。-i, --interactive prompt before overwrite (overrides a previous -noption)-H follow command-line symbolic links in SOURCE#覆盖既有文件之前先询问用户(使前面的 -n 选项失效)。-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#不覆盖已存在的文件(该参数会覆盖 -i 的功能)a previous -i option)-P, --no-dereference never follow symbolic links in SOURCE#不援引#不遵循源文件的符号链接-p same as --preserve=mode,ownership,timestamps--preserve[=ATTR_LIST] preserve the specified attributes (default:mode,ownership,timestamps), if possibleadditional attributes: context, links, xattr,all#等同于 --preserve=mode,ownership,timestamps:保存权限、所属关系、时间戳-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--remove-destination remove each existing destination file beforeattempting to open it (contrast with --force)--sparse=WHEN control creation of sparse files. See below--strip-trailing-slashes remove any trailing slashes from each SOURCEargument-s, --symbolic-link make symbolic links instead of copying#创建符号链接,而不是复制-S, --suffix=SUFFIX override the usual backup suffix#在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀;-t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY#将所有源文件的参数复制到目录中【cp的三大用法之一】,如果用-t,后面必须接" DIRECTORY"。-T, --no-target-directory treat DEST as a normal file#将目标文件当成普通文件,而不考虑是否为目录-u, --update copy only when the SOURCE file is newerthan the destination file or when thedestination file is missing#仅当源文件比目标文件新或者目标文件缺失时才复制-v, --verbose explain what is being done#说明操作过程-x, --one-file-system stay on this file system#留在这个文件系统中-Z set SELinux security context of destinationfile to default type#安全增强型 linux 安全上下文设置为默认类型
二、命令示例
案例1:一个文件复制到不存在的文件 #cp
一个文件复制到不存在的文件,相当于改名。
cp 源文件 目标文件
源文件只有一个,不添加任何参数,简单的文件复制。
文件内容、权限、归属用户、用户组等等属性没有改变。
atime、mtime、ctime 改变成执行命令的日期。
[root@centos7 app]#cp f1 f11
[root@centos7 app]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat f11
word
1234
aaaaa
bbbbb
[root@centos7 app]#stat f1
File: ‘f1’
Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 76 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 22:34:51.316995192 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-13 22:34:48.598995062 +0800
Birth: -
[root@centos7 app]#stat f11
File: ‘f11’
Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 80 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 16:36:42.145440759 +0800
Modify: 2022-12-14 16:36:33.672440354 +0800
Change: 2022-12-14 16:36:33.672440354 +0800
Birth: -
案例2:一个文件复制到已存在的文件 #cp
一个文件复制到已存在的文件,将源文件中的内容覆盖至目的地的文件内容中。
默认情况设置别名
cp is aliased to 'cp -i'
。
如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。
注意数据丢失风险!
[root@centos7 app]#cat f2
hello linux
[root@centos7 app]#cp f2 f111
cp: overwrite ‘f111’? yes
[root@centos7 app]#cat f111
hello linux
案例3:一个文件复制到已存在的目录 #cp
cp 源文件 目标目录
,一个文件复制到已存在的目录 ,会直接复制到目的地的目录下。
在目的地的文件夹下新建与原文件同名的文件并将源文件中内容填充至新文件中。
[root@centos7 ~]#cat fcptest
this is test cp
[root@centos7 ~]#cp /root/fcptest /app/testdir1
[root@centos7 ~]#cd /app
[root@centos7 app]#cd testdir1/
[root@centos7 testdir1]#ls
fcptest
[root@centos7 testdir1]#cat fcptest
this is test cp
案例4:多个文件复制#cp
多个文件复制到不存在的文件,提示错误,错误用法。
多个文件复制到已存在的文件,提示错误,错误用法。
多个文件复制到已存在的目录,会复制到目的地的目录下,内容与原文件一直。
不能多个文件拷贝到一个文件上,但是能拷贝到一个文件夹里
[root@centos7 app]#cp f1 f2 f4 f5 /app/fffff #不存在的路径
cp: target ‘/app/fffff’ is not a directory
[root@centos7 app]#cp f1 f2 f4 f5 /app/dir1 #存在的路径
[root@centos7 app]#ls dir1
dir2 f1 f2 f4 f5
案例5:目录复制到文件
目录复制须使用 -r (递归)选项。
cp -r 源目录 目的文件
目录复制到不存在同名的文件的路径中,会创建指定目的地的同名目录,复制源目录中所有文件至目标路径下。
[root@centos7 app]#ls /root
Desktop Downloads f{1-9}{a-b} fcptest Pictures Templates
Documents f[1-9][a-b] f333 Music Public Videos
[root@centos7 app]#cp -r /app/dir1 /root
[root@centos7 app]#ls /root
Desktop Documents f[1-9][a-b] f333 Music Public Videos
dir1 Downloads f{1-9}{a-b} fcptest Pictures Templates
[root@centos7 app]#ls /root/dir1/
dir2 f1 f2 f4 f5
目录复制到已存在同名的文件,会报错,错误用法。
[root@centos7 ~]#cp -r /app/testdir1 /root/f333
cp: cannot overwrite non-directory ‘/root/f333’ with directory ‘/app/testdir1’
案例6:目录复制到已存在的目录 #cp -r
cp -r 源目录 目的目录
目录复制到已存在目录中,会在目的地目录下创建同名目录,复制源目录中所有内容至同名目录下。
[root@centos7 app]#ls dir1
dir2 f1 f2 f4 f5 #源路径下有文件夹和文件
[root@centos7 app]#ls /root/a #目标文件夹为空
[root@centos7 app]#cp -r /app/dir1 /root/a/
[root@centos7 app]#ls /root/a
dir1 #复制后,创建同名文件夹
[root@centos7 dir1]#cd /root/a/dir1/
[root@centos7 dir1]#ls
dir2 f1 f2 f4 f5 #与源路径下有文件夹和文件一致。
案例7:#cp -a 保留属性,归档
复制的时候将源文件的所有属性保留,链接、文件属性等。并复制指定目录下的所有内容,常用于复制目录。
也就是复制后的文件除了名字不同(不同路径也可以一样),其他属性都和源文件一模一样,可以用来文件存档。
归档复制,常用于备份,相当于cp -dr --preserve=all
cp -a
后除ctime 其他都一样。
[root@centos7 app]#stat f1File: ‘f1’Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 76 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 22:34:51.316995192 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-13 22:34:48.598995062 +0800Birth: -
[root@centos7 app]#cp -a f1 /root/
[root@centos7 app]#cd /root
[root@centos7 ~]#stat f1File: ‘f1’Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 1798161 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-13 22:34:51.316995192 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-14 22:20:02.467008407 +0800Birth: -
案例8:#cp -b 覆盖提示且备份
如果文件存在先询问是否要覆盖,并且备份覆盖的文件。备份的文件后面带
~
[root@centos7 app]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat f11
hey
[root@centos7 app]#cp -b f1 f11
cp: overwrite ‘f11’? yes
[root@centos7 app]#cat f11
word
1234
aaaaa
bbbbb
---------------------------------
[root@centos7 app]#ls
cat.txt f1 f111 f2_hardlink fff fo program
dir1 f11 f1_softlink f4 fi ls.log test.sh
dirname f11~ f2 f5 fifo_file merrys tomhome
[root@centos7 app]#cat f11
f11 f11~ f111 #带~是备份的文件
[root@centos7 app]#cat f11~
hey #原内容的备份。
案例9:#cp -d 复制符号链接
当复制的文件是符号连接时,把目标文件也建立为符号连接,并指向与源文件指向相同。
复制链接文件,保留链接属性。可以看见,复制完后,源文件和目的文件都指向f1
[root@centos7 app]#ll
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
-rw-r--r--. 2 root root 12 Dec 13 22:27 f2
-rw-r--r--. 2 root root 12 Dec 13 22:27 f2_hardlink
-rw-r--r--. 1 root root 22 Dec 14 22:36 f4
[root@centos7 app]#cp -d f1_softlink f4
cp: overwrite ‘f4’? y
[root@centos7 app]#ll
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
-rw-r--r--. 2 root root 12 Dec 13 22:27 f2
-rw-r--r--. 2 root root 12 Dec 13 22:27 f2_hardlink
lrwxrwxrwx. 1 root root 2 Dec 14 22:36 f4 -> f1
案例10:#cp -f 强制复制
强行复制文件或目录,不论目标文件或目录是否已存在。
因为
cp
是cp -i
命令的别名,所以这个命令也会输出提示。
-f 强制覆盖,其实是先删除,再创建。 如果是家目录,任何家目录的文件可以删掉或者覆盖掉
[root@centos7 fff]#touch f1
[root@centos7 fff]#cp -f ai f1
cp: overwrite ‘f1’? y
案例11:#cp -i 交互
默认
cp
是cp -i
命令的别名,已经加了-i
,所以会提示。
[root@centos7 fff]#cp -f ai f1
cp: overwrite ‘f1’? y
案例12:#cp -l 创建硬链接
使用硬链接(两个文件数据和属性同步,索引号相同,但删除其中一个不会影响另外的文件)。
就是一个文件,两个名字,给原文件再起了另一个名字。
格式:
cp -l 原文件(已存在) 目的文件(不存在)
目的文件不能是已存在,会报错。
[root@centos7 app]#cp -l a c
[root@centos7 app]#ll
-rw-r--r--. 2 root root 0 Dec 14 22:54 a
-rw-r--r--. 2 root root 0 Dec 14 22:54 c
[root@centos7 app]#stat aFile: ‘a’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 88 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 22:54:11.429106109 +0800
Modify: 2022-12-14 22:54:11.429106109 +0800
Change: 2022-12-14 22:57:40.847116095 +0800Birth: -
[root@centos7 app]#stat cFile: ‘c’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 88 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 22:54:11.429106109 +0800
Modify: 2022-12-14 22:54:11.429106109 +0800
Change: 2022-12-14 22:57:40.847116095 +0800Birth: -
案例13:#cp -L 复制链接源文件内容
复制链接文件的指向文件的内容,而不是复制链接本身文件。
[root@centos7 app]#cp -L f1_softlink zzzzzz
[root@centos7 app]#ll
-rw-r--r--. 1 root root 22 Dec 13 22:34 f1
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
-rw-r--r--. 1 root root 22 Dec 14 23:04 zzzzzz
[root@centos7 app]#cat f1 #原文件
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat zzzzzz #复制后的文件,内容与f1一样
word
1234
aaaaa
bbbbb
案例14:#cp -p 保留属性
复制的时候将文件的全部属性带入到新的文件中。
等同–preserv=mode, ownership, timestamp
[root@centos7 app]#cp -p f1 qwer
[root@centos7 app]#cat qwer
word
1234
aaaaa
bbbbb
[root@centos7 app]#stat f1File: ‘f1’Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 76 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 22:36:27.815055392 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-13 22:34:48.598995062 +0800Birth: -
[root@centos7 app]#stat qwer File: ‘qwer’Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 93 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 23:18:53.176176765 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-14 23:18:46.968176469 +0800Birth: -
案例15:#cp -P 复制链接
效果与
-d
相同,原样复制链接文件,保留链接属性。
[root@centos7 app]#cp -P f1_softlink qwer
cp: overwrite ‘qwer’? y
[root@centos7 app]#ll
lrwxrwxrwx. 1 root root 2 Dec 7 21:57 f1_softlink -> f1
lrwxrwxrwx. 1 root root 2 Dec 14 23:26 qwer -> f1
案例16:#cp -r/-R 递归
递归复制,可以将目录的所有子文件进行复制,即复制整个目录。
复制目录必用
[root@centos7 app]#cp -r dir1 fff
[root@centos7 app]#ll fff
total 0
drwxr-xr-x. 3 root root 58 Dec 14 23:35 dir1
案例17:#cp -s 创建软链接
对源文件建立符号连接(软连接),不可对目录创建。
与ln -s
效果完全相同。
[root@centos7 app]#cp -s a c
[root@centos7 app]#ll
total 52
-rw-r--r--. 1 root root 0 Dec 14 23:38 a
-rw-r--r--. 1 root root 0 Dec 14 23:38 b
lrwxrwxrwx. 1 root root 1 Dec 14 23:38 c -> a
案例18:#cp -S 后缀
备份后缀的符号。
root@centos7 app]#cp -S a b 1234
[root@centos7 app]#ll
-rw-r--r--. 1 root root 0 Dec 14 23:41 1234
-rw-r--r--. 1 root root 0 Dec 14 23:38 a
-rw-r--r--. 1 root root 0 Dec 14 23:38 b[root@centos7 app]#cp -S a b ~~~~~~~
[root@centos7 app]#ll
-rw-r--r--. 1 root root 0 Dec 14 23:43 ~~~~~~~
-rw-r--r--. 1 root root 0 Dec 14 23:41 1234
-rw-r--r--. 1 root root 0 Dec 14 23:38 a
-rw-r--r--. 1 root root 0 Dec 14 23:38 b
案例19:#cp --attributes-only
保留文件的属性,但不复制文件内容,相对于
cp -a
后将数据删除。
[root@centos7 app]#cp f1 abcdef --attributes-only
[root@centos7 app]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 app]#cat abcdef
[root@centos7 app]#stat f1File: ‘f1’Size: 22 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 76 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 22:36:27.815055392 +0800
Modify: 2022-12-13 22:34:48.598995062 +0800
Change: 2022-12-13 22:34:48.598995062 +0800Birth: -
[root@centos7 app]#stat abcdef File: ‘abcdef’Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 96 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 23:53:20.389275337 +0800
Modify: 2022-12-14 23:53:12.091274941 +0800
Change: 2022-12-14 23:53:12.091274941 +0800Birth: -
案例20:#cp --preserve[=ATTR_LIST]
保留一些特殊的属性:权限mode、归属ownership、时间戳timestamps,同 -p
,还包括上下文context、链接links、扩展xattar和全部all等属性。
mode:权限
ownership:属主属组
timestamp:
links
xattr
context
all
[root@centos7 ~]#cp --preserv=timestamps fcptest /app/f2
cp: overwrite ‘/app/f2’? yes
[root@centos7 ~]#stat fcptest File: ‘fcptest’Size: 16 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 125 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-12-14 17:21:20.612568478 +0800
Modify: 2022-12-14 17:21:08.205567886 +0800
Change: 2022-12-14 17:21:08.205567886 +0800Birth: -
[root@centos7 ~]#stat /app/f2File: ‘/app/f2’Size: 16 Blocks: 8 IO Block: 4096 regular file
Device: 805h/2053d Inode: 69 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2022-12-14 17:21:20.612568478 +0800
Modify: 2022-12-14 17:21:08.205567886 +0800
Change: 2022-12-15 00:16:26.307341423 +0800Birth: -
案例21:去掉提示技巧
使用cp命令复制文件时候,有时候会需要覆盖一些同名文件,覆盖文件的时候都会有提示。需要不停的按Y来确定执行覆盖,文件数量不多还好,但是要是几百个要按好多遍。
全部执行同意
\cp f1 f8
可以在命令前面加上\
[root@centos7 app]#cp f1 f8
cp: overwrite ‘f8’? yes
[root@centos7 app]#\cp f1 f8