国产宅男网站在线|亚洲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>
    學習啦 > 學習電腦 > 操作系統 > Linux教程 > linux中的mv命令

    linux中的mv命令

    時間: 佳洲1085 分享

    linux中的mv命令

      Linux系統中的mv令是一個常用的基本命令,它的主要功能是對文件或目錄重命名,或者移除目錄。下面由學習啦小編為大家整理了linux中的mv命令的相關知識,希望對大家有幫助!

      linux中的mv令詳解

      mv命令用來對文件或目錄重新命名,或者將文件從一個目錄移到另一個目錄中。

      注意事項:mv與cp的結果不同,mv好像文件“搬家”,文件個數并未增加。而cp對文件進行復制,文件個數增加了。

      (1)用法:

      用法: mv [選項]... [-T] 源文件 目標文件

      或: mv [選項]... 源文件... 目錄

      或: mv [選項]... -t 目錄 源文件...

      (2)功能:

      將源文件重命名為目標文件,或將源文件移動至指定目錄。

      (3)選項參數:

      1) -b: 當文件存在時,覆蓋前,為其創(chuàng)建一個備份

      2) -f 若目標文件或目錄與現有的文件或目錄重復,則直接覆蓋現有的文件或目錄

      3) -i 交互式操作,覆蓋前先行詢問用戶,如果源文件與目標文件或目標目錄中的文件同名,則詢問用戶是否覆蓋目標文件。這樣可以避免誤將文件覆蓋。

      4) -f -force 強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋

      5) -u 若目標文件已經存在,且 source 比較新,才會更新(update)

      linux中的mv命令實例

      1)[sunjimeng@localhost Document]$ mv text1 mytext 由于此處源文件test1與目標文件是在同一目錄下,可以看作僅僅是改了文件的名字

      [sunjimeng@localhost Document]$ ll //目錄下為空

      總用量 0

      [sunjimeng@localhost Document]$ cat >text1 <<EOF //新建文件文檔并從標準輸入中輸入數據到文件

      > I am MenAngel

      > PWD=$(pwd)

      > I am testing the order of mv!

      > EOF

      [sunjimeng@localhost Document]$ ll

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 text1

      [sunjimeng@localhost Document]$ mv text1 mytext //執(zhí)行mv命令

      [sunjimeng@localhost Document]$ cat mytext

      I am MenAngel

      PWD=/home/sunjimeng/Document

      I am testing the order of mv!

      [sunjimeng@localhost Document]$ ll //可見已經改名

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext

      [sunjimeng@localhost Document]$

      2)[sunjimeng@localhost Document]$ mv mytext{,.txt} 與[sunjimeng@localhost Document]$ mv text text.txt 給文件名增加后綴

      [sunjimeng@localhost Document]$ mv mytext{,.txt} //增加后綴名的原始方法

      [sunjimeng@localhost Document]$ ll

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      [sunjimeng@localhost Document]$ touch text

      [sunjimeng@localhost Document]$ ll

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

      [sunjimeng@localhost Document]$ mv text text.txt //利用mv的改名目錄增加后綴

      [sunjimeng@localhost Document]$ ll

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

      3)[root@localhost Documents]# mv ../Document/* . 將文件從源目錄移動到目標目錄,這里源目錄和目標目錄可以任意指定。.代表當前目錄

      [sunjimeng@localhost Document]$ ll //Document下游兩個文件

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

      [sunjimeng@localhost Document]$ cd ../Documents //進入同級兄弟目錄Documents,發(fā)現其下為空

      [sunjimeng@localhost Documents]$ ll

      總用量 0

      [sunjimeng@localhost Documents]$ mv ../Document/* . //將Document下的所有文件(*),移動到當前目錄(.)。

      mv: 無法將"../Document/mytext.txt" 移動至"./mytext.txt": 權限不夠 //Linux用組名和用戶名來管理文件,此時當前用戶沒有權限移動文件,必須改為root用戶

      mv: 無法將"../Document/text.txt" 移動至"./text.txt": 權限不夠

      [sunjimeng@localhost Documents]$ su root

      密碼:

      ABRT 已檢測到 '1' 個問題。預了解詳細信息請執(zhí)行:abrt-cli list --since 1462423345

      [root@localhost Documents]# mv ../Document/* .

      [root@localhost Documents]# ll //移動完成

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

      [root@localhost Documents]# ls -l ../Document //查看Document目錄已經沒有任何東西

      總用量 0

      4)[root@localhost Documents]# mv -t ../Document ./* 功能同(3),但區(qū)別是源文件的路徑和目標路徑的位置發(fā)生了變化

      [root@localhost Documents]# ll

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

      [root@localhost Documents]# mv -t ./* ../Document //-t參數的功能就是讓他們的位置發(fā)生變化,這里第一個參數是目標路徑

      mv: 目標"./mytext.txt" 不是目錄

      [root@localhost Documents]# mv -t ../Document ./* //位置調換一下就行了

      [root@localhost Documents]# ll

      總用量 0

      [root@localhost Documents]# ll

      總用量 0

      [root@localhost Documents]# ls -l ../Document

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

      5)[root@localhost Document]# mv mytext.txt mytext 如果第二個參數不是目錄名,才將源文件改名,否則,移動源文件到該目錄下(與實例1作比較)

      [root@localhost Document]# mkdir mytext

      [root@localhost Document]# ll

      總用量 4

      drwxr-xr-x. 2 root root 6 5月 5 22:34 mytext

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

      [root@localhost Document]# mv mytext.txt mytext //與實例一不同的是,這里mytext是個目錄

      [root@localhost Document]# ll

      總用量 0

      drwxr-xr-x. 2 root root 23 5月 5 22:35 mytext

      -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

      [root@localhost Document]# ls -l mytext

      總用量 4

      -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

      6)[root@localhost Document]# mv -b myword text 源文件和目標文件都是存在的,因此會有覆蓋提醒,-b用于在覆蓋時備份文件

      [root@localhost Document]# cat >myword <<EOF

      > this is my word!

      > EOF

      [root@localhost Document]# cat >text <<EOF

      > this is my text!

      > EOF

      [root@localhost Document]# mv -b myword text //在一個文件即將覆蓋另一個文件時,默認是提醒的,所以加上-i參數和不加是一樣的

      mv:是否覆蓋"text"? y

      [root@localhost Document]# cat myword

      cat: myword: 沒有那個文件或目錄

      [root@localhost Document]# cat text

      this is my word!

      [root@localhost Document]# ll

      總用量 8

      drwxr-xr-x. 2 root root 23 5月 5 22:35 mytext //這里text里存的是前面myword的內容,text的內容備份到text~中,需要特殊軟件才能查看

      -rw-r--r--. 1 root root 17 5月 5 22:41 text

      -rw-rw-r--. 1 sunjimeng sunjimeng 17 5月 5 22:41 text~

      7) [root@localhost text]# mv * ../ 將當前目錄下的所有內容移動到父級目錄(特殊情況)

      [root@localhost Document]# mkdir text

      [root@localhost Document]# touch ./text/{text1,text2,text3}

      [root@localhost Document]# cd text

      [root@localhost text]# mv * ../

      [root@localhost text]# cd ../

      [root@localhost Document]# ll

      總用量 0

      drwxr-xr-x. 2 root root 6 5月 5 22:57 text

      -rw-r--r--. 1 root root 0 5月 5 22:57 text1

      -rw-r--r--. 1 root root 0 5月 5 22:57 text2

      -rw-r--r--. 1 root root 0 5月 5 22:57 text3

      8)[root@localhost Document]# mv -f text2 text3 強制執(zhí)行操作,并不做任何提醒

      9)[root@localhost Document]# mv -i text2 text3 加不加-i在覆蓋時都會提醒

      [root@localhost Document]# ll

      總用量 0

      drwxr-xr-x. 2 root root 6 5月 5 23:05 text

      -rw-r--r--. 1 root root 0 5月 5 22:57 text2

      -rw-r--r--. 1 root root 0 5月 5 22:57 text3

      -rw-r--r--. 1 root root 0 5月 5 22:57 text4

      [root@localhost Document]# mv text2 text3

      mv:是否覆蓋"text3"? n

      [root@localhost Document]# mv -i text2 text3

      mv:是否覆蓋"text3"? n

      [root@localhost Document]# mv -f text2 text3

      [root@localhost Document]# ll

      總用量 0

      drwxr-xr-x. 2 root root 6 5月 5 23:05 text

      -rw-r--r--. 1 root root 0 5月 5 22:57 text3

      -rw-r--r--. 1 root root 0 5月 5 22:57 text4

      10)[root@localhost Document]# mv Dir text 將Dir目錄移動到text目錄下(text存在時),如果不存在直接將Dir改名為text

      [root@localhost Document]# mkdir testDir

      [root@localhost Document]# ll //下面的操作先將文件text3和text4放到textDir目錄下

      總用量 0

      drwxr-xr-x. 2 root root 6 5月 5 23:09 testDir

      drwxr-xr-x. 2 root root 6 5月 5 23:05 text

      -rw-r--r--. 1 root root 0 5月 5 22:57 text3

      -rw-r--r--. 1 root root 0 5月 5 22:57 text4

      [root@localhost Document]# mv {text3,text4} ./testDir

      [root@localhost Document]# mv testDir Dir //由于Dir不存在,所以testDir改名為Dir

      [root@localhost Document]# mv Dir text //由于text是存在的,所以將Dir移到text目錄下

      [root@localhost Document]# ll

      總用量 0

      drwxr-xr-x. 3 root root 16 5月 5 23:10 text //下面驗證了這一點

      [root@localhost Document]# cd text

      [root@localhost text]# ll

      總用量 0

      drwxr-xr-x. 2 root root 30 5月 5 23:09 Dir

      [root@localhost text]# cd Dir

      [root@localhost Dir]# ll

      總用量 0

      -rw-r--r--. 1 root root 0 5月 5 22:57 text3

      -rw-r--r--. 1 root root 0 5月 5 22:57 text4

      11)[root@localhost /]# mv --help

      [root@localhost /]# mv --help

      用法:mv [選項]... [-T] 源文件 目標文件

      或:mv [選項]... 源文件... 目錄

      或:mv [選項]... -t 目錄 源文件...

      Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

      Mandatory arguments to long options are mandatory for short options too.

      --backup[=CONTROL] 為每個已存在的目標文件創(chuàng)建備份

      -b 類似--backup 但不接受參數

      -f, --force 覆蓋前不詢問

      -i, --interactive 覆蓋前詢問

      -n, --no-clobber 不覆蓋已存在文件

      如果您指定了-i、-f、-n 中的多個,僅最后一個生效。

      --strip-trailing-slashes 去掉每個源文件參數尾部的斜線

      -S, --suffix=SUFFIX 替換常用的備份文件后綴

      -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY

      -T, --no-target-directory treat DEST as a normal file

      -u, --update move only when the SOURCE file is newer

      than the destination file or when the

      destination file is missing

      -v, --verbose explain what is being done

      -Z, --context set SELinux security context of destination

      file to default type

      --help 顯示此幫助信息并退出

      --version 顯示版本信息并退出

      The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.

      The version control method may be selected via the --backup option or through

      the VERSION_CONTROL environment variable. Here are the values:

      none, off 不進行備份(即使使用了--backup 選項)

      numbered, t 備份文件加上數字進行排序

      existing, nil 若有數字的備份文件已經存在則使用數字,否則使用普通方式備份

      simple, never 永遠使用普通方式備份

      GNU coreutils online help: <http://www.gnu.org/software/coreutils/>

      請向<http://translationproject.org/team/zh_CN.html> 報告mv 的翻譯錯誤

      要獲取完整文檔,請運行:info coreutils 'mv invocation'

      12)[root@localhost /]# mv --version

      [root@localhost /]# mv --version

      mv (GNU coreutils) 8.22

      Copyright (C) 2013 Free Software Foundation, Inc.

      許可證:GPLv3+:GNU 通用公共許可證第3 版或更新版本<http://gnu.org/licenses/gpl.html>。

      本軟件是自由軟件:您可以自由修改和重新發(fā)布它。

      在法律范圍內沒有其他保證。

      由Mike Parker、David MacKenzie 和Jim Meyering 編寫。

      補充:其他

      用-b做備份時:

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

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

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

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

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

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

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

    3626106