linux 命令详解
本文主要内容来自Linux man 手册
命令名称:
cp ( copy )复制文件和目录
命令用法:
cp [选项]... [-T] 源文件 目标文件
cp [选项]... 源文件... 目录
cp [选项]... -t 目录 源文件...
命令概述:
拷贝源文件到目标文件,或者拷贝多个文件到目标目录
命令参数:
-a , --archive : 归档
相当于 -dR --preserve=all:保存所有属性
--attributes-only
不复制数据,仅仅复制属性
--backup[=CONTROL]
备份目标文件,备份文件后缀默认为'~',除非设置了--suffix(见后文)或者
SIMPLE_BACKUP_SUFFIX。版本控制可以由 --backup 和 VERSION_CONTROL
环境变量决定。
CONTROL 或 VERSION_CONTROL可以为以下几种:
none 或 off ,不使用备份,即使使用了 --backup 参数;
numbered 或 t,用数字表示备份版本
existing 或 nil,如果数字备份已存在,继续使用数字备份
simple 或 never,一直使用简单的备份(多次备份采用覆盖的方式)
作为一种特殊用法:当使用备份选项时(--backup 或 -b),如果目标文件已存在,则 cp 也会对
目标文件进行备份。
-b
和 --backup 类似,但是不带参数(简单备份)
--copy-contents
递归复制时,考虑文件的内容(默认不是如此???)
-d
等于 --no-dereference --preserve=links 保存符号链接【复制的是链接文件本身,而不是链接指
向的文件,同时不会破坏链接】
-f, --force 强制
如果现存的目标文件无法打开,则将其删除并重新尝试(当 -n 也被使用时,-f 参
数将不起作用)【不用它就对了,反正也看不懂@_@】
-i,--interactive 互动
覆盖前给予提示(当和 -n 同时使用时,-n 不起作用)
-H
遵循源文件的命令行链接 (???)
-l, --link 链接
为文件创建硬链接,而不是复制
-L, --dereference 援引
遵循源文件的符号链接【复制的是所链接的文件,而不是链接本身】
-n, --no-clobber 无冲突
不覆盖已存在的文件(该参数会覆盖 -i 的功能)
-P, --no-dereference 不援引
不遵循源文件的符号链接
-p
等同于 --preserve=mode,ownership,timestamps:保存权限、所属关系、时间戳
--preserve[=ATTR_LIST]
保存特殊属性(默认为:权限、拥有关系和时间戳,同 -p ),还可能的属性:上下文(context)、
链接(links)、扩展属性(xattr)和 all
--no-preserve=ATTR_LIST
不保存特殊属性
--parents
在目录下使用源文件的全名(???)
-R, -r, --recursive
递归复制
--reflink[=WHEN]
克隆、写时拷贝(写时拷贝是一种可以推迟甚至免除拷贝数据的技术)
WHEN 为always时,采用轻量级复制,即只有在源文件被修改时才进行复制,否则可能复制失败。
如果WHEN为auto,则使用标准复制。
--remove-destination
在尝试打开已存的目标文件之前将其删除(与 -force 相反)【明明是和 -f 差不多】
--sparse=WHEN
稀疏文件的创建(一种计算机文件,可百度了解详情),默认情况下,WHEN 为auto,如果源
文件被检测为稀疏文件,则目标文件也会是稀疏文件;如果WHEN为always,则无论源文件怎
样,目标文件都会被创建为稀疏文件;--sparse=never 可以禁止创建稀疏文件
--strip-trailing-slashes
删除每个参数后面的斜杠
-s, --symbolic-link
创建符号链接,而不是复制
-S, --suffix=SUFFIX
修改常用的备份后缀,备份文件后缀默认为'~',除非设置了--suffix 或者
SIMPLE_BACKUP_SUFFIX。
-t, --target-directory=DIRECTORY
将所有源文件的参数复制到目录中【cp的三大用法之一】,如果用-t,后面必须接" DIRECTORY"。
-T, --no-target-directory
将目标文件当成普通文件,而不考虑是否为目录(不知有啥用)
-u, --update
仅当源文件比目标文件新或者目标文件缺失时才复制
-v, --verbose 冗长
说明操作过程
-x, --one-file-system
留在这个文件系统中(???)
-Z
将安全增强型 linux 安全上下文设置为默认类型(???)
--context[=CTX]
和 -Z 类似,如果指定了 CTX,则将安全增强型 SElinux 或 SMACK 安全上下文设置为 CTX
(???)
--help 显示帮助信息
--version 显示版本信息
示例:
1. cp 源文件 目标文件
不添加任何参数选项,且源文件只有一个,为cp的第一种用法,简单的文件复制,部分属性被保留(比如权限,归属关系等等)
xiaohui@ubuntu:~/work/cp_learn$ cp text.c a.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l a.c text.c
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 13 15:34 a.c
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 12 23:05 text.c
xiaohui@ubuntu:~/work/cp_learn$
2. cp 源文件… 目录
不添加任何选项参数,源文件可以不止一个,目标文件必须为目录文件,这是cp的第二种用法,也就是批量复制。
xiaohui@ubuntu:~/work/cp_learn$ ls -l a.c b.c c.c dir_dest/
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 13 15:39 a.c
-rw-rw-r-- 1 xiaohui xiaohui 17 7月 13 15:39 b.c
-rw-rw-r-- 1 xiaohui xiaohui 54 7月 13 15:39 c.c
dir_dest/:
total 16
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 13 15:42 a.c
-rw-rw-r-- 1 xiaohui xiaohui 17 7月 13 15:42 b.c
-rw-rw-r-- 1 xiaohui xiaohui 54 7月 13 15:42 c.c
xiaohui@ubuntu:~/work/cp_learn$
3. cp -t 目录 源文件…
不添加任何选项参数(-t 参数比较特殊,是cp命令的第三种用法)第三种用法和第二种类似,只是有了-t参数,目录的位置可以随意改动,甚至可以放到源多个文件中间(但必须在-t后面)。
xiaohui@ubuntu:~/work/cp_learn$ cp -t dir_dest/ a.c b.c c.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l a.c b.c c.c dir_dest/
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 15:55 a.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 15:55 b.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 15:55 c.c
dir_dest/:
total 0
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 16:02 a.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 16:02 b.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 16:02 c.c
xiaohui@ubuntu:~/work/cp_learn$
“-t 目录”等同于“–target-directory=DIRECTORY”
“长选项的强制参数对于短选项也是强制的”,这句话指的就是像-t的这类选项:–target-deirectory这个长选项后面必须接参数,所以-t这个短选项也必须带参数(即目标目录),但不是用“=”相连,而是用空格。
4. cp -a 源文件/… 目标文件/目录
将源文件的所有属性保留,也就是复制后的文件除了名字不同,其他属性都和源文件一模一样,可以用来文件存档。
xiaohui@ubuntu:~/work/cp_learn$ cp text.c -a a.c && cp ln_test.c ln_dest.c -a
xiaohui@ubuntu:~/work/cp_learn$ ls -l text.c a.c ln_test.c ln_dest.c
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 12 23:05 a.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_dest.c -> dir1/ln_src.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
-rw-rw-r-- 1 xiaohui xiaohui 51 7月 12 23:05 text.c
xiaohui@ubuntu:~/work/cp_learn$
5. cp --attributes-only
保留文件的属性,但不复制文件内容,相对于cp -a 后将数据删除。
xiaohui@ubuntu:~/work/cp_learn$ cp text.c a.c --attributes-only
xiaohui@ubuntu:~/work/cp_learn$ ls -l text.c a.c
-rw-rw-r-- 1 xiaohui xiaohui 0 7月 13 16:19 a.c
-rw-rw-r-- 1 xiaohui xiaohui 12 7月 13 16:19 text.c
xiaohui@ubuntu:~/work/cp_learn$ cat text.c
I am test.c
xiaohui@ubuntu:~/work/cp_learn$ cat a.c
xiaohui@ubuntu:~/work/cp_learn$
6. cp --backup[=CONTROL] 源文件/… 目标文件/目录
如果CONTRL为simple或never时,使用简单的备份,即只有一个版本的备份。(备份方法:先将要备份的文件内容复制到一个中间文件中,然后再使用cp命令,将中间文件作为cp命令的目标文件,选择备份的选项,源文件可以任意指定(备份完成后中间文件的内容将变成源文件内容),复制完成后会生成一个和中间文件同名的备份文件)
xiaohui@ubuntu:~/work/cp_learn$ touch text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ ls text.c*
text.c text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cp text.c --backup=never text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cat text.c
I am test.c
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak~
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak
I am test.c
xiaohui@ubuntu:~/work/cp_learn$
首先,text.c.bak是一个空文件,经过第一次备份后,text.c的内容复制给了text.c.bak,同时产生了备份文件text.c.bak~,其内容和备份前的text.c.bak相同,为空。
如果再对text.c.bak进行一次备份:
xiaohui@ubuntu:~/work/cp_learn$ cp --backup=simple a.c text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cat a.c
I am a.c
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak~
I am test.c
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak
I am a.c
xiaohui@ubuntu:~/work/cp_learn$
可以看到,text.c.bak~ 的内容变成了之前 text.c.bak的内容,也就是备份成功,同时,text.c.bak的内容也变成了 a.c的内容。
上面这种为简单的备份,备份文件只有一个版本,如果想实现版本控制,则CONTROL可以选择numbered或t,也可以使用existing或nil,后面两个的作用是如果当前已经有了数字版本的备份,则继续使用,否则还是简单备份方式。
xiaohui@ubuntu:~/work/cp_learn$ cp text.c text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ echo "version_control" >> text.c
xiaohui@ubuntu:~/work/cp_learn$ cp text.c --backup=t text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cat text.c
I am test.c
version_control
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak
I am test.c
version_control
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak.~1~
I am test.c
xiaohui@ubuntu:~/work/cp_learn$ cp text.c --backup=nil text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cat text.c.bak.~2~
I am test.c
version_control
xiaohui@ubuntu:~/work/cp_learn$
text.c.bak.~1~ 和 text.c.bak.~2~ 为不同的备份版本。
除了用 --backup 参数,还可以通过设定环境变量VERSION_CONTROL的值来指定备份方式:
xiaohui@ubuntu:~/work/cp_learn$ export VERSION_CONTROL=numbered
xiaohui@ubuntu:~/work/cp_learn$ cp text.c text.c.bak --backup
xiaohui@ubuntu:~/work/cp_learn$ ls text.c*
text.c text.c.bak text.c.bak.~1~
xiaohui@ubuntu:~/work/cp_learn$
如果想一直不使用备份(即使添加了备份的选项参数,如 --backup),则可以将VERSION_CONTROL设为none或off。
xiaohui@ubuntu:~/work/cp_learn$ VERSION_CONTROL=none
xiaohui@ubuntu:~/work/cp_learn$ printenv | grep VERSION_CONTROL
VERSION_CONTROL=none
xiaohui@ubuntu:~/work/cp_learn$ ls text.c*
text.c text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cp text.c text.c.bak --backup
xiaohui@ubuntu:~/work/cp_learn$ ls text.c*
text.c text.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cp text.c text.c.bak --backup=t
xiaohui@ubuntu:~/work/cp_learn$ ls text.c*
text.c text.c.bak text.c.bak.~1~
xiaohui@ubuntu:~/work/cp_learn$
从上面的实验中,当同时存在VERSION_CONTROL和CONTROL时,以CONTROL为准。
7. cp -d 源文件/… 目标文件/目录
复制链接文件,保留链接属性。
xiaohui@ubuntu:~/work/cp_learn$ cp -d ln_test.c ln_dest.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l ln_test.c ln_dest.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 13 22:00 ln_dest.c -> dir1/ln_src.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$
8. cp -l 源文件/… 目标文件/目录
使用硬链接(两个文件数据和属性同步,索引号相同,但删除其中一个不会影响另外的文件)
xiaohui@ubuntu:~/work/cp_learn$ cp text.c -l hard_ln.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l text.c hard_ln.c -i
419796 -rw-rw-r-- 2 xiaohui xiaohui 28 7月 13 17:44 hard_ln.c
419796 -rw-rw-r-- 2 xiaohui xiaohui 28 7月 13 17:44 text.c
xiaohui@ubuntu:~/work/cp_learn$
9. cp -L 源文件/… 目标文件/目录
复制链接文件的指向文件,而不是复制链接本身(虽然它们数据一样,但属性完全不同)
xiaohui@ubuntu:~/work/cp_learn$ ls ln_test.c ln_dest.c dir1/ln_src.c -l
-rw-rw-r-- 1 xiaohui xiaohui 4 7月 12 19:51 dir1/ln_src.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月 13 22:09 ln_dest.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$
10. cp -P 源文件/… 目标文件/目录
效果和-d相同,原样复制链接文件,保留链接属性。
xiaohui@ubuntu:~/work/cp_learn$ cp ln_test.c -P ln_dest.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l ln_*
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 13 22:16 ln_dest.c -> dir1/ln_src.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$
11. cp -p 源文件/… 目标文件/目录
保留源文件的权限、归属关系、时间戳三个属性,当然,文件内容不算属性。
xiaohui@ubuntu:~/work/cp_learn$ cp ln_test.c a.c -p
xiaohui@ubuntu:~/work/cp_learn$ ls -l a.c ln_test.c
-rw-rw-rw- 1 xiaohui xiaohui 4 7月 12 19:51 a.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$ cat a.c
aaa
xiaohui@ubuntu:~/work/cp_learn$ cat ln_test.c
aaa
xiaohui@ubuntu:~/work/cp_learn$
12. cp --preserve[=ATTR_LIST] 源文件/… 目标文件/目录
保留一些特殊的属性:(权限mode、归属ownership、时间戳timestamps,同 -p ),还包括上下文context、链接links、扩展xattar和全部all等属性。
xiaohui@ubuntu:~/work/cp_learn$ cp ln_test.c --preserve=mode,ownership,timestamps,links a.c
xiaohui@ubuntu:~/work/cp_learn$ ls ln_test.c a.c dir1/ln_src.c -l
-rw-rw-rw- 1 xiaohui xiaohui 4 7月 12 19:51 a.c
-rw-rw-rw- 1 xiaohui xiaohui 4 7月 12 19:51 dir1/ln_src.c
lrwxrwxrwx 1 xiaohui xiaohui 13 7月 12 18:59 ln_test.c -> dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$
上面links属性貌似没太多作用,默认就会对链接进行引用。
13. cp --no-preserve=ATTR_LIST 源文件/… 目标文件/目录
不保存某些属性。
xiaohui@ubuntu:~/work/cp_learn$ cp ln_test.c --no-preserve=all a.c
xiaohui@ubuntu:~/work/cp_learn$ cp ln_test.c --no-preserve=timestamps b.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l a.c b.c dir1/ln_src.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月 13 22:49 a.c
-rw-rw-r-- 1 xiaohui xiaohui 4 7月 13 22:49 b.c
-rw-rw-rw- 1 xiaohui xiaohui 4 7月 12 19:51 dir1/ln_src.c
xiaohui@ubuntu:~/work/cp_learn$
咋感觉用了这个选项,不管等号后面怎么填,结果都差不多。。。
14. cp -R/-r 源目录… 目标目录
递归复制,可以将目录的所有子文件进行复制,即复制整个目录。
xiaohui@ubuntu:~/work/cp_learn$ cp dir1/ dir_dest/
cp: omitting directory 'dir1/'
xiaohui@ubuntu:~/work/cp_learn$ cp dir1/ dir_dest/ -r
xiaohui@ubuntu:~/work/cp_learn$ ls dir_dest/
dir1
xiaohui@ubuntu:~/work/cp_learn$
15. cp -s 源文件/… 目标文件/目录
创建符号链接(软链接),相当于Windows系统的快捷方式。
xiaohui@ubuntu:~/work/cp_learn$ cp -s /home/xiaohui/work/test.c ln_dest.c
xiaohui@ubuntu:~/work/cp_learn$ ls -l ln_dest.c test.c
lrwxrwxrwx 1 xiaohui xiaohui 25 7月 13 23:06 ln_dest.c -> /home/xiaohui/work/test.c
-rw-rw-r-- 1 xiaohui xiaohui 29 7月 13 22:19 test.c
xiaohui@ubuntu:~/work/cp_learn$
不知道是不是比ln好用。
16. cp -S <SUFFIX> 源文件/… 目标文件/目录
备份后缀的符号,默认为~,同 --suffix=SUFFIX。
xiaohui@ubuntu:~/work/cp_learn$ cp test.c test.c.bak
xiaohui@ubuntu:~/work/cp_learn$ cp test.c test.c.bak --backup=simple -S @_@
xiaohui@ubuntu:~/work/cp_learn$ ls
dir1 dir2 dir3 dir_dest ln_test.c test.c test.c.bak test.c.bak@_@
xiaohui@ubuntu:~/work/cp_learn$
测试发现:版本备份(数字记录备份)只能用~后缀
man手册
以下为 cp 的参考手册原文:
CP(1) User Commands CP(1)
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options
too.
-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
-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 be opened, remove it and
try again (this option is ignored when the -n option is also
used)
-i, --interactive
prompt before overwrite (overrides a previous -n option)
-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)
-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,time‐
stamps), if possible additional attributes: context, links,
xattr, all
--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 before attempting to open
it (contrast with --force)
--sparse=WHEN
control creation of sparse files. See below
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument
-s, --symbolic-link
make symbolic links instead of copying
-S, --suffix=SUFFIX
override the usual backup suffix
-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY
-T, --no-target-directory
treat DEST as a normal file
-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
-x, --one-file-system
stay on this file system
-Z set SELinux security context of destination file to default type
--context[=CTX]
like -Z, or if CTX is specified then set the SELinux or SMACK
security context to CTX
--help display this help and exit
--version
output version information and exit
By default, sparse SOURCE files are detected by a crude heuristic and
the corresponding DEST file is made sparse as well. That is the behav‐
ior selected by --sparse=auto. Specify --sparse=always to create a
sparse DEST file whenever the SOURCE file contains a long enough
sequence of zero bytes. Use --sparse=never to inhibit creation of
sparse files.
When --reflink[=always] is specified, perform a lightweight copy, where
the data blocks are copied only when modified. If this is not possible
the copy fails, or if --reflink=auto is specified, fall back to a stan‐
dard copy.
The backup suffix is '~', unless set with --suffix or SIM‐
PLE_BACKUP_SUFFIX. The version control method may be selected via the
--backup option or through the VERSION_CONTROL environment variable.
Here are the values:
none, off
never make backups (even if --backup is given)
numbered, t
make numbered backups
existing, nil
numbered if numbered backups exist, simple otherwise
simple, never
always make simple backups
As a special case, cp makes a backup of SOURCE when the force and
backup options are given and SOURCE and DEST are the same name for an
existing, regular file.
AUTHOR
Written by Torbjorn Granlund, David MacKenzie, and Jim Meyering.
REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report cp translation bugs to <http://translationproject.org/team/>
COPYRIGHT
Copyright © 2016 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
Full documentation at: <http://www.gnu.org/software/coreutils/cp>
or available locally via: info '(coreutils) cp invocation'
GNU coreutils 8.25 February 2017 CP(1)