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]# 
相关推荐
AEMC马广川13 分钟前
关于综合能源服务认证证书的全解析专业认证团队
java·大数据·服务器·能源
Cisco_hw_zte1 小时前
思科路由器做DNS服务器
服务器·网络·智能路由器
伊H2 小时前
C语言main的参数;argc与argv
linux·c语言·算法
husertuo2 小时前
Linux下的网络管理配置
linux·云计算
文牧之3 小时前
PostgreSQL 用户资源管理
运维·数据库·postgresql
liqingdi4374 小时前
WSL+Ubuntu+miniconda环境配置
linux·windows·ubuntu
luoqice4 小时前
通过 Samba 服务实现 Ubuntu 和 Windows 之间互传文件
linux
极客先躯7 小时前
高级java每日一道面试题-2025年4月13日-微服务篇[Nacos篇]-Nacos如何处理网络分区情况下的服务可用性问题?
java·服务器·网络·微服务·nacos·高级面试
知远同学7 小时前
docker学习笔记2-最佳实践
运维·docker·容器
哈哈幸运8 小时前
MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
linux·运维·数据库·mysql·性能优化