linux中mv命令使⽤详解(移动⽂件或者将⽂件改名)mv命令是move的缩写,可以⽤来移动⽂件或者将⽂件改名(move (rename) files),是Linux系统下常⽤的命令,经常⽤来备份⽂件或者⽬录。
1.命令格式:
mv [选项] 源⽂件或⽬录⽬标⽂件或⽬录
2.命令功能:
视mv命令中第⼆个参数类型的不同(是⽬标⽂件还是⽬标⽬录),mv命令将⽂件重命名或将其移⾄⼀个新的⽬录中。当第⼆个参数类型是⽂件时,mv命令完成⽂件重命名,此时,源⽂件只能有⼀个(也可以是源⽬录名),它将所给的源⽂件或⽬录重命名为给定的⽬标⽂件名。当第⼆个参数是已存在的⽬录名称时,源⽂件或⽬录参数可以有多个,mv命令将各参数指定的源⽂件均移⾄⽬标⽬录中。在跨⽂件系统移动⽂件时,mv先拷贝,再将原有⽂件删除,⽽链⾄该⽂件的链接也将丢失。
3.命令参数:
-b :若需覆盖⽂件,则覆盖前先⾏备份。
-f :force 强制的意思,如果⽬标⽂件已经存在,不会询问⽽直接覆盖;
-i :若⽬标⽂件 (destination) 已经存在时,就会询问是否覆盖!
-u :若⽬标⽂件已经存在,且 source ⽐较新,才会更新(update)
-t  : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的⽬标⽬录,该选项适⽤于移动多个源⽂件到⼀个⽬录的情况,此时⽬标⽬录在前,源⽂件在后。
4.命令实例:
实例⼀:⽂件改名
命令:
mv test.
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
-rw-r--r-- 1 root root 16 10-28 06:04 test.log
[root@localhost test]# mv test.
[root@localhost test]# ll
总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf
-rw-r--r-- 1 root root 16 10-28 06:
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
说明:
将⽂件test.log重命名为
实例⼆:移动⽂件
命令:
test3
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf
-
rw-r--r-- 1 root root 29 10-28 06:
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# test3
[root@localhost test]# ll
总计 16drwxr-xr-x 6 root root 4096 10-27 01:58 scf drwxrwxrwx 2 root root 4096 10-28 06:09 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# cd test3
[root@localhost test3]# ll
总计 4
-rw-r--r-- 1 root root 29 10-28 06:
[root@localhost test3]#
说明:
将⽂件移到⽬录test3中
实例三:将⽂件,,移动到⽬录test3中。命令:
test3
mv -t /opt/soft/test/test4/  
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 28
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 12 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
drwxrwxrwx 2 root root 4096 10-28 06:09 test3
[root@localhost test]# test3 [root@localhost test]# ll
总计 16drwxrwxrwx 2 root root 4096 10-28 06:18 test3 [root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 16
-rw-r--r-- 1 root root 8 10-28 06:
-
rw-r--r-- 1 root root 12 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
-rw-r--r-- 1 root root 29 10-28 06:
[root@localhost test3]#
[root@localhost test3]# ll
总计 20
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 12 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:
[root@localhost test3]# mv -t /opt/soft/test/test4/
[root@localhost test3]# cd ..
[root@localhost test]# cd test4/
[root@localhost test4]# ll
总计 12
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 12 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]#
说明:
test3 命令将 ,, 三个⽂件移到 test3⽬录中去,mv -t /opt/soft/test/test4/ 命令⼜将三个⽂件移动到test4⽬录中去
实例四:将⽂件file1改名为file2,如果file2已经存在,则询问是否覆盖
命令:
mv -
输出:
复制代码
代码如下:
[root@localhost test4]# ll
总计 12
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 12 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]#
odfdfs
[root@localhost test4]#
ererwerwer
[root@localhost test4]# mv -
mv:是否覆盖“”? y
[root@localhost test4]#
odfdfs
[root@localhost test4]#
实例五:将⽂件file1改名为file2,即使file2存在,也是直接覆盖掉。
命令:
mv -
输出:
复制代码
代码如下:
[root@localhost test4]# ll
总计 8
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]#
odfdfs
[root@localhost test4]# cat log3
cat: log3: 没有那个⽂件或⽬录
[root@localhost test4]# ll
总计 8
-rw-r--r-- 1 root root 8 10-28 06:
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]#
odfdfs
[root@localhost test4]#
dfosdfsdfdss
[root@localhost test4]# mv -
[root@localhost test4]#
dfosdfsdfdss
[root@localhost test4]# ll
总计 4
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]#
说明:
<的内容直接覆盖了内容,-f 这是个危险的选项,使⽤的时候⼀定要保持头脑清晰,⼀般情况下最好不⽤加上它。
实例六:⽬录的移动
命令:
mv dir1 dir2
输出:
复制代码
代码如下:
[root@localhost test4]# ll
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]# ll
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]# cd ..
[root@localhost test]# ll
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 3 root root 4096 10-28 06:24 test3
drwxr-xr-x 2 root root 4096 10-28 06:48 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# cd test3
[root@localhost test3]# ll
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:
[root@localhost test3]# cd ..
[root@localhost test]# mv test4 test3
[root@localhost test]# ll
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 4 root root 4096 10-28 06:54 test3
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# cd test3/
[root@localhost test3]# ll
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 06:48 test4
[root@localhost test3]#
说明:
如果⽬录dir2不存在,将⽬录dir1改名为dir2;否则,将dir1移动到dir2中。
实例7:移动当前⽂件夹下的所有⽂件到上⼀级⽬录
命令:
mv * ../
输出:
复制代码
代码如下:
[root@localhost test4]# ll
-rw-r--r-- 1 root root 25 10-28 07:
-rw-r--r-- 1 root root 13 10-28 06:
[root@localhost test4]# mv * ../
[root@localhost test4]# ll
[root@localhost test4]# cd ..
[root@localhost test3]# ll
-rw-r--r-- 1 root root 25 10-28 07:
-
rw-r--r-- 1 root root 13 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 07:02 test4
实例⼋:把当前⽬录的⼀个⼦⽬录⾥的⽂件移动到另⼀个⼦⽬录⾥命令:
mv test3/*.txt test5
输出:
复制代码
代码如下:
[root@localhost test]# ll
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 4 root root 4096 10-28 07:02 test3
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# cd test3
linux修改文件夹名称[root@localhost test3]# ll
-rw-r--r-- 1 root root 25 10-28 07:
-rw-r--r-- 1 root root 13 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:
drwxr-xr-x 2 root root 4096 10-28 07:02 test4
[root@localhost test3]# cd ..
[root@localhost test]# mv test3/*.txt test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
-rw-r--r-- 1 root root 25 10-28 07:
-rw-r--r-- 1 root root 13 10-28 06:
-rw-r--r-- 1 root root 29 10-28 06:
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1
[root@localhost test5]# cd ..
[root@localhost test]# cd test3/
[root@localhost test3]# ll
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
drwxr-xr-x 2 root root 4096 10-28 07:02 test4