mv命令是linux的常用命令,经常 用来备份文件或者目录。
命令格式:mv [选项] 源文件或者目录 目标文件或者目录
命令功能:视mv命令第二个参数的不同,mv命令将文件重命名或将其移动至另外一个目录
命令参数:
-b:若需要覆盖文件,则覆盖前先行备份。
-f:若目标文件已经存在,不会询问而直接覆盖。
-i:若目标文件已经存在的时候,就会询问是否覆盖。
-u:若目标文件已经存在,并且source比较新的时候,才会更新。
-t:用于移动多个文件到一个目标目录,此时目标文件在前,源文件在后。
命令实例:
(1)
mv test.log test001.log
说明:将文件重命名
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# mv test.log test001.log
[root@centos65 testorder]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# ls
test001.log
(2)
移动文件到指定目录
[root@centos65 testorder]# ll
total 4
drwxr-xr-x 2 root root 6 Aug 19 09:33 test
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# mv test001.log test
[root@centos65 testorder]#
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 24 Aug 19 09:33 test
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# cd test
[root@centos65 test]#
[root@centos65 test]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
(3)
将多个文件移动到test文件夹当中
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 24 Aug 19 09:33 test
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# cd ..
[root@centos65 local]#
[root@centos65 local]#
[root@centos65 local]# cd testorder/
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# pwd
/usr/local/testorder
[root@centos65 testorder]# mv -t /usr/local/testorder/test test002.log test003.log test004.log
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
[root@centos65 testorder]# cd test
[root@centos65 test]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
(5)
将test001.log更名为test002.log 如果test002.log已经存在 询问是否覆盖
[root@centos65 testorder]# touch test001.log
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# touch test002.log
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# mv -i test001.log test002.log
mv: overwrite `test002.log'? y
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
-rw-r--r-- 1 root root 0 Aug 19 09:41 test002.log
(6)将test001.log重命名为test002.log 即便test002.log存在也不需要询问
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
-rw-r--r-- 1 root root 0 Aug 19 09:43 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:41 test002.log
[root@centos65 testorder]#
[root@centos65 testorder]# mv -f test001.log test002.log
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
-rw-r--r-- 1 root root 0 Aug 19 09:43 test002.log
(7)目录的移动
mv dir001 dir002
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
drwxr-xr-x 2 root root 6 Aug 19 09:46 test001
-rw-r--r-- 1 root root 0 Aug 19 09:43 test002.log
[root@centos65 testorder]#
[root@centos65 testorder]# cd test
[root@centos65 test]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
[root@centos65 test]# cd ../test001
[root@centos65 test001]# cd ..
[root@centos65 testorder]# mv test test001
[root@centos65 testorder]# ll
total 0
drwxr-xr-x 3 root root 17 Aug 19 09:47 test001
-rw-r--r-- 1 root root 0 Aug 19 09:43 test002.log
[root@centos65 testorder]# cd test00
-bash: cd: test00: No such file or directory
[root@centos65 testorder]# cd test001
[root@centos65 test001]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
(8)
移动当前文件夹下的内容到上一个目录
[root@centos65 test001]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:38 test
[root@centos65 test001]#
[root@centos65 test001]# cd test
[root@centos65 test]#
[root@centos65 test]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
[root@centos65 test]# mv * ../
[root@centos65 test]# ll
total 0
[root@centos65 test]# cd ..
[root@centos65 test001]# ll
total 4
drwxr-xr-x 2 root root 6 Aug 19 09:50 test
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
(9)
把当前目录的一个子目录里面的文件移动到另外一个子文件里面
[root@centos65 test001]# ll
total 0
drwxr-xr-x 2 root root 78 Aug 19 09:54 test
drwxr-xr-x 2 root root 6 Aug 19 09:54 test001
[root@centos65 test001]# mv test/*.log test001
[root@centos65 test001]# ll
total 0
drwxr-xr-x 2 root root 6 Aug 19 09:55 test
drwxr-xr-x 2 root root 78 Aug 19 09:55 test001
[root@centos65 test001]# cd test
[root@centos65 test]# ll
total 0
[root@centos65 test]# cd ../test001
[root@centos65 test001]# ll
total 4
-rw-r--r-- 1 root root 40 Aug 19 09:24 test001.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test002.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:36 test004.log
(10)覆盖文件之前做简单备份
[root@centos65 testorder]# cat test002.log
nice to meet you!
[root@centos65 testorder]# mv test002.log -b test003.log
mv: overwrite `test003.log'? y
[root@centos65 testorder]#
[root@centos65 testorder]#
[root@centos65 testorder]# ll
total 4
drwxr-xr-x 4 root root 31 Aug 19 09:54 test001
-rw-r--r-- 1 root root 18 Aug 19 09:58 test003.log
-rw-r--r-- 1 root root 0 Aug 19 09:57 test003.log~
[root@centos65 testorder]# cat test003.log
nice to meet you!
[root@centos65 testorder]# cat test003.log~