linux系列(七):mv命令
阿新 • • 發佈:2018-11-06
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
輸出:
[email protected]:~/test$ ls test.txt [email protected]-computer:~/test$ mv test.txt ttt.txt [email protected]-computer:~/test$ ls ttt.txt [email protected]-computer:~/test$
(2)、移動檔案
命令:
mv ttt.txt test3
輸出:
[email protected]:~/test$ tree . ├── test3 └── ttt.txt 1 directory, 1 file [email protected]-computer:~/test$ mv ttt.txt test3/ [email protected]-computer:~/test$ tree . └── test3 └── ttt.txt 1 directory, 1 file [email protected]-computer:~/test$
(3)、移動多個檔案到指定目錄
命令:
mv -t test4/ test3/*
輸出:
[email protected]:~/test$ tree . ├── test3 │ ├── 1.txt │ ├── 2.txt │ ├── 3.txt │ ├── 4.txt │ └── ttt.txt └── test4 2 directories, 5 files [email protected]-computer:~/test$ mv -t test4/ test3/* [email protected]-computer:~/test$ tree . ├── test3 └── test4 ├── 1.txt ├── 2.txt ├── 3.txt ├── 4.txt └── ttt.txt 2 directories, 5 files [email protected]-computer:~/test$
(4)、將檔案1命名為檔案2,如果檔案2已存在,詢問是否覆蓋
命令:
mv -i 3.txt 1.txt
輸出:
[email protected]:~/test/test4$ ls 1.txt 2.txt 3.txt 4.txt ttt.txt [email protected]-computer:~/test/test4$ mv -i 3.txt 1.txt mv:是否覆蓋'1.txt'? y [email protected]-computer:~/test/test4$ ls 1.txt 2.txt 4.txt ttt.txt [email protected]-computer:~/test/test4$
(5)、將檔案1命名為檔案2,如果檔案2已存在,直接覆蓋
命令:
mv -f 2.txt 1.txt
輸出:
[email protected]:~/test/test4$ mv -f 2.txt 1.txt [email protected]-computer:~/test/test4$ ls 1.txt 4.txt ttt.txt [email protected]-computer:~/test/test4$
(6)、目錄移動,如果目錄dir2不存在,將目錄dir1改名為dir2;否則,將dir1移動到dir2中
命令:
mv test4 test3
輸出:
[email protected]:~/test$ tree . ├── test3 └── test4 ├── 1.txt ├── 4.txt └── ttt.txt 2 directories, 3 files [email protected]-computer:~/test$ mv test4 test3 [email protected]-computer:~/test$ tree . └── test3 └── test4 ├── 1.txt ├── 4.txt └── ttt.txt 2 directories, 3 files [email protected]-computer:~/test$
(7)、檔案被覆蓋前做簡單備份
命令:
mv 2.txt -b 1.txt
輸出:
[email protected]:~/test/test3/test4$ ls 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt [email protected]-computer:~/test/test3/test4$ mv 2.txt -b 1.txt [email protected]-computer:~/test/test3/test4$ ls 1.txt 1.txt~ 3.txt 4.txt 5.txt 6.txt [email protected]-computer:~/test/test3/test4$