Linux笔记(6)Linux中文件(夹)的复制(cp)和文件(夹)的移动(mv)

本文详细介绍了Linux系统中使用`cp`和`mv`命令进行文件和目录复制、移动及重命名的操作步骤和注意事项,包括单个文件、多个文件、文件夹的复制与移动,以及如何处理已存在的目标文件。同时,解释了`cp`命令的别名设置及其影响,以及如何使用命令的绝对路径来避免别名问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cp 复制copy
用途:复制文件或目录

1.单个文件的复制

[root@localhost lianxi]# touch   song.txt
[root@localhost lianxi]# ls
feng.txt  song.txt
[root@localhost lianxi]# mkdir  changsha
[root@localhost lianxi]# ls
changsha  feng.txt  song.txt
[root@localhost lianxi]# cp  song.txt  changsha
                              源文件           目的地
                              source           destination
[root@localhost lianxi]# ls changsha
song.txt                             

2.多个文件的复制

[root@localhost lianxi]#
[root@localhost lianxi]# mkdir  hunan
[root@localhost lianxi]# ls
changsha  feng.txt  hunan  song.txt  wei.txt
[root@localhost lianxi]# cp  feng.txt  song.txt  wei.txt   hunan
                               前面的3个文件都是源文件       目的地
[root@localhost lianxi]# ls
changsha  feng.txt  hunan  song.txt  wei.txt                               

3.文件的复制,粘贴,重命名一步到位

[root@localhost lianxi]# ls hunan
feng.txt  song.txt  wei.txt
[root@localhost lianxi]# 
[root@localhost lianxi]# cp  *.txt  hunan  将所有的.txt结尾的文件复制到hunan

4.为什么复制文件的时候,如果文件在目的地已经存在,会给予提醒

[root@localhost lianxi]# alias 
alias cp='cp -i'
       -i, --interactive 交互式
              prompt before overwrite在覆盖之前给予提醒 
解决方法:
	1.取消别名
	unalias  cp
	2.使用cp命令的绝对路径 --》绕过别名的使用  ---》推荐的方法
	[root@localhost lianxi]# which cp
	alias cp='cp -i'
	/usr/bin/cp
	[root@localhost lianxi]# /usr/bin/cp  *.txt hunan
	[root@localhost lianxi]# \cp *.txt hunan

将song.txt复制到hunan目录,改名为syx.txt

[root@localhost lianxi]# cp songyuxiao.txt  hunan/syx.txt
[root@localhost lianxi]# cp feng.txt  hunan/fengd.txt

5.cp命令的绝对路径的使用

[root@localhost lianxi]# cp  /etc/passwd   .(当前目录)   将/etc/passwd 文件复制到当前目录下
[root@localhost lianxi]# cp  /etc/passwd  /lianxi/hunan
[root@localhost lianxi]# ls /lianxi/hunan
fengd.txt  feng.txt  passwd  song.txt  syx.txt  wei.txt
[root@localhost lianxi]# 

6.单个文件夹的复制
-r 复制文件夹的时候需要使用
(-r在命令之前或者之后都没有关系,mv命令中不需要加-r)

[root@localhost lianxi]# ls
changsha  feng.txt  hubei  hunan  song.txt  wei.txt
[root@localhost lianxi]# cp  -r hunan  hubei
[root@localhost lianxi]# cp changsha  hubei  -r

[root@localhost lianxi]# cp -r hunan hubei  guangdong
                               源文件夹     目的地
[root@localhost lianxi]# ls guangdong/
hubei  hunan

7.文件夹的复制,粘贴,重命名一步到位

[root@localhost lianxi]# 
[root@localhost lianxi]# cp  /boot    .  -r
[root@localhost lianxi]# cp  guangdong(源文件)  hunan(目的地)/yue(重命名后的文件名)  -r
 

mv - move (rename) files
1.移动文件或者文件夹 --》后面接的文件夹如果存在就是移动

[root@localhost lianxi]# mv hunan guangxi

2.重命名 --》后面接的文件如果不存在就是重命名

移动多个文件

[root@localhost lianxi]# mv gui changsha hubei guangdong/  china
                                源文件                      目的地
mv boot/  weijunlin.txt  china

cp  boot  wenjunlin.txt  china  -r
[root@localhost lianxi]# mv *.txt  china  将.txt结尾的文件复制到china目录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值