mv 命令是一個與cp類似的命令,但是它并非創建檔案或目錄的復制品/副本,不管你在使用什么版本的Linux系統,mv 都默認安裝在你的Linux系統上了,來看一下 mv 命令在日常操作中的一些例子,
[root@localhost tmp]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup 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 display this help and exit
--version output version information and exit
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 never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups
GNU coreutils online help: http://www.gnu.org/software/coreutils/
For complete documentation, run: info coreutils 'mv invocation'
1.移動檔案
移動檔案時需要注意的是檔案的源地址和目標地址必須不同,這里有個例子,想要將file_1.txt檔案從當前目錄移動到其它目錄,以/home/pungki/為例,語法應該如下:
- $ mv file_1.txt /home/pungki/office

如我們所見,當我們移動 filetxt 檔案時,先前目錄的 file1.txt 就被洗掉了,
2.移動多個檔案
如果想一次移動多個檔案,我們可以將他們放在一行并用空格分開,
- $ mv file_2.txt file_3.txt file_4.txt /home/pungki/office

如果你的檔案有規律可循的話那么你就可以使用通配符,比如,為了移除所有以.txt為擴展名的檔案,我們可以用下面的命令:
- $ mv *.txt /home/pungki/office

3.移動目錄
不同于復制命令,用 mv 命令移動目錄相當直接,移動目錄你可以使用不帶選項的 mv 命令,看下面的截圖就一目了然了,

4.重命名檔案或目錄
我們也用 mv 命令來重命名檔案或目錄,不過目標位置和源位置必須相同才可以,然后檔案名必須不同,
假定我們當前所在目錄為/home/pungki/Documents,而我們想將file*1.txt重命名為file*2.txt,那么命令應該如下:
- $ mv file_1.txt file_2.txt
如果是絕對路徑,它應該像下面這樣:
- $ mv /home/pungki/Documents/file_1.txt /home/pungki/Documents/file_2.txt

5. 重命名目錄
上一段的規則同樣適用于目錄,請看這個例子:
- $ mv directory_1/ directory_2/
6. 列印移動資訊
當你移動或重命名一大堆檔案或目錄時,你可能會想在不去目標位置去查看的情況下知道你自己的命令是否成功地執行了,這就要用到-v選項了,
- $ mv -v *.txt /home/pungki/office

該方法同樣適用于目錄,

7. 使用互動模式
當你將檔案移動到其它位置,而那個位置恰好有同樣的檔案,這時 mv 命令會覆寫掉原來的檔案,對于mv的這一行為一般不會有什么提示,如果想產生一個關于覆寫檔案的提示,我們可以使用-i選項,(譯注:通常發行版會通過alias命令,將-i作為默認選項,所以會有提示,)
假設我們想將 file1.txt 移動到 /home/pungki/office,同時,/home/pungki/office 目錄下已經有file1.txt檔案了,
- $ mv -i file_1.txt /home/pungki/office

這個提示會讓我們知道目標位置處file_1.txt的存在,如果我們按y鍵,那么那個檔案將會被洗掉,否則不會,
8. 使用更新選項
-i 選項會提示我們關于覆寫檔案的提示,而 -u 則只在源檔案比目標檔案新時才執行更新,讓我們看一看下面的例子:

假如 file1.txt 和 file2.txt有如下特點:
- File_1.txt has 84 bytes file size and it last modified time is12:00
- File_2.txt has 0 bytes file size and it last modified time is11:59
我們想將它們移動到 /home/pungki/office 目錄下,**但是目標地址*已經有file1.txt和file2.txt了,
我們用下面的命令將file1.txt 和file2.txt從當前目錄移動到/home/pungki/office
- $ mv -uv *.txt /home/pungki/office
可以看到這些檔案被移動了,能移動這些檔案是因為它們最近的修改時間戳比 /home/pungki/office 目錄中的檔案新,
9.不要覆寫任何已存在的檔案
如果-i選項詢問我們是否要覆寫檔案,那么 -n 選項將不會允許我們覆寫任何已存在的檔案,
繼續使用第8點中的例子,如果我們將-u 換成 -n同時加上-v選項,那么我們會看到沒有任何檔案移動到了 /home/pungki/office 目錄下,
- $ mv -vn *.txt /home/pungki/office

10. 復制時創建備份
默認情況下,移動檔案將會覆寫已存在的目標檔案,但是如果我們移動錯了檔案而目標檔案已經被新的檔案覆寫了,這時應該怎么辦才好呢?有沒有一種方法可以恢復之前的檔案呢?答案是肯定的,我們可以用-b選項,該選項會在新檔案覆寫舊檔案時將舊檔案做備份,這里我們還以第8點為例,
- $ mv -bv *.txt /home/pungki/office

如截圖中所見,在 /home/pungki/office 目錄下出現了名為file*1.txt~ and file*2.txt~ 的檔案,那個波浪符號(~)意味著這些檔案是備份檔案,從它們的屬性中我們可以看到,這些檔案比file1.txt和file2.txt要舊,
11. 無條件覆寫已經存在的檔案
(譯注:這一節是譯者補充的,原文遺漏了這個重要選項)
當你希望無論如何都覆寫已經存在的檔案或目錄時,你可以使用 -f 選項,如果同時指定了 -f 選項和 -i 或 -n 選項,則 -f 選項會覆寫它們——即不進行任何提示而覆寫,所以,在使用此引數時,知道你在做什么,
- $ mv -f *.txt /home/pungki/office
總結
移動檔案和目錄命令是Linux系統的基本命令,通常你可以通過man mv 或者 mv --help顯示mv的手冊頁以了解更多詳細資訊,

本文由博客一文多發平臺 OpenWrite 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/251307.html
標籤:Linux
上一篇:k8s,容器如何快速學習討論
下一篇:linux 安裝命令
