每日一个linux命令07-cp

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 说明本次redis集群安装在rhel6.8 64位机器上,redis版本为3.2.8,redis的gem文件版本为...
    读或写阅读 15,469评论 3 9
  • 1、Linux上的文件管理命令有哪些以及常用方法shell:shell负责接收用户输入的命令并进行解释,将需要执行...
    乀koala阅读 4,256评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,268评论 19 139
  • 96女 喜文艺,喜欧美,喜拍照,愿识你❤
    Y_鹿九少女阅读 2,615评论 0 0
  • #玩卡不卡·每日一抽# 每一位都可以通过这张卡片觉察自己: 1、直觉他叫什么名字? 花 2、他几岁了? 53岁 3...
    文俊zh阅读 1,157评论 0 0

友情链接更多精彩内容