Linux命令(10):mv

mv意为:move
可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。
命令格式:
mv [选项] 源文件或目录 目标文件或目录
命令功能:
视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中。当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。在跨文件系统移动文件时,mv先拷贝,再将原有文件删除,而链至该文件的链接也将丢失。
命令参数:

  • -b :若需覆盖文件,则覆盖前先行备份。

  • -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;

  • -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!

  • -u :若目标文件已经存在,且 source 比较新,才会更新(update)

  • -t : –target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

    例1.文件改名
    命令:mv m1 m2.txt (将文件m1重命名为m1.txt)

[mt555@localhost Desktop]$ ll
总用量 0
drwxrwxr-x. 7 mt555 mt555 51 97 04:21 m1
-rw-rw-r--. 1 mt555 mt555  0 96 21:51 mt1.txt~
[mt555@localhost Desktop]$ mv m1 m2.txt
[mt555@localhost Desktop]$ ll        # 此时可看到m1已结被重命名为m2.txt
总用量 0
drwxrwxr-x. 7 mt555 mt555 51 97 04:21 m2.txt
-rw-rw-r--. 1 mt555 mt555  0 96 21:51 mt1.txt~
[mt555@localhost Desktop]$ 

例2.移动文件
命令:mv m2.txt m3 (将m1.txt文件移到目录m3中)

[mt555@localhost Desktop]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 00:51 m1.txt
drwxrwxr-x. 2 mt555 mt555 6 98 00:52 m2
-rw-rw-r--. 1 mt555 mt555 0 96 21:51 mt1.txt~
[mt555@localhost Desktop]$ mv m1.txt m2
[mt555@localhost Desktop]$ cd m2 # 进入m2目录发现m1.txt被移动至此目录
[mt555@localhost m2]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 00:51 m1.txt
[mt555@localhost m2]$ 

例3.将文件m2.txt,m3.txt,m4.txt移动到目录t3中。
命令:
mv m2.txt,m3.txt,m4.txt t3 (此命令是将三个文件移到 t3目录中去)

mv -t /home/mt555/Desktop/t4/ m2.txt,m3.txt,m4.txt (此命令又将三个文件移动到t4目录中去)

[mt555@localhost Desktop]$ ll # 首先可看到桌面有这些文件
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m2.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m3.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m4.txt
-rw-rw-r--. 1 mt555 mt555 0 96 21:51 mt1.txt~
drwxrwxr-x. 2 mt555 mt555 6 98 03:17 t3
drwxrwxr-x. 2 mt555 mt555 6 98 01:03 t4
[mt555@localhost Desktop]$ mv m2.txt m3.txt m4.txt t3 # 移动文件命令
[mt555@localhost Desktop]$ cd t3 # 进入t3目录查看是否移动成功
[mt555@localhost t3]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m2.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m3.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m4.txt
[mt555@localhost t3]$ mv -t /home/mt555/Desktop/t4/ m2.txt m3.txt m4.txt # 再将此三个文件移动到t4目录中
[mt555@localhost t3]$ cd ..
[mt555@localhost Desktop]$ cd t4 # 进入t4目录查看文件显示
[mt555@localhost t4]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m2.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m3.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m4.txt
[mt555@localhost t4]$ 

例4.将文件file1改名为file2,如果file2已经存在,则询问是否覆盖
命令:mv -i f1.txt f2.txt

[mt555@localhost Desktop]$ cat f1.txt~  # 用cat分别查看文件内容
123
123
0
[mt555@localhost Desktop]$ cat f2.txt~ 
hello
my name is mt
[mt555@localhost Desktop]$ mv -i f1.txt~ f2.txt~ 
mv:是否覆盖"f2.txt~"? y       # y表示同意覆盖
[mt555@localhost Desktop]$ cat f2.txt~  # 再查看f2.txt文件内容,已经被f1.txt覆盖
123
123
0
[mt555@localhost Desktop]$ 

例5.将文件file2改名为file3,即使file3存在,也是直接覆盖掉。
命令:mv -f f2.txt f3.txt (-f 这个选项比较危险,慎重使用)

[mt555@localhost Desktop]$ cat f2.txt 
123
123
0
[mt555@localhost Desktop]$ cat f3.txt
gggggggggg
[mt555@localhost Desktop]$ mv -f f2.txt f3.txt
[mt555@localhost Desktop]$ cat f3.txt #f2.txt的内容直接覆盖了f3.txt的内容
123
123
0
[mt555@localhost Desktop]$ 

