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]# 
相关推荐
likfishdn13 分钟前
Linux的文件与目录管理
linux·运维·服务器
渲染101专业云渲染14 分钟前
川翔云电脑优势总结
服务器·3d·电脑·blender·maya
chian-ocean32 分钟前
Linux 文件缓冲区:高效数据访问的幕后推手
linux·运维·服务器
加油=^_^=34 分钟前
【Linux】进程优先级 | 进程调度(三)
linux·运维·服务器
橘子味的茶二43 分钟前
高级系统架构师--第十章:计算机网络
服务器·计算机网络·系统架构
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
紫菜(Nori)1 小时前
短连接服务器压测-wrk
服务器·压力测试
追寻光1 小时前
Cannot find a valid baseurl for repo: base/7/x86_64
linux
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos