Linux命令之mv

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

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

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

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

(1).常用选项

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

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

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

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

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

-u,--update 源文件比目标文件新,或目标文件不存在时才进行移动

(2).实例

如果第二个参数不是目录,mv才将源文件重命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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给文件加后缀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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将文件从源目录移动到目标目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[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将移动源文件到目标目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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备份目标文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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,如果存在则移动到目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[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:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

posted @   苦逼运维  阅读(381)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示