1. 命令解析
命令用途:
复制文件或目录至目标位置,或复制一组文件或目录至目标位置
命令格式:
复制文件或目录至指定位置 cp [OPTION]... [-T] SOURCE DEST
复制一组文件或目录至指定位置: cp [OPTION]... SOURCE... DIRECTORY
复制一组文件或目录至指定位置: cp [OPTION]... -t DIRECTORY SOURCE...
命令参数:
-a, --archive same as -dR --preserve=all
--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 (redundant if
the -n option is used)
-i, --interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, --link 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,timestamps), if possible
additional attributes: context, links, xattr,
all
-c same as --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 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, --context=CONTEXT set security context of copy to CONTEXT
--help display this help and exit
--version output version information and exit
2. 示例
2.1 不带参数的复制文件
[root@fanshifeng cpTest]# ls
f1 f2 f2.~1~ f2.~2~ f3 f4
[root@fanshifeng cpTest]# cp f1 f5
[root@fanshifeng cpTest]# ls
f1 f2 f2.~1~ f2.~2~ f3 f4 f5
[root@fanshifeng cpTest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 11 19:39 f1
-rw-r--r-- 1 root root 0 Apr 11 19:46 f2
-rw-r--r-- 1 root root 0 Apr 11 19:39 f2.~1~
-rw-r--r-- 1 root root 0 Apr 11 19:46 f2.~2~
-rw-r--r-- 1 root root 0 Apr 11 19:40 f3
-rw-r--r-- 1 root root 0 Apr 11 19:39 f4
-rw-r--r-- 1 root root 0 Apr 11 19:53 f5
可以看到,复制的文件的属性(创建时间)发生了变化
2.2 同时复制多个文件
[root@fanshifeng cpTest]# mkdir d1
[root@fanshifeng cpTest]# ls
d1 f1 f2 f2.~1~ f2.~2~ f3 f4 f5
[root@fanshifeng cpTest]# cp f1 f2 f3 f4 f5 d1
[root@fanshifeng cpTest]# tree d1
d1
├── f1
├── f2
├── f3
├── f4
└── f5
0 directories, 5 files
2.3 复制一个目录
[root@fanshifeng cpTest]# ls
d1 f1 f2 f2.~1~ f2.~2~ f3 f4 f5
[root@fanshifeng cpTest]# cp -r d1 d2
[root@fanshifeng cpTest]# ls
d1 d2 f1 f2 f2.~1~ f2.~2~ f3 f4 f5
[root@fanshifeng cpTest]# tree d2
d2
├── f1
├── f2
├── f3
├── f4
└── f5
0 directories, 5 files
2.4 复制目录或文件,且保留其归档信息(mode,ownership,timestamps) -a
[root@fanshifeng cpTest]# ls
d1 d2 d3 f1 f2 f2.~1~ f2.~2~ f3 f4 f5
[root@fanshifeng cpTest]# ll
total 12
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
-rw-r--r-- 1 root root 0 Apr 11 19:39 f1
[root@fanshifeng cpTest]# cp -a f1 f6
[root@fanshifeng cpTest]# cp -a d1 d6
[root@fanshifeng cpTest]# ll
total 16
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
drwxr-xr-x 2 root root 4096 Apr 11 19:55 d6
-rw-r--r-- 1 root root 0 Apr 11 19:39 f1
-rw-r--r-- 1 root root 0 Apr 11 19:39 f6
2.5 创建文件的硬链接(快捷方式)而不是复制它们 -l
[root@fanshifeng cpTest]# cp -l f1 d7 -v
`f1' -> `d7/f1'
[root@fanshifeng cpTest]# ls -lvi
total 16
1321179 drwxr-xr-x 2 root root 4096 Apr 11 19:55 d1
1321176 drwxr-xr-x 2 root root 4096 Apr 11 19:56 d2
1321191 drwxr-xr-x 2 root root 4096 Apr 11 20:07 d7
1321172 -rw-r--r-- 2 root root 6 Apr 11 20:05 f1
1321197 -rw-r--r-- 1 root root 0 Apr 11 19:39 f6
[root@fanshifeng cpTest]# ls -lvi d7
total 4
1321172 -rw-r--r-- 2 root root 6 Apr 11 20:05 f1
可以看到:原文件的inode为1321172,目标目录的文件的inode也是1321172
2.6 创建文件的软链接 -s
[root@fanshifeng cpTest]# cp f2 -s d7 -v
`f2' -> `d7/f2'
cp: `d7/f2': can make relative symbolic links only in current directory
[root@fanshifeng cpTest]# cd d7
[root@fanshifeng d7]# cp -s -v ../f1 f1.soft
`../f1' -> `f1.soft'
软链接只能在当前目录下创建
2.7 只复制软链接(不复制文件本身) -P
[root@fanshifeng d7]# cp -v -P f1.soft f3.soft
`f1.soft' -> `f3.soft'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:05 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
2.8 仅当原文件比较新时拷贝 -u
[root@fanshifeng d7]# cp -u -v f1 f2
[root@fanshifeng d7]# ls
f1 f1.soft f2 f3.soft
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:05 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# touch f1
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:18 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -u -v f1 f2;
cp: overwrite `f2'? y
`f1' -> `f2'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:49 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
2.9 赋值前删除目标文件(若存在) --remove-destination
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 20:49 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -v -remove-destination f1 f2;
cp: invalid option -- 'e'
Try `cp --help' for more information.
[root@fanshifeng d7]# cp -v --remove-destination f1 f2;
cp: overwrite `f2'? y
removed `f2'
`f1' -> `f2'
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
2.10 复制文件仅当目标文件不存在时 -n
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1
[root@fanshifeng d7]# cp -v -n f1 f2
[root@fanshifeng d7]# ll
total 8
-rw-r--r-- 2 root root 6 Apr 11 20:49 f1
lrwxrwxrwx 1 root root 5 Apr 11 20:12 f1.soft -> ../f1
-rw-r--r-- 1 root root 6 Apr 11 21:01 f2
lrwxrwxrwx 1 root root 5 Apr 11 20:19 f3.soft -> ../f1