首页 >> 学习 >> Linux常用命令 >> mv
mv命令

本节重要性:★★★★★    本节难度:★★★★☆

Linux下移动文件或给文件改名,用的是同一个命令,mv,即move的缩写。

命令用途

移动文件或重命名文件

命令格式

mv [option] source destination

常用选项

-i	如果目的文件已存在,提示是否覆盖
-n	不覆盖已存在的文件

常见用法

与cp命令类似,mv后面也至少需跟两个参数,根据参数是文件还是目录,mv的用法可以分为几种情况:

1. 两个参数都是文件,并且在同一目录下。这种情况是给文件重命名:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file3  flower  fruit  hello.sh  lily  my_flower  prac
test  test_eof
[peter@ibi98 ~]$ mv file3 file4
[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file4  flower  fruit  hello.sh  lily  my_flower  prac
test  test_eof

2. 两个参数都是文件,文件名不同并且在不同的目录下。这种情况是移动并重命名文件:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file4  flower  fruit  hello.sh  lily  my_flower  prac
test  test_eof
[peter@ibi98 ~]$ ls prac/
at_LEC1_protein.fa  file1  file2  file3  flower  fruit  hello.sh  my_flower
[peter@ibi98 ~]$ mv file4 prac/file5
[peter@ibi98 ~]$ ls prac/
at_LEC1_protein.fa  file1  file2  file3  file5  flower  fruit  hello.sh  my_flower

3. 第一个参数是文件,第二个参数是目录(相当于两个参数都是文件,文件名相同并且在不同的目录下)。这种情况是移动文件:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  flower  fruit  hello.sh  lily  my_flower  prac  test
test_eof
[peter@ibi98 ~]$ ls prac/
at_LEC1_protein.fa  file1  file2  file3  file5  flower  fruit  hello.sh  my_flower
[peter@ibi98 ~]$ mv prac/file5 .
[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file5  flower  fruit  hello.sh  lily  my_flower  prac
test  test_eof
[peter@ibi98 ~]$ ls prac/
at_LEC1_protein.fa  file1  file2  file3  flower  fruit  hello.sh  my_flower

4. 第一个参数是目录,第二个参数是一个不存在的目录。这种情况是重命名目录:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file3  flower  fruit  hello.sh  my_flower
[peter@ibi98 ~]$ mv fruit/ my_fruit/
[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file3  flower  my_fruit  hello.sh  my_flower

5. 第一个参数是目录,第二个参数是已存在的目录。这种情况是将第一个目录移动到第二个目录中:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file5  flower  hello.sh  lily  my_flower  my_fruit  prac
test  test_eof
[peter@ibi98 ~]$ ls flower/
real_lily  rose
[peter@ibi98 ~]$ mv my_flower/ flower/
[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file5  flower  hello.sh  lily  my_fruit  prac  test
test_eof
[peter@ibi98 ~]$ ls flower/
my_flower  real_lily  rose

6. 参数超过两个,最后一个是目录。这种情况就是把多个文件或目录一起移动到一个目录中:

[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  file1  file2  file5  flower  hello.sh  lily  my_fruit  prac  test
test_eof
[peter@ibi98 ~]$ mv file1 file2 file5 test test_eof flower/
[peter@ibi98 ~]$ ls
at_LEC1_protein.fa  flower  hello.sh  lily  my_fruit  prac

用mv移动或重命名文件时,相对路径和绝对路径都是可以的。

<<上一节  下一节>>