每天一個linux命令:mv(7)


mv

mv命令可以用來移動文件或者將文件改名(move (rename) files),是Linux系統下常用的命令,經常用來備份文件或者目錄。 在跨文件系統移動文件時,mv先拷貝,再將原有文件刪除,而鏈至該文件的鏈接也將丟失

格式

mv [選項] 源文件或目錄 目標文件或目錄

參數選項

參數 備注
-b 若需覆蓋文件,則覆蓋前先行備份。
-f force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋;
-i 若目標文件 (destination) 已經存在時,就會詢問是否覆蓋!
-u 若目標文件已經存在,且 source 比較新,才會更新(update)
-t --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的情況,此時目標目錄在前,源文件在后。

實例

  • 文件改名

    命令:mv oldNameFile newNameFile

    [root@VM_0_9_centos ~]# mkdir test
    [root@VM_0_9_centos ~]# cd test
    [root@VM_0_9_centos test]# touch oldNameFile
    [root@VM_0_9_centos test]# ls
    oldNameFile
    [root@VM_0_9_centos test]# mv oldNameFile newNameFile
    [root@VM_0_9_centos test]# ls
    newNameFile
    [root@VM_0_9_centos test]# 
    
  • 移動文件

    命令:mv myFile.txt ./temp/myFile.txt

    [root@VM_0_9_centos test]# touch myFile.txt
    [root@VM_0_9_centos test]# tree
    .
    |-- myFile.txt
    `-- temp
    
    1 directory, 1 file
    
    [root@VM_0_9_centos test]# mv myFile.txt ./temp/myFile.txt
    [root@VM_0_9_centos test]# tree
    .
    `-- temp
        `-- myFile.txt
    
    1 directory, 1 file
    
  • 將文件myFile1改名為MyFile2,即使MyFile2存在,也是直接覆蓋掉

    命令:mv -f myFile1 myFile2

    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile1
    -rw-r--r-- 1 root root 0 Oct 27 10:06 myFile2
    [root@VM_0_9_centos test]# mv myFile1 myFile2
    mv: overwrite ?.yFile2?. n
    [root@VM_0_9_centos test]# mv -f  myFile1 myFile2
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2
    [root@VM_0_9_centos test]# 
    
  • 文件覆蓋前做簡單備份

    命令: **mv -b myFile1 myFile2 **

    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:14 myFile1
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2
    [root@VM_0_9_centos test]# mv -b myFile1 myFile2
    mv: overwrite ?.yFile2?. y
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:14 myFile2
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2~
    

    -b 不接受參數,mv會去讀取環境變量VERSION_CONTROL來作為備份策略。

    --backup該選項指定如果目標文件存在時的動作,共有四種備份策略:

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

    2.CONTROL=numbered或t:數字編號的備份

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

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

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

    [root@VM_0_9_centos test]# mv -b --backup=numbered myFile2
    mv: missing destination file operand after ?.yFile2?
    Try 'mv --help' for more information.
    [root@VM_0_9_centos test]# mv -b --backup=numbered myFile1 myFile2
    mv: overwrite ?.yFile2?. y
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:27 myFile2
    -rw-r--r-- 1 root root 0 Oct 27 10:16 myFile2.~1~
    [root@VM_0_9_centos test]# 
    
  • 移動多個文件到一個目錄

    命令:mv -t backup/ myFile2~ myFile2.1

    [root@VM_0_9_centos test]# mkdir backup
    [root@VM_0_9_centos test]# ll
    total 4
    drwxr-xr-x 2 root root 4096 Oct 27 10:33 backup
    -rw-r--r-- 1 root root    0 Oct 27 10:27 myFile2
    -rw-r--r-- 1 root root    0 Oct 27 10:16 myFile2~
    -rw-r--r-- 1 root root    0 Oct 27 10:16 myFile2.~1~
    [root@VM_0_9_centos test]# mv -t backup/ myFile2~ myFile2.~1~ 
    [root@VM_0_9_centos test]# tree
    .
    |-- backup
    |   |-- myFile2~
    |   `-- myFile2.~1~
    `-- myFile2
    
    1 directory, 3 files
    

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM