国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

    linux命令之mv命令

    時(shí)間: 佳洲1085 分享

      linux系統(tǒng)中mv命令是用來移動文件,是linux系統(tǒng)下常用的基本命令之一。下面由學(xué)習(xí)啦小編為大家整理了linux命令之mv命令的相關(guān)知識,希望對大家有所幫助!

      linux命令之mv命令詳解

      mv命令是move的縮寫,可以用來移動文件或者將文件改名(move (rename) files),是Linux系統(tǒng)下常用的命令,經(jīng)常用來備份文件或者目錄。

      1.命令格式:

      mv [選項(xiàng)] 源文件或目錄 目標(biāo)文件或目錄

      2.命令功能:

      視mv命令中第二個(gè)參數(shù)類型的不同(是目標(biāo)文件還是目標(biāo)目錄),mv命令將文件重命名或?qū)⑵湟浦烈粋€(gè)新的目錄中。當(dāng)?shù)诙€(gè)參數(shù)類型是文件時(shí),mv命令完成文件重命名,此時(shí),源文件只能有一個(gè)(也可以是源目錄名),它將所給的源文件或目錄重命名為給定的目標(biāo)文件名。當(dāng)?shù)诙€(gè)參數(shù)是已存在的目錄名稱時(shí),源文件或目錄參數(shù)可以有多個(gè),mv命令將各參數(shù)指定的源文件均移至目標(biāo)目錄中。在跨文件系統(tǒng)移動文件時(shí),mv先拷貝,再將原有文件刪除,而鏈至該文件的鏈接也將丟失。

      3.命令參數(shù):

      -b :若需覆蓋文件,則覆蓋前先行備份。

      -f :force 強(qiáng)制的意思,如果目標(biāo)文件已經(jīng)存在,不會詢問而直接覆蓋;

      -i :若目標(biāo)文件 (destination) 已經(jīng)存在時(shí),就會詢問是否覆蓋!

      -u :若目標(biāo)文件已經(jīng)存在,且 source 比較新,才會更新(update)

      -t : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目標(biāo)目錄,該選項(xiàng)適用于移動多個(gè)源文件到一個(gè)目錄的情況,此時(shí)目標(biāo)目錄在前,源文件在后。

      linux命令之mv命令實(shí)例

      實(shí)例一:文件改名

      命令:

      mv test.log test1.txt

      輸出:

      [root@localhost test]# ll

      總計(jì) 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.log test1.txt

      [root@localhost test]# ll

      總計(jì) 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf

      -rw-r--r-- 1 root root 16 10-28 06:04 test1.txt

      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重命名為test1.txt

      實(shí)例二:移動文件

      命令:

      mv test1.txt test3

      輸出:

      [root@localhost test]# ll

      總計(jì) 20drwxr-xr-x 6 root root 4096 10-27 01:58 scf

      -rw-r--r-- 1 root root 29 10-28 06:05 test1.txt

      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]# mv test1.txt test3

      [root@localhost test]# ll

      總計(jì) 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

      總計(jì) 4

      -rw-r--r-- 1 root root 29 10-28 06:05 test1.txt

      [root@localhost test3]#

      說明:

      將test1.txt文件移到目錄test3中

      實(shí)例三:將文件log1.txt,log2.txt,log3.txt移動到目錄test3中。

      命令:

      mv log1.txt log2.txt log3.txt test3

      mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt

      輸出:

      [root@localhost test]# ll

      總計(jì) 28

      -rw-r--r-- 1 root root 8 10-28 06:15 log1.txt

      -rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      drwxrwxrwx 2 root root 4096 10-28 06:09 test3

      [root@localhost test]# mv log1.txt log2.txt log3.txt test3

      [root@localhost test]# ll

      總計(jì) 16drwxrwxrwx 2 root root 4096 10-28 06:18 test3

      [root@localhost test]# cd test3/

      [root@localhost test3]# ll

      總計(jì) 16

      -rw-r--r-- 1 root root 8 10-28 06:15 log1.txt

      -rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      -rw-r--r-- 1 root root 29 10-28 06:05 test1.txt

      [root@localhost test3]#

      [root@localhost test3]# ll

      總計(jì) 20

      -rw-r--r-- 1 root root 8 10-28 06:15 log1.txt

      -rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.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

      [root@localhost test3]# mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt

      [root@localhost test3]# cd ..

      [root@localhost test]# cd test4/

      [root@localhost test4]# ll

      總計(jì) 12

      -rw-r--r-- 1 root root 8 10-28 06:15 log1.txt

      -rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      [root@localhost test4]#

      說明:

      mv log1.txt log2.txt log3.txt test3 命令將log1.txt ,log2.txt, log3.txt 三個(gè)文件移到 test3目錄中去,mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt 命令又將三個(gè)文件移動到test4目錄中去

      實(shí)例四:將文件file1改名為file2,如果file2已經(jīng)存在,則詢問是否覆蓋

      命令:

      mv -i log1.txt log2.txt

      輸出:

      [root@localhost test4]# ll

      總計(jì) 12

      -rw-r--r-- 1 root root 8 10-28 06:15 log1.txt

      -rw-r--r-- 1 root root 12 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      [root@localhost test4]# cat log1.txt

      odfdfs

      [root@localhost test4]# cat log2.txt

      ererwerwer

      [root@localhost test4]# mv -i log1.txt log2.txt

      mv:是否覆蓋“log2.txt”? y

      [root@localhost test4]# cat log2.txt

      odfdfs

      [root@localhost test4]#

      實(shí)例五:將文件file1改名為file2,即使file2存在,也是直接覆蓋掉。

      命令:

      mv -f log3.txt log2.txt

      輸出:

      [root@localhost test4]# ll

      總計(jì) 8

      -rw-r--r-- 1 root root 8 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      [root@localhost test4]# cat log2.txt

      odfdfs

      [root@localhost test4]# cat log3

      cat: log3: 沒有那個(gè)文件或目錄

      [root@localhost test4]# ll

      總計(jì) 8

      -rw-r--r-- 1 root root 8 10-28 06:15 log2.txt

      -rw-r--r-- 1 root root 13 10-28 06:16 log3.txt

      [root@localhost test4]# cat log2.txt

      odfdfs

      [root@localhost test4]# cat log3.txt

      dfosdfsdfdss

      [root@localhost test4]# mv -f log3.txt log2.txt

      [root@localhost test4]# cat log2.txt

      dfosdfsdfdss

      [root@localhost test4]# ll

      總計(jì) 4

      -rw-r--r-- 1 root root 13 10-28 06:16 log2.txt

      [root@localhost test4]#

      說明:

      log3.txt的內(nèi)容直接覆蓋了log2.txt內(nèi)容,-f 這是個(gè)危險(xiǎn)的選項(xiàng),使用的時(shí)候一定要保持頭腦清晰,一般情況下最好不用加上它。

      實(shí)例六:目錄的移動

      命令:

      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中。

      實(shí)例7:移動當(dāng)前文件夾下的所有文件到上一級目錄

      命令:

      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

      實(shí)例八:把當(dāng)前目錄的一個(gè)子目錄里的文件移動到另一個(gè)子目錄里

      命令:

      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]#

      實(shí)例九:文件被覆蓋前做簡單備份,前面加參數(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]#

      說明:

      -b 不接受參數(shù),mv會去讀取環(huán)境變量VERSION_CONTROL來作為備份策略。

      --backup該選項(xiàng)指定如果目標(biāo)文件存在時(shí)的動作,共有四種備份策略:

      1.CONTROL=none或off : 不備份。

      2.CONTROL=numbered或t:數(shù)字編號的備份

      3.CONTROL=existing或nil:如果存在以數(shù)字編號的備份,則繼續(xù)編號備份m+1...n:

      執(zhí)行mv操作前已存在以數(shù)字編號的文件log2.txt.~1~,那么再次執(zhí)行將產(chǎn)生log2.txt~2~,以次類推。如果之前沒有以數(shù)字編號的文件,則使用下面講到的簡單備份。

      4.CONTROL=simple或never:使用簡單備份:在被覆蓋前進(jìn)行了簡單備份,簡單備份只能有一份,再次被覆蓋時(shí),簡單備份也會被覆蓋。

    3604057