用来移动文件(或目录)或修改文件名(目录名);
命令格式 - mv [选项] 源文件或目录 目标文件或目录
命令功能 - 视 mv 命令中第二个参数类型的不同(是目标文件还是目标目录),mv 命令将文件重命名或将其移至一个新目录中;
命令参数 -
-b :若需覆盖文件,则覆盖前先行备份;
-f :若目标文件已存在时,不会 询问而直接覆盖;
-i :若目标文件已经存在时,会 询问是否覆盖;
-u :若目标文件已经存在,且 source 比较新,才会更新;
-t :—target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定 mv 的目标目录,用于移动多个源文件到一个目录的情况;此时目标目录在前,源文件在后;
命令实例 -
实例一:文件改名
命令
xxxxxxxxxx
11mv log1.log log2.log
输出
x
1Qs-MacBook-Pro:dir3 qiu$ ll
2total 8
3-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log1.log
4Qs-MacBook-Pro:dir3 qiu$ mv log1.log log2.log
5Qs-MacBook-Pro:dir3 qiu$ ll
6total 8
7-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
实例二:移动文件
命令
xxxxxxxxxx
11mv log2.log test1
输出
xxxxxxxxxx
151Qs-MacBook-Pro:t1 qiu$ ls -lR
2total 8
3-rw-r--r--@ 1 qiu staff 14 4 10 09:50 log2.log
4drwxr-xr-x 3 qiu staff 96 4 10 09:56 test1
5
6./test1:
7Qs-MacBook-Pro:t1 qiu$ mv log2.log test1
8Qs-MacBook-Pro:t1 qiu$ ls -lR
9total 0
10drwxr-xr-x 4 qiu staff 128 4 10 09:57 test1
11
12./test1:
13total 8
14-rw-r--r--@ 1 qiu staff 14 4 10 09:50 log2.log
15Qs-MacBook-Pro:t1 qiu$
实例三:将文件 log1.log log2.log log3.log 移动到目录 test2 中
命令
xxxxxxxxxx
11mv log1.log log2.log log3.log test2
xxxxxxxxxx
11mv -t test3/ log1.log log2.log log3.log
输出
xxxxxxxxxx
171Qs-MacBook-Pro:t1 qiu$ ll
2total 24
3-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log1.log
4-rw-r--r--@ 1 qiu staff 14 4 10 09:50 log2.log
5-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
6drwxr-xr-x 3 qiu staff 96 4 10 10:02 test1
7Qs-MacBook-Pro:t1 qiu$ mv log1.log log2.log log3.log test1
8Qs-MacBook-Pro:t1 qiu$ ll
9total 0
10drwxr-xr-x 6 qiu staff 192 4 10 10:04 test1
11Qs-MacBook-Pro:t1 qiu$ cd test1/
12Qs-MacBook-Pro:test1 qiu$ ll
13total 24
14-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log1.log
15-rw-r--r--@ 1 qiu staff 14 4 10 09:50 log2.log
16-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
17Qs-MacBook-Pro:test1 qiu$
xxxxxxxxxx
161// Mac_Terminal 下 该方法无效 -t参数无效
2[root@localhost test3]# ll
3总计 20
4-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt
5-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt
6-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt
7drwxr-xr-x 2 root root 4096 10-28 06:21 logs
8-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
9[root@localhost test3]# mv -t /opt/soft/test/test4/ log1.txt log2.txt log3.txt
10[root@localhost test3]# cd ..
11[root@localhost test]# cd test4/
12[root@localhost test4]# ll
13总计 12
14-rw-r--r-- 1 root root 8 10-28 06:15 log1.txt
15-rw-r--r-- 1 root root 12 10-28 06:15 log2.txt
16-rw-r--r-- 1 root root 13 10-28 06:16 log3.txt
实例四:将文件 file1 改名为 file2,如果 file2 已经存在,则询问是否覆盖
命令
xxxxxxxxxx
11mv -i log1.log log2.log
输出
xxxxxxxxxx
131Qs-MacBook-Pro:t1 qiu$ ll
2total 24
3-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log1.log
4-rw-r--r--@ 1 qiu staff 14 4 10 09:50 log2.log
5-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
6drwxr-xr-x 3 qiu staff 96 4 10 10:05 test1
7Qs-MacBook-Pro:t1 qiu$ mv -i log1.log log2.log
8overwrite log2.log? (y/n [n]) y
9Qs-MacBook-Pro:t1 qiu$ ll
10total 16
11-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
12-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
13drwxr-xr-x 3 qiu staff 96 4 10 10:05 test1
实例五:将文件 file1 改名为 file2,即使file2 存在,也直接覆盖掉
命令
xxxxxxxxxx
11mv -f log1.log log2.log
输出
xxxxxxxxxx
121Qs-MacBook-Pro:t1 qiu$ ll
2total 24
3-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log1.log
4-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
5-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
6drwxr-xr-x 3 qiu staff 96 4 10 10:05 test1
7Qs-MacBook-Pro:t1 qiu$ mv -f log1.log log2.log
8Qs-MacBook-Pro:t1 qiu$ ll
9total 16
10-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
11-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
12drwxr-xr-x 3 qiu staff 96 4 10 10:05 test1
实例六:目录的移动
命令
注意:dir2 已经存在
xxxxxxxxxx
11mv dir1 dir2
输出
xxxxxxxxxx
121Qs-MacBook-Pro:test1 qiu$ ll
2total 0
3drwxr-xr-x 2 qiu staff 64 4 10 10:17 dir1
4drwxr-xr-x 2 qiu staff 64 4 10 10:17 dir2
5Qs-MacBook-Pro:test1 qiu$ mv dir1 dir2
6Qs-MacBook-Pro:test1 qiu$ ll
7total 0
8drwxr-xr-x 3 qiu staff 96 4 10 10:18 dir2
9Qs-MacBook-Pro:test1 qiu$ cd dir2/
10Qs-MacBook-Pro:dir2 qiu$ ll
11total 0
12drwxr-xr-x 2 qiu staff 64 4 10 10:17 dir1
实例七:目录的改名
命令
注意:dir2 不存在
xxxxxxxxxx
11mv dir1 dir2
输出
xxxxxxxxxx
71Qs-MacBook-Pro:dir2 qiu$ ll
2total 0
3drwxr-xr-x 2 qiu staff 64 4 10 10:17 dir1
4Qs-MacBook-Pro:dir2 qiu$ mv dir1 dir2
5Qs-MacBook-Pro:dir2 qiu$ ll
6total 0
7drwxr-xr-x 2 qiu staff 64 4 10 10:17 dir2
实例八:移动当前文件夹下的所有文件到上一级目录
命令
xxxxxxxxxx
11mv * ../
输出
xxxxxxxxxx
141Qs-MacBook-Pro:dir2 qiu$ ll
2total 16
3drwxr-xr-x 2 qiu staff 64 4 10 10:24 dir3
4-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
5-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
6Qs-MacBook-Pro:dir2 qiu$ mv * ../
7Qs-MacBook-Pro:dir2 qiu$ ll
8Qs-MacBook-Pro:dir2 qiu$ cd ../
9Qs-MacBook-Pro:dir1 qiu$ ll
10total 16
11drwxr-xr-x 2 qiu staff 64 4 10 10:26 dir2
12drwxr-xr-x 2 qiu staff 64 4 10 10:24 dir3
13-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
14-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
实例九:将当前目录的一个子目录中的文件移动到另一个子目录中
命令
xxxxxxxxxx
11mv dir2/* dir3
输出
xxxxxxxxxx
231Qs-MacBook-Pro:dir1 qiu$ ls -lR
2total 0
3drwxr-xr-x 5 qiu staff 160 4 10 10:27 dir2
4drwxr-xr-x 2 qiu staff 64 4 10 10:24 dir3
5
6./dir2:
7total 16
8-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
9-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
10
11./dir3:
12Qs-MacBook-Pro:dir1 qiu$ mv dir2/* dir3
13Qs-MacBook-Pro:dir1 qiu$ ls -lR
14total 0
15drwxr-xr-x 3 qiu staff 96 4 10 10:30 dir2
16drwxr-xr-x 4 qiu staff 128 4 10 10:30 dir3
17
18./dir2:
19
20./dir3:
21total 16
22-rw-r--r--@ 1 qiu staff 9 4 10 10:03 log2.log
23-rw-r--r--@ 1 qiu staff 12 4 10 10:03 log3.log
实例十:文件被覆盖前做简单备份,前面加参数 -b
命令
xxxxxxxxxx
11mv log1.txt -b log2.txt
输出
xxxxxxxxxx
131// Mac_Terminal 下该方法无效,-b参数无效
2[root@localhost test5]# ll
3-rw-r--r-- 1 root root 25 10-28 07:02 log1.txt
4-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt
5-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
6drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1
7[root@localhost test5]# mv log1.txt -b log2.txt
8mv:是否覆盖“log2.txt”? y
9[root@localhost test5]# ll
10-rw-r--r-- 1 root root 25 10-28 07:02 log2.txt
11-rw-r--r-- 1 root root 13 10-28 06:16 log2.txt~
12-rw-r--r-- 1 root root 29 10-28 06:05 test1.txt
13drwxr-xr-x 2 root root 4096 10-25 17:56 test5-1
说明
-b 不接收参数,mv 会去读取环境变量 VERSION_CONTROL 来作为备份策略;
共有四种备份策略: