推薦:天天學一個 Linux 命令(17):chmodcentos
mv 命令用於移動並重命名文件和目錄。或者將文件從一個目錄移動到另外一個目錄中,若是將一個文件移動到一個已經存在的目標文件中,這時目標文件的內容會被此文件內容覆蓋。centos7
若是源爲文件,而目標爲目錄,mv 將進行文件的位置移動。若是源爲目錄,則目標只能是目錄(不能爲文件),mv 將進行目錄的重命令名。spa
mv 命令移動文件時,在目標不一樣的狀況下,會有下面4種不一樣的結果:3d
mv [選項] 源文件或目錄 目標文件或目錄 mv [options] source destination
-b #當文件存在時,覆蓋前,爲其建立一個備份 -f #若是移到的文件或目錄與目標重複,則直接覆蓋 -i #交互式操做,覆蓋前會提示用戶進行確認操做,用戶經過輸入Y/N來確認是否覆蓋 -u #若目標文件已存在,且與需移動的文件同名,只有在源文件比目標文件較新時,纔會更新目標文件 -t #指定mv的目標目錄,此選項使用於移動多個文件到一個目錄的狀況,此時目標文件在前,源文件在後。 -S<後綴>:#爲備份文件指定後綴,而不使用默認的後綴(刪除源文件中的斜槓「/」) -n #不覆蓋任何現有文件 -T #將目標看成普通文件,而不是目錄 -v #詳細輸出命令的執行過程
#移動文件到目標目錄 [root@centos7 testdir]# ll total 0 -rw-r--r-- 1 root root 0 Feb 25 2021 filetest -rw-r--r-- 1 root root 0 Feb 25 2021 testfile [root@centos7 ~]# mv mingongge.txt testdir/ [root@centos7 ~]# ll testdir/ total 0 -rw-r--r-- 1 root root 0 Feb 25 2021 filetest -rw-r--r-- 1 root root 0 Jan 2 08:43 mingongge.txt -rw-r--r-- 1 root root 0 Feb 25 2021 testfile #移動並重命令文件 [root@centos7 testdir]# mv mingongge.txt test/mingongge [root@centos7 testdir]# ll test/ total 0 -rw-r--r-- 1 root root 0 Jan 2 08:50 mingongge
若是目標位置有同名文件,咱們不但願它被覆蓋,能夠加上-n選項。code
[root@centos7 testdir]# ll *.txt dir/*.txt -rw-r--r-- 1 root root 0 Jan 2 08:58 dir/test1.txt -rw-r--r-- 1 root root 0 Jan 2 08:58 dir/test2.txt -rw-r--r-- 1 root root 0 Jan 2 09:03 test1.txt -rw-r--r-- 1 root root 0 Jan 2 08:57 test2.txt -rw-r--r-- 1 root root 0 Jan 2 09:03 test3.txt [root@centos7 testdir]# mv -nv *.txt dir/ ‘test3.txt’ -> ‘dir/test3.txt’ #目標目錄中沒有test3.txt文件,因此移動成功 [root@centos7 testdir]# ll total 0 drwxr-xr-x 2 root root 57 Jan 2 09:04 dir -rw-r--r-- 1 root root 0 Jan 2 09:03 test1.txt -rw-r--r-- 1 root root 0 Jan 2 08:57 test2.txt
備份功能blog
#若是test2.txt存在,它將會被重命令爲test2.txt,原來的文件會被備份 [root@centos7 dir]# cat test1.txt 1 [root@centos7 dir]# cat test2.txt 2 [root@centos7 dir]# mv -b test1.txt test2.txt mv: overwrite ‘test2.txt’? y [root@centos7 dir]# ll total 12 -rw-r--r-- 1 root root 2 Jan 2 09:12 test2.txt -rw-r--r-- 1 root root 2 Jan 2 09:12 test2.txt~ -rw-r--r-- 1 root root 2 Jan 2 09:12 test3.txt [root@centos7 dir]# cat test2.txt 1 [root@centos7 dir]# cat test2.txt~ 2 #若是test3.txt存在,它將會被重命令爲test3.txt.bak [root@centos7 dir]# mv -b --suffix=.bak test2.txt test3.txt mv: overwrite ‘test3.txt’? y [root@centos7 dir]# ll total 12 -rw-r--r-- 1 root root 2 Jan 2 09:12 test2.txt~ -rw-r--r-- 1 root root 2 Jan 2 09:12 test3.txt -rw-r--r-- 1 root root 2 Jan 2 09:12 test3.txt.bak [root@centos7 dir]# cat test3.txt 1 [root@centos7 dir]# cat test3.txt.bak 3