linux系列(七):mv命令

一、命令格式:spa

  mv [選項] 源文件或目錄 目標文件或目錄code

二、命令功能:blog

  Linux mv命令用來爲文件或目錄更名、或將文件或目錄移入其它位置。get

三、命令參數:io

-b :若需覆蓋文件,則覆蓋前先行備份。 
-f :force 強制的意思,若是目標文件已經存在,不會詢問而直接覆蓋;
-i :若目標文件 (destination) 已經存在時,就會詢問是否覆蓋!
-u :若目標文件已經存在,且 source 比較新,纔會更新(update)
-t  : --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目標目錄,該選項適用於移動多個源文件到一個目錄的狀況,此時目標目錄在前,源文件在後。

四、簡單實例:class

(1)、文件更名test

命令:date

  mv test.txt ttt.txtfile

輸出:命名

felix@felix-computer:~/test$ ls
test.txt
felix@felix-computer:~/test$ mv test.txt ttt.txt
felix@felix-computer:~/test$ ls
ttt.txt
felix@felix-computer:~/test$ 

(2)、移動文件

命令:

  mv ttt.txt test3

輸出:

felix@felix-computer:~/test$ tree
.
├── test3
└── ttt.txt

1 directory, 1 file
felix@felix-computer:~/test$ mv ttt.txt test3/
felix@felix-computer:~/test$ tree
.
└── test3
    └── ttt.txt

1 directory, 1 file
felix@felix-computer:~/test$ 

(3)、移動多個文件到指定目錄

命令:

  mv -t test4/ test3/*

輸出:

felix@felix-computer:~/test$ tree
.
├── test3
│   ├── 1.txt
│   ├── 2.txt
│   ├── 3.txt
│   ├── 4.txt
│   └── ttt.txt
└── test4

2 directories, 5 files
felix@felix-computer:~/test$ mv -t test4/ test3/*
felix@felix-computer:~/test$ tree
.
├── test3
└── test4
    ├── 1.txt
    ├── 2.txt
    ├── 3.txt
    ├── 4.txt
    └── ttt.txt

2 directories, 5 files
felix@felix-computer:~/test$ 

(4)、將文件1命名爲文件2,若是文件2已存在,詢問是否覆蓋

命令:

  mv -i 3.txt 1.txt

輸出:

felix@felix-computer:~/test/test4$ ls
1.txt  2.txt  3.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ mv -i 3.txt 1.txt 
mv:是否覆蓋'1.txt'? y
felix@felix-computer:~/test/test4$ ls
1.txt  2.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ 

(5)、將文件1命名爲文件2,若是文件2已存在,直接覆蓋

命令:

   mv -f 2.txt 1.txt

輸出:

felix@felix-computer:~/test/test4$ mv -f 2.txt 1.txt 
felix@felix-computer:~/test/test4$ ls
1.txt  4.txt  ttt.txt
felix@felix-computer:~/test/test4$ 

(6)、目錄移動,若是目錄dir2不存在,將目錄dir1更名爲dir2;不然,將dir1移動到dir2中

命令:

  mv test4 test3

輸出:

felix@felix-computer:~/test$ tree
.
├── test3
└── test4
    ├── 1.txt
    ├── 4.txt
    └── ttt.txt

2 directories, 3 files
felix@felix-computer:~/test$ mv test4 test3
felix@felix-computer:~/test$ tree
.
└── test3
    └── test4
        ├── 1.txt
        ├── 4.txt
        └── ttt.txt

2 directories, 3 files
felix@felix-computer:~/test$

(7)、文件被覆蓋前作簡單備份

命令:

  mv 2.txt -b 1.txt

輸出:

felix@felix-computer:~/test/test3/test4$ ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt
felix@felix-computer:~/test/test3/test4$ mv 2.txt -b 1.txt 
felix@felix-computer:~/test/test3/test4$ ls
1.txt  1.txt~  3.txt  4.txt  5.txt  6.txt
felix@felix-computer:~/test/test3/test4$ 
相關文章
相關標籤/搜索