Linux命令之mv

mv [选项] … [-T] 源文件 目标文件

mv [选项] … 源文件 … 目标

mv [选项] … -t 目标 源文件 …

说明:将源文件重命名为目标文件,或将源文件移动至指定目录

(1).常用选项

-b 当文件存在时,覆盖前创建一个备份

-f,--force 目标文件存在时,直接覆盖,不询问

-i,--interactive 目标文件存在时,询问用户是否覆盖

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

如果指定了-f,-i,-n中的多个,仅最后一个生效

-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:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

猜你喜欢

转载自www.cnblogs.com/diantong/p/9198172.html