大熊知识站

  • 🧑🏻‍💻关于我
  • 🧰工具
    • 示例页面
justx
书写是为了更好的思考,文字记录了我的成长
  1. 首页
  2. 每日Linux
  3. 正文

每日 Linux 命令学习系列:Day 6 - cp

2024 年 4 月 9 日 801点热度 0人点赞 0条评论

介绍

rm(copy)用来复制文件(或目录)到指定的路径,可同时复制多个文件和目录。

命令格式:

cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file [target_file | target_directory]

第一种.将源文件的内容复制到目标文件中。
第二种.将每个指定的源文件的内容复制到目标目录中。文件名本身不会更改。
如果 cp 检测到尝试将文件复制到自身的情况,复制操作将失败。

常用选项:

-a, --archive
  保留链接、文件属性,并复制目录下的所有内容。其作用等于 dpR 参数组合

-b
  覆盖已存在的文件前将文件备份

-d
  当复制符号连接时,把符号连接指向源文件或源目录

-f, --force
  强制复制,覆盖已经存在的文件时不提示

-i, --interactive
  在覆盖已经存在的文件时给出提示,要求用户确认是否覆盖

-l, --link
  对源文件建立硬连接,而非复制文件

-n, --no-clobber
  不覆盖已经存在的文件

-p
  除复制文件的内容外,同时复制修改时间和访问权限等

-R, -r, --recursive
  递归处理,将指定目录下的文件与子目录一并复制

-s, --symbolic-link
  对源文件建立符号连接,而非复制文件

使用示例:

➜ linuxtest ls
d1 d2 test1.txt test2.txt
# 复制并重命名
➜ linuxtest cp test2.txt d2/test2new.txt
➜ linuxtest ls -l d2/
total 0
-rw-r--r-- 1 justx staff 0 4 9 17:27 test2new.txt

# 复制多个到指定目录
➜ linuxtest cp test1.txt test2.txt d2 
➜ linuxtest ls -l d2/ 
total 0
-rw-r--r-- 1 justx staff 0 4 9 17:28 test1.txt
-rw-r--r-- 1 justx staff 0 4 9 17:28 test2.txt
-rw-r--r-- 1 justx staff 0 4 9 17:27 test2new.txt

# 递归复制文件夹到指定目录
➜ linuxtest cp -r d2 d1
➜ linuxtest ls -l d1
total 0
drwxr-xr-x 5 justx staff 160 4 9 17:28 d2
-rw-r--r-- 1 justx staff 0 4 9 17:25 test.txt
-rw-r--r-- 1 justx staff 0 4 9 17:26 test2.txt

# 显示提示信息
➜ linuxtest cp -i d2 d1 
cp: d2 is a directory (not copied).
➜ linuxtest cp -ir d2 d1
overwrite d1/d2/test1.txt? (y/n [n]) y
overwrite d1/d2/test2.txt? (y/n [n]) y
overwrite d1/d2/test2new.txt? (y/n [n]) y

 

标签: cp linux
最后更新:2024 年 4 月 30 日

justx

知道的越多 不知道的越多

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

justx

知道的越多 不知道的越多

最新 热点 随机
最新 热点 随机
每日 Linux 命令学习系列:Day 12 - grep 每日 Linux 命令学习系列:Day 11 - tail 每日 Linux 命令学习系列:Day 10 - head MySQL分组查询-GROUP BY MySQL-计算列与计算字段 每日 Linux 命令学习系列:Day 9 - less
每日 Linux 命令学习系列:Day 7 - mv vim 常用命令(补全中) Nginx 反向代理与负载均衡 JavaScript设计模式-单例模式 Nginx基础概念 MySQL-计算列与计算字段

COPYRIGHT © 2024 justx. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备2024246463号

粤公网安备44011302004429号