mv命令用來對文件或目錄從新命名,或者將文件從一個目錄移到另外一個目錄中。html
注意事項:mv與cp的結果不一樣,mv好像文件「搬家」,文件個數並未增長。而cp對文件進行復制,文件個數增長了。less
(1)用法:this
用法: mv [選項]... [-T] 源文件 目標文件 spa
或: mv [選項]... 源文件... 目錄 翻譯
或: mv [選項]... -t 目錄 源文件... code
(2)功能:orm
將源文件重命名爲目標文件,或將源文件移動至指定目錄。htm
(3)選項參數:blog
1) -b: 當文件存在時,覆蓋前,爲其建立一個備份排序
2) -f 若目標文件或目錄與現有的文件或目錄重複,則直接覆蓋現有的文件或目錄
3) -i 交互式操做,覆蓋前先行詢問用戶,若是源文件與目標文件或目標目錄中的文件同名,則詢問用戶是否覆蓋目標文件。這樣能夠避免誤將文件覆蓋。
4) -f -force 強制的意思,若是目標文件已經存在,不會詢問而直接覆蓋
5) -u 若目標文件已經存在,且 source 比較新,纔會更新(update)
(4)實例:
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 //執行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,發現其下爲空 [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' 個問題。預瞭解詳細信息請執行: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),但區別是源文件的路徑和目標路徑的位置發生了變化
[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參數的功能就是讓他們的位置發生變化,這裏第一個參數是目標路徑 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 強制執行操做,並不作任何提醒
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] 爲每一個已存在的目標文件建立備份 -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>。 本軟件是自由軟件:您能夠自由修改和從新發布它。 在法律範圍內沒有其餘保證。 由Mike Parker、David MacKenzie 和Jim Meyering 編寫。
(5)其餘:
用-b作備份時:
-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.txt~2~,以次類推。若是以前沒有以數字編號的文件,則使用下面講到的簡單備份。
4.CONTROL=simple或never:使用簡單備份:在被覆蓋前進行了簡單備份,簡單備份只能有一份,再次被覆蓋時,簡單備份也會被覆蓋。