例6.目录的移动
命令:mv dir1 dir2 (如果目录dir2不存在,将目录dir1改名为dir2;否则,将dir1移动到dir2中。)

[mt555@localhost Desktop]$ cd t3 # 分别进入t3目录和t4目录,查看目录内容
[mt555@localhost t3]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 03:55 L1
drwxrwxr-x. 2 mt555 mt555 6 98 03:55 L2
[mt555@localhost t3]$ cd ..
[mt555@localhost Desktop]$ cd t4
[mt555@localhost t4]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m2.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m3.txt
drwxrwxr-x. 2 mt555 mt555 6 98 01:02 m4.txt
[mt555@localhost t4]$ cd ..
[mt555@localhost Desktop]$ mv t3 t4  # 将目录t3移动到目录t4
[mt555@localhost Desktop]$ cd t4  # 进入目录t4,发现已有t3
[mt555@localhost t4]$ ll
总用量 0
drwxrwxr-x. 2 mt555 mt555  6 98 01:02 m2.txt
drwxrwxr-x. 2 mt555 mt555  6 98 01:02 m3.txt
drwxrwxr-x. 2 mt555 mt555  6 98 01:02 m4.txt
drwxrwxr-x. 4 mt555 mt555 24 98 03:55 t3
[mt555@localhost t4]$ 

例7.移动当前文件夹下的所有文件到上一级目录
命令:mv * ../

[mt555@localhost Desktop]$ ls  # 查看桌面文件,只有三个
f3.txt~  t3  t4
[mt555@localhost Desktop]$ cd t4 # 进入t4目录,并查看t4中的文件,有四个
[mt555@localhost t4]$ ls
m2.txt  m3.txt  m4.txt
[mt555@localhost t4]$ mv * ../  # 移动当前文件夹(t4)下的所有文件到上一级目录(Desktop)
[mt555@localhost t4]$ ls  # 此时t4中已无文件
[mt555@localhost t4]$ cd ..
[mt555@localhost Desktop]$ ls  # 桌面上多了t4中的文件
f3.txt~  m2.txt  m3.txt  m4.txt  t3  t4
[mt555@localhost Desktop]$ 

例8.把当前目录的一个子目录里的一类文件移动到另一个子目录里
命令:mv test3/*.txt test5

[mt555@localhost Desktop]$ ls         # 首先查看当前目录文件
f3.txt~  m2.txt  m3.txt  m4.txt  t3  t4
[mt555@localhost Desktop]$ cd m2.txt/ #查看当前目录中子目录里的文件
[mt555@localhost m2.txt]$ ls       # m2.txt中有hh.txt文件
hh.txt
[mt555@localhost m2.txt]$ cd ..
[mt555@localhost Desktop]$ cd m3.txt/  # m3.txt中有ww.txt和yy.txt两个文件
[mt555@localhost m3.txt]$ ls
ww.txt  yy.txt
[mt555@localhost m3.txt]$ cd ..
[mt555@localhost Desktop]$ mv m2.txt/*.txt m3.txt/  #把m2.txt中所有属于.txt的文件都移动到m3.txt中
[mt555@localhost Desktop]$ cd m3.txt/
[mt555@localhost m3.txt]$ ls
hh.txt  ww.txt  yy.txt
[mt555@localhost m3.txt]$ 

不好意思,上述例子有点乱,要理清文件目录,就很简单了。

例9.文件被覆盖前做简单备份,前面加参数-b
命令:mv m2.txt -b m3.txt

[mt555@localhost Desktop]$ ls
f1.txt  f2.txt 
[mt555@localhost Desktop]$ mv f1.txt -b f2.txt
mv:是否覆盖“log2.txt”? y
[mt555@localhost Desktop]$ ls
f1.txt  f2.txt  f2.txt~
[mt555@localhost Desktop]$

注意:
-b 不接受参数,mv会去读取环境变量VERSION_CONTROL来作为备份策略。
–backup该选项指定如果目标文件存在时的动作,共有四种备份策略:
1.CONTROL=none或off : 不备份。
2.CONTROL=numbered或t:数字编号的备份
3.CONTROL=existing或nil:如果存在以数字编号的备份,则继续编号备份m+1…n:
执行mv操作前已存在以数字编号的文件log2.txt.~1~,那么再次执行将产生log2.txt~2~,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。
4.CONTROL=simple或never:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值