linux中cp 命令使用介绍(复制文件或者目录)
。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。
1.命令格式:
用法:
cp [选项]... [-T] 源 目的
或:cp [选项]... 源... 目录
或:cp [选项]... -t 目录 源...
2.命令功能:
将源文件复制至目标文件,或将多个源文件复制至目标目录。
3.命令参数:
-a, --archive 等于-dR --preserve=all
--backup[=CONTROL 为每个已存在的目标文件创建备份
-b 类似--backup 但不接受参数
--copy-contents 在递归处理是复制特殊文件内容
-d 等于--no-dereference --preserve=links
-f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项
存在时则不需再选此项)
-i, --interactive 覆盖前询问(使前面的 -n 选项失效)
-H 跟随源文件中的命令行符号链接
-l, --link 链接文件而不复制
-L, --dereference 总是跟随符号链接
-n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)
-P, --no-dereference 不跟随源文件中的符号链接
-p 等于--preserve=模式,所有权,时间戳
--preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果
可能保持附加属性:环境、链接、xattr 等
-R, -r, --recursive 复制目录及目录内的所有项目
4.命令实例:
实例一:复制单个文件到目标目录,文件在目标文件中不存在
命令:
cp log.log test5
输出:
[root@localhost test]# cp log.log test5
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 2 root root 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:53 log.log
说明:
在没有带-a参数时,两个文件的时间是不一样的。在带了-a参数时,两个文件的时间是一致的。
实例二:目标文件存在时,会询问是否覆盖
命令:
cp log.log test5
输出:
[root@localhost test]# cp log.log test5
cp:是否覆盖“test5/log.log”? n
[root@localhost test]# cp -a log.log test5
cp:是否覆盖“test5/log.log”? y
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
说明:
目标文件存在时,会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时,即使加了-f标志,也还会询问是否覆盖。
实例三:复制整个目录
命令:
输出:
目标目录存在时:
[root@localhost test]# cp -a test3 test5
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
目标目录不存在是:
[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#
说明:
注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。
实例四:复制的 log.log 建立一个连结档 log_link.log
命令:
cp -s log.log log_link.log
输出:
[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
说明:
那个 log_link.log 是由 -s 的参数造成的,建立的是一个『快捷方式』,所以您会看到在文件的最右边,会显示这个文件是『连结』到哪里去的!
相关文章

Fedora Linux 42 稳定版发布: 带来大量新功能和软件更新
Fedora 42昨日发布,这是 Red Hat 赞助开发的杰出前沿 Linux 发行版的最新版,包含大量新功能和软件更新,使其成为 2025 年上半年发布的一款出色的 Linux 操作系统之一,内2025-04-16
如何在Linux查看硬盘信息? 查看Linux硬盘大小类型和硬件信息的5种方法
使用Linux系统的过程中,查看和了解硬盘信息是非常重要的工作,尤其是对于系统管理员而言,那么在Linux系统中如何查看硬盘信息?以下是具体内容介绍2025-03-12
如何在 Linux 中查看 CPU 详细信息? 3招轻松查看CPU型号、核心数和温度
在日常运维工作中,获取 CPU 信息是系统运维管理员常见的工作内容,无论是为了性能调优、硬件升级还是仅仅满足好奇心2025-03-11
什么是 Arch Linux? 独树一帜的Arch Linux发行版分析
Arch Linux是为简化,优化,现代化,实用主义,用户中心和多功能性而创建Linux发行版,究竟是什么让 Arch 与众不同?下面我们就来简要解读2025-02-19
一直用的linux办公,想要将笔记本电脑从 Linux 系统切换回 Windows 11,我们可以制作一个win11装机u盘,详细如下2025-02-17
Rsnapshot怎么用? 基于Rsync的强大Linux备份工具使用指南
Rsnapshot 不仅可以备份本地文件,还能通过 SSH 备份远程文件,接下来详细介绍如何安装、配置和使用 Rsnapshot,包括创建每小时、每天、每周和每月的本地备份,以及如何进2025-02-06
Linux Kernel 6.13发布:附更新内容及新特性解读
Linux 内核 6.13 正式发布,新版本引入了惰性抢占支持,简化内核抢占逻辑,通过减少与调度器相关的调用次数,让内核在运行时表现更优,从而提高效率2025-01-23
五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布
今日,深度操作系统正式推出deepin 25 Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Treeland窗口合成器、AI For OS以及Distrobox子系统2025-01-18
Linux Mint Xia 22.1重磅发布: 重要更新一览
Beta 版 Linux Mint“Xia” 22.1 发布,新版本基于 Ubuntu 24.04,内核版本为 Linux 6.8,这次更新带来了诸多优化和改进,进一步巩固了 Mint 在 Linux 桌面操作系统领域的2025-01-16
LinuxMint怎么安装? Linux Mint22下载安装图文教程
Linux Mint22发布以后,有很多新功能,很多朋友想要下载并安装,该怎么操作呢?下面我们就来看看详细安装指南2025-01-16









最新评论