Linux基础指令文件管理002

本章讲述Linux文件管理中如何复制,移动文件。

操作系统:

CentOS Stream 9

复制文件

指令cp

将文件复制到同一目录下

bash 复制代码
[root@localhost ~]# mkdir -pv /a/b/c
mkdir: 已创建目录 '/a'
mkdir: 已创建目录 '/a/b'
mkdir: 已创建目录 '/a/b/c'
[root@localhost ~]# cd /a/b/c
[root@localhost c]# pwd
/a/b/c
[root@localhost c]# touch 1.txt
[root@localhost c]# cp 1.txt 2.txt
[root@localhost c]# ll
总用量 0
-rw-r--r-- 1 root root 0  6月  8 14:13 1.txt
-rw-r--r-- 1 root root 0  6月  8 14:14 2.txt
[root@localhost c]# 

复制文件到指定目录

bash 复制代码
[root@localhost c]# cp 1.txt /a
[root@localhost c]# cd ../../
[root@localhost a]# ll
总用量 0
-rw-r--r-- 1 root root  0  6月  8 14:16 1.txt
drwxr-xr-x 3 root root 15  6月  8 14:12 b
[root@localhost a]# pwd
/a
[root@localhost a]# 

其常用选项

-a 保留原有文件的全部属性,包括权限、时间戳、软链接等,并且递归复制目录。

-v显示详细的复制过程,复制过程中会输出每个被操作的文件名

bash 复制代码
[root@localhost c]# cp -av 1.txt /a/b
'1.txt' -> '/a/b/1.txt'
[root@localhost c]# 

移动文件

指令mv

重命名文件

bash 复制代码
[root@localhost c]# ll
总用量 0
-rw-r--r-- 1 root root 0  6月  8 14:13 1.txt
-rw-r--r-- 1 root root 0  6月  8 14:14 3.txt
[root@localhost c]# 

移动文件到另一个目录

bash 复制代码
[root@localhost c]# mv 3.txt /a/b/
[root@localhost c]# cd ..
[root@localhost b]# ll
总用量 0
-rw-r--r-- 1 root root  0  6月  8 14:13 1.txt
-rw-r--r-- 1 root root  0  6月  8 14:14 3.txt
drwxr-xr-x 2 root root 19  6月  8 14:31 c
[root@localhost b]# 

文件重命名

bash 复制代码
[root@localhost b]# mv 1.txt 4.txt
[root@localhost b]# ll
总用量 0
-rw-r--r-- 1 root root  0  6月  8 14:14 3.txt
-rw-r--r-- 1 root root  0  6月  8 14:13 4.txt
drwxr-xr-x 2 root root 19  6月  8 14:31 c
[root@localhost b]# 

其常用选项

-i如果目标文件已存在,询问是否覆盖

bash 复制代码
[root@localhost c]# mv -i 1.txt /a/
mv:是否覆盖'/a/1.txt'? y
[root@localhost c]# cd ../../
[root@localhost a]# ll
总用量 0
-rw-r--r-- 1 root root  0  6月  8 14:13 1.txt
drwxr-xr-x 3 root root 41  6月  8 14:33 b
[root@localhost a]# 

-f强制执行,不询问

bash 复制代码
[root@localhost a]# mv -f 1.txt /a/b/c
[root@localhost a]# cd /a/b/c
[root@localhost c]# ll
总用量 0
-rw-r--r-- 1 root root 0  6月  8 14:13 1.txt

-u若目标文件存在,则比源文件新,才会移动

bash 复制代码
[root@localhost c]# echo "123"> 1.txt
[root@localhost c]# cat 1.txt
123
[root@localhost c]# mv -u 1.txt /a
[root@localhost c]# cd ../../
[root@localhost a]# ll
总用量 4
-rw-r--r-- 1 root root  4  6月  8 14:47 1.txt
drwxr-xr-x 3 root root 41  6月  8 14:33 b
[root@localhost a]# cat 1.txt
123
[root@localhost a]# 
相关推荐
小Mie不吃饭12 分钟前
FastAPI 小白教程:从入门级到实战(源码教程)
运维·服务器
fo安方1 小时前
运维的利器–监控–zabbix–第三步:配置zabbix–中间件–Tomcat–步骤+验证
运维·中间件·zabbix
爱奥尼欧1 小时前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
戒不掉的伤怀2 小时前
【Navicat 连接MySQL时出现错误1251:客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端】
服务器·数据库·mysql
超喜欢下雨天2 小时前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
搬码临时工2 小时前
小企业如何搭建本地私有云服务器,并设置内部网络地址提供互联网访问
运维·服务器
old-six-programmer2 小时前
NAT 类型及 P2P 穿透
服务器·网络协议·webrtc·p2p·nat
tan77º2 小时前
【Linux网络编程】网络基础
linux·服务器·网络
风口上的吱吱鼠2 小时前
Armbian 25.5.1 Noble Gnome 开启远程桌面功能
服务器·ubuntu·armbian
18你磊哥3 小时前
Windows 本地安装部署 Apache Druid
运维·debian