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]# 
相关推荐
Everbrilliant894 分钟前
Ubuntu系统下交叉编译Android的X265库
linux·运维·ubuntu·x265交叉编译·android x265·ffmpeg x265
我不要放纵12 分钟前
LVS集群搭建
linux·服务器·lvs
阿巴~阿巴~17 分钟前
自主Shell命令行解释器
linux·运维·服务器
许白掰18 分钟前
Linux入门篇学习——借助 U 盘或 TF 卡拷贝程序到开发板上
linux·学习·借助 u 盘拷贝程序到开发板上·借助 tf卡拷贝程序到开发板上
小周学学学19 分钟前
docker安装与简单项目上手
运维·docker·容器
SHUIPING_YANG44 分钟前
根据用户id自动切换表查询
java·服务器·数据库
chao_7891 小时前
更灵活方便的初始化、清除方法——fixture【pytest】
服务器·自动化测试·python·pytest
枷锁—sha1 小时前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
枷锁—sha1 小时前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
scuter_yu1 小时前
腾讯云云服务器深度介绍
服务器·云计算·腾讯云