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]# 
相关推荐
开开心心就好11 分钟前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
m0_653031361 小时前
腾讯云认证考试报名 - TDSQL数据库交付运维专家(TCCE PostgreSQL版)
运维·数据库·腾讯云
IC 见路不走1 小时前
LeetCode 第91题:解码方法
linux·运维·服务器
没有名字的小羊2 小时前
8.Docker镜像讲解
运维·docker·容器·tomcat
翻滚吧键盘2 小时前
查看linux中steam游戏的兼容性
linux·运维·游戏
小能喵2 小时前
Kali Linux Wifi 伪造热点
linux·安全·kali·kali linux
Code季风2 小时前
深入理解微服务中的服务注册与发现(Consul)
java·运维·微服务·zookeeper·架构·go·consul
java1234_小锋2 小时前
解释一下NGINX的反向代理和正向代理的区别?
运维·nginx
汀沿河2 小时前
8.1 prefix Tunning与Prompt Tunning模型微调方法
linux·运维·服务器·人工智能
zly35002 小时前
centos7 ping127.0.0.1不通
linux·运维·服务器