linux中的mv命令的詳細解釋(2)
三、Linux中的mv命令使用實例——目錄操作
實例1:目錄的移動
命令:
mv dir1 dir2
輸出:
復制代碼
代碼如下:
[root@localhost test4]# ll
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
[root@localhost test4]# ll
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
[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:05 test1.txt
[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:05 test1.txt
drwxr-xr-x 2 root root 4096 10-28 06:48 test4
[root@localhost test3]#
說明:
如果目錄dir2不存在,將目錄dir1改名為dir2;否則,將dir1移動到dir2中。
實例2:移動當前文件夾下的所有文件到上一級目錄
命令:
mv * ../
輸出:
復制代碼
代碼如下:
[root@localhost test4]# ll
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
[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:02 log1.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
drwxr-xr-x 2 root root 4096 10-28 07:02 test4
實例3:把當前目錄的一個子目錄里的文件移動到另一個子目錄里
命令:
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
[root@localhost test3]# ll
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
drwxr-xr-x 2 root root 4096 10-28 06:21 logs
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
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:02 log1.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
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
[root@localhost test3]#
實例4:文件被覆蓋前做簡單備份,前面加參數(shù)-b
命令:
mv log1.txt -b log2.txt
輸出:
復制代碼
代碼如下:
[root@localhost test5]# ll
-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1
[root@localhost test5]# mv log1.txt -b log2.txt
mv:是否覆蓋“log2.txt”? y
[root@localhost test5]# ll
-rw-r--r-- 1 root root 25 10-28 07:02 log2.txt
-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt~
-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1
[root@localhost test5]#