Linux命令之mv

mv [選項] … [-T] 源文件 目標文件this

mv [選項] … 源文件 … 目標blog

mv [選項] … -t 目標 源文件 …class

說明:將源文件重命名爲目標文件,或將源文件移動至指定目錄test

(1).經常使用選項變量

-b 當文件存在時,覆蓋前建立一個備份date

-f,--force 目標文件存在時,直接覆蓋,不詢問im

-i,--interactive 目標文件存在時,詢問用戶是否覆蓋命名

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

若是指定了-f,-i,-n中的多個,僅最後一個生效word

-u,--update 源文件比目標文件新,或目標文件不存在時才進行移動

(2).實例

若是第二個參數不是目錄,mv纔將源文件重命名

[root@CentOS6 桌面]# ll
總用量 0
[root@CentOS6 桌面]# cat >text1<<EOF
> I am MenAngel
> PWD=$(pwd)
> I am testing the order of mv!
> EOF
[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 text1
[root@CentOS6 桌面]# mv text1 mytext
[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext

使用mv給文件加後綴

[root@CentOS6 桌面]# mv mytext{,.txt}
[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
[root@CentOS6 桌面]# touch text
[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root  0 6月  14 19:59 text
[root@CentOS6 桌面]# mv text text.txt
[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root  0 6月  14 19:59 text.txt

 使用mv將文件從源目錄移動到目標目錄

[root@CentOS6 桌面]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root  0 6月  14 19:59 text.txt
[root@CentOS6 桌面]# cd ../公共的
[root@CentOS6 公共的]# ll
總用量 0
[root@CentOS6 公共的]# mv ../桌面/* .
[root@CentOS6 公共的]# ll
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root  0 6月  14 19:59 text.txt
[root@CentOS6 公共的]# ls -l ../桌面
總用量 0
[root@CentOS6 公共的]# mv -t ../桌面 ./*    //注意這裏是用法中的第三種,目標在前,源文件在後
[root@CentOS6 公共的]# ll
總用量 0
[root@CentOS6 公共的]# ls -l ../桌面
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root  0 6月  14 19:59 text.txt

若是第二個參數是目錄,mv將移動源文件到目標目錄下

[root@CentOS6 桌面]# mkdir mytext
[root@CentOS6 桌面]# ll
總用量 8
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
-rw-r--r--. 1 root root   61 6月  14 19:39 mytext.txt
-rw-r--r--. 1 root root    0 6月  14 19:59 text.txt
[root@CentOS6 桌面]# mv mytext.txt mytext
[root@CentOS6 桌面]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
-rw-r--r--. 1 root root    0 6月  14 19:59 text.txt
[root@CentOS6 桌面]# ls -l mytext
總用量 4
-rw-r--r--. 1 root root 61 6月  14 19:39 mytext.txt

 若是目標文件存在時,使用-b備份目標文件

[root@CentOS6 桌面]# cat >myword <<EOF
> this is my word!
> EOF
[root@CentOS6 桌面]# cat >text <<EOF
> this is my text!
> EOF
[root@CentOS6 桌面]# mv -b myword text
mv:是否覆蓋"text"? y
[root@CentOS6 桌面]# cat myword    //移動後myword文件已經不存在了
cat: myword: 沒有那個文件或目錄
[root@CentOS6 桌面]# cat text    //text的內容變成myword的內容
this is my word!
[root@CentOS6 桌面]# ll
總用量 12
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
-rw-r--r--. 1 root root   17 6月  14 20:24 text
-rw-r--r--. 1 root root   17 6月  14 20:24 text~
-rw-r--r--. 1 root root    0 6月  14 19:59 text.txt

 將Dir目錄移動到myDir目錄下,若是不存在則更名爲myDir,若是存在則移動到目錄下

[root@CentOS6 桌面]# ll
總用量 12
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
-rw-r--r--. 1 root root   17 6月  14 20:24 text
-rw-r--r--. 1 root root   17 6月  14 20:24 text~
-rw-r--r--. 1 root root    0 6月  14 19:59 text.txt
[root@CentOS6 桌面]# mkdir Dir
[root@CentOS6 桌面]# ll
總用量 16
drwxr-xr-x. 2 root root 4096 6月  14 20:32 Dir
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
-rw-r--r--. 1 root root   17 6月  14 20:24 text
-rw-r--r--. 1 root root   17 6月  14 20:24 text~
-rw-r--r--. 1 root root    0 6月  14 19:59 text.txt
[root@CentOS6 桌面]# mv {text,text~,text.txt} Dir
[root@CentOS6 桌面]# ll
總用量 8
drwxr-xr-x. 2 root root 4096 6月  14 20:33 Dir
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
[root@CentOS6 桌面]# mv Dir myDir    //不存在myDir,因此更名爲myDir
[root@CentOS6 桌面]# ll
總用量 8
drwxr-xr-x. 2 root root 4096 6月  14 20:33 myDir
drwxr-xr-x. 2 root root 4096 6月  14 20:21 mytext
[root@CentOS6 桌面]# mv myDir mytext    //存在mytext,因此移動到mytest目錄下
[root@CentOS6 桌面]# ll
總用量 4
drwxr-xr-x. 3 root root 4096 6月  14 20:34 mytext
[root@CentOS6 桌面]# ls -l mytext
總用量 8
drwxr-xr-x. 2 root root 4096 6月  14 20:33 myDir
-rw-r--r--. 1 root root   61 6月  14 19:39 mytext.txt

(3).其餘

用-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:使用簡單備份:在被覆蓋前進行了簡單備份,簡單備份只能有一份,再次被覆蓋時,簡單備份也會被覆蓋。

相關文章
相關標籤/搜索