1、命令格式:
mv [選項] 源文件或目錄 目標文件或目錄
2、命令功能:
Linux mv命令用來為文件或目錄改名、或將文件或目錄移入其它位置。
3、命令參數:
-b :若需覆蓋文件,則覆蓋前先行備份。 -f :force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋; -i :若目標文件 (destination) 已經存在時,就會詢問是否覆蓋! -u :若目標文件已經存在,且 source 比較新,才會更新(update) -t : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的情況,此時目標目錄在前,源文件在后。
4、簡單實例:
(1)、文件改名
命令:
mv test.txt ttt.txt
輸出:
felix@felix-computer:~/test$ ls test.txt felix@felix-computer:~/test$ mv test.txt ttt.txt felix@felix-computer:~/test$ ls ttt.txt felix@felix-computer:~/test$
(2)、移動文件
命令:
mv ttt.txt test3
輸出:
felix@felix-computer:~/test$ tree . ├── test3 └── ttt.txt 1 directory, 1 file felix@felix-computer:~/test$ mv ttt.txt test3/ felix@felix-computer:~/test$ tree . └── test3 └── ttt.txt 1 directory, 1 file felix@felix-computer:~/test$
(3)、移動多個文件到指定目錄
命令:
mv -t test4/ test3/*
輸出:
felix@felix-computer:~/test$ tree . ├── test3 │ ├── 1.txt │ ├── 2.txt │ ├── 3.txt │ ├── 4.txt │ └── ttt.txt └── test4 2 directories, 5 files felix@felix-computer:~/test$ mv -t test4/ test3/* felix@felix-computer:~/test$ tree . ├── test3 └── test4 ├── 1.txt ├── 2.txt ├── 3.txt ├── 4.txt └── ttt.txt 2 directories, 5 files felix@felix-computer:~/test$
(4)、將文件1命名為文件2,如果文件2已存在,詢問是否覆蓋
命令:
mv -i 3.txt 1.txt
輸出:
felix@felix-computer:~/test/test4$ ls 1.txt 2.txt 3.txt 4.txt ttt.txt felix@felix-computer:~/test/test4$ mv -i 3.txt 1.txt mv:是否覆蓋'1.txt'? y felix@felix-computer:~/test/test4$ ls 1.txt 2.txt 4.txt ttt.txt felix@felix-computer:~/test/test4$
(5)、將文件1命名為文件2,如果文件2已存在,直接覆蓋
命令:
mv -f 2.txt 1.txt
輸出:
felix@felix-computer:~/test/test4$ mv -f 2.txt 1.txt felix@felix-computer:~/test/test4$ ls 1.txt 4.txt ttt.txt felix@felix-computer:~/test/test4$
(6)、目錄移動,如果目錄dir2不存在,將目錄dir1改名為dir2;否則,將dir1移動到dir2中
命令:
mv test4 test3
輸出:
felix@felix-computer:~/test$ tree . ├── test3 └── test4 ├── 1.txt ├── 4.txt └── ttt.txt 2 directories, 3 files felix@felix-computer:~/test$ mv test4 test3 felix@felix-computer:~/test$ tree . └── test3 └── test4 ├── 1.txt ├── 4.txt └── ttt.txt 2 directories, 3 files felix@felix-computer:~/test$
(7)、文件被覆蓋前做簡單備份
命令:
mv 2.txt -b 1.txt
輸出:
felix@felix-computer:~/test/test3/test4$ ls 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt felix@felix-computer:~/test/test3/test4$ mv 2.txt -b 1.txt felix@felix-computer:~/test/test3/test4$ ls 1.txt 1.txt~ 3.txt 4.txt 5.txt 6.txt felix@felix-computer:~/test/test3/test4$