软件测试|Linux基础教程:cp命令详解,复制文件或目录

简介: 软件测试|Linux基础教程:cp命令详解,复制文件或目录

image.png

简介

在Linux系统中,cp命令是一个非常常用且强大的命令,用于复制文件和目录。cp命令允许我们在不同目录之间复制文件或目录,并可以根据需求对文件复制的行为进行调整。在本文中,我们将详细解释cp命令的用法以及一些常见的选项。

cp命令

基本语法

cp命令的基本语法如下:

cp [选项] 源文件 目标文件

选项常用参数:

  • d:如果源文件为软链接(对硬链接无效),则复制出的目标文件也为软链接;
  • i:询问,如果目标文件已经存在,则会询问是否覆盖;
  • l:把目标文件建立为源文件的硬链接文件,而不是复制源文件;
  • s:把目标文件建立为源文件的软链接文件,而不是复制源文件;
  • p:复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间);
  • r:递归复制,用于复制目录;
  • u:若目标文件比源文件有差异,则使用该选项可以更新目标文件,此选项可用于对文件的升级和备用
  • a:相当于 -d、-p、-r 选项的集合

注:源文件可以有多个,但这种情况下,目标文件必须是目录才可以。

复制文件

  1. 复制文件,不改变目标名
muller@ubuntu:~$ cd files/
# 创建fruits.txt文件
muller@ubuntu:~/files$ touch fruits.txt
# 将文件复制到/home/muller/下
muller@ubuntu:~/files$ cp fruits.txt /home/muller/
# 回到上一级目录,查看是否成功复制
muller@ubuntu:~/files$ cd ..
muller@ubuntu:~$ ls
Desktop    Downloads  fruits.txt  Pictures  snap       Videos
Documents  files      Music       Public    Templates
  1. 复制文件,改变目标名
muller@ubuntu:~$ cd files/
# 复制文件并改名为food.txt
muller@ubuntu:~/files$ cp fruits.txt /home/muller/food.txt
# 查看是否复制成功
muller@ubuntu:~/files$ cd ..
muller@ubuntu:~$ ls
Desktop    Downloads  food.txt    Music     Public  Templates
Documents  files      fruits.txt  Pictures  snap    Videos
  1. 复制多个文件
muller@ubuntu:~/files$ ls
cars.txt  fruits.txt  players.txt
# 将files目录下的3个文件复制到/home/muller/demo目录下
muller@ubuntu:~/files$ cp fruits.txt cars.txt players.txt /home/muller/demo/
# 查看是否复制成功
muller@ubuntu:~/files$ cd ../demo
muller@ubuntu:~/demo$ ls
cars.txt  fruits.txt  players.txt

复制目录

  1. 复制目录及其内容

要复制一个目录及其所有内容(包括子目录和文件),需要使用-r-R参数。这些选项用于递归地复制整个目录结构。命令如下:

cp -r /path/to/source_directory /path/to/destination/

示例如下:

# 将files目录复制到demo目录下
muller@ubuntu:~$ cp -r /home/muller/files/ /home/muller/demo/
# 查看结果
muller@ubuntu:~$ cd demo
muller@ubuntu:~/demo$ ls
cars.txt  files  fruits.txt  players.txt
# 查看是否保留原有目录层级结构
muller@ubuntu:~/demo$ cd files/
muller@ubuntu:~/demo/files$ ls
cars.txt  fruits.txt  players.txt
  1. 复制目录并保留属性

如果想要复制目录并保留其所有属性(例如权限、时间戳等),可以使用-a选项,它等同于-dR --preserve=all

cp -a /path/to/source_directory /path/to/destination/

示例如下:

muller@ubuntu:~$ cp -a /home/muller/files/ /home/muller/demo/
muller@ubuntu:~$ ls
demo     Documents  files     fruits.txt  Pictures  snap       Videos
Desktop  Downloads  food.txt  Music       Public    Templates
muller@ubuntu:~$ ll files/
total 12
drwxrwxr-x  2 muller muller 4096 Jul 25 17:09 ./
drwxr-xr-x 19 muller muller 4096 Jul 25 17:09 ../
-rw-rw-r--  1 muller muller    0 Jul 25 17:09 cars.txt
-rw-rw-r--  1 muller muller   59 Jul 25 17:00 fruits.txt
-rw-rw-r--  1 muller muller    0 Jul 25 17:09 players.txt

注意事项

  • 复制到一个目录时,目标路径必须是一个目录,否则会将源文件重命名为指定的目标路径。
  • 在递归复制目录时,务必小心以免覆盖现有文件。
  • 在使用-i选项时,cp命令会提示你是否覆盖已存在的目标文件。

总结

cp命令是Linux中一个强大且常用的命令,用于复制文件和目录。通过本文,你学会了如何使用cp命令来复制文件和目录,并了解了一些常见的选项。请在使用cp命令时小心谨慎,避免不必要的数据丢失或文件覆盖。

相关文章
|
11月前
|
Linux Shell
linux自动崩溃,模拟测试
该脚本创建一个 systemd 服务和定时器,在系统启动3分钟后触发崩溃。通过向 /proc/sysrq-trigger 写入 "c" 来实现内核崩溃,用于测试系统崩溃后的恢复机制。
266 4
|
8月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
442 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
8月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
933 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
9月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
511 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
9月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
581 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
10月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
673 18
|
9月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
687 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
828 0
|
9月前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
875 0
|
9月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
321 0