linux 命令:mv 详解

本文详细介绍了Linux系统中mv命令的用法,包括重命名、移动文件和目录,以及各种选项如-f、-i、-n等的含义。通过示例展示了如何在不同场景下使用mv,如覆盖文件、交互提示、移动到目录等操作,并提到了系统预设的alias提示确认信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用法:mv [OPTION]... [-T] SOURCE DEST
 或:mv [OPTION]... SOURCE... DIRECTORY
 或:mv [OPTION]... -t DIRECTORY SOURCE...
把 SOURCE 重命名为 DEST, 或者把 SOURCE(s) 移动到 DIRECTORY.

      --backup[=CONTROL]            为每个已存在的DEST创建备份
  -b                                类似--backup 但不接受参数
  -f, --force                       覆盖前不询问
  -i, --interactive                 覆盖前询问
  -n, --no-clobber                  不覆盖已存在文件
                                    如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
      --strip-trailing-slashes	    去掉每个 SOURCE 尾部的斜线
  -S, --suffix=SUFFIX		        替换常用的备份文件后缀
  -t, --target-directory=DIRECTORY  移动所有的 SOURCE 的参数到 DIRECTORY
  -T, --no-target-directory         把 DEST 看成普通文件
  -u, --update                      只有当 SOURCE 比 DEST 更新
                                    或 DEST 不存在的情况才执行
  -v, --verbose                     显示过程信息
  -Z, --context                     设置 DEST 的 SeLinux 安全上下文为默认类型
      --help		                显示此帮助信息并退出
      --version		                显示版本信息并退出

如果不使用 --suffix 或 SIMPLE_BACKUP_SUFFIX 指定后缀, 备份文件的后缀是 '~'
在使用 --backup 选项 或 VERSION_CONTROL 环境变量时,版本控制方法有可能会生效
下边是可选值:

  none, off       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

使用示例:

1. mv file1 file2: 如果file2不存在,修改file1名字为file2,如果file2已存在,把file1的内容覆盖file2,删除file1。

# file2不存在的情况
[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    4 12月  1 15:52 file1
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# echo 123 > file1
[root@server dir]# mv file1 file2
[root@server dir]# ll file1
ls: 无法访问file1: 没有那个文件或目录
[root@server dir]# cat file2
123
# file2存在的情况
[root@server dir]# ll
总用量 16
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    4 12月  1 15:52 file1
-rw-r--r-- 1 root root    4 12月  1 15:53 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# cat file1
123
[root@server dir]# cat file2
456
[root@server dir]# mv file1 file2
[root@server dir]# cat file1
cat: file1: 没有那个文件或目录
[root@server dir]# cat file2
123

2. mv -i file1 file2: 操作前提示是否确定操作

[root@server dir]# ll
总用量 16
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    4 12月  1 15:58 file1
-rw-r--r-- 1 root root    4 12月  1 15:52 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# mv -i file1 file2
mv:是否覆盖"file2"? y
[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    4 12月  1 15:58 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip

3. mv file1 file2 dir1: 把file1和file2移动到dir1里:

[root@server dir]# ll
总用量 16
drwxr-xr-x 2 root root 4096 11月 28 15:12 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    4 12月  1 15:59 file1
-rw-r--r-- 1 root root    4 12月  1 15:58 file2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# mv file1 file2 dir1
[root@server dir]# ll
总用量 8
drwxr-xr-x 2 root root 4096 12月  1 15:59 dir1
lrwxrwxrwx 1 root root    4 11月 28 15:13 dir2 -> dir1
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# ll dir1
总用量 8
-rw-r--r-- 1 root root 4 12月  1 15:59 file1
-rw-r--r-- 1 root root 4 12月  1 15:58 file2

4. mv dir1 dir2: 如果dir2不存在,相当于把dir1改名。如果dir2存在,把dir1放到dir2中,相当于下一层目录。

[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 12月  1 15:59 dir1
drwxr-xr-x 2 root root 4096 12月  1 16:02 dir2
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# mv dir1 dir3
[root@server dir]# ll
总用量 12
drwxr-xr-x 2 root root 4096 12月  1 16:02 dir2
drwxr-xr-x 2 root root 4096 12月  1 15:59 dir3
-rw-r--r-- 1 root root    0 11月 28 15:12 file3
-rw-r--r-- 1 root root    0 11月 29 14:41 file4
-rw-r--r-- 1 root root  164 11月 28 16:41 file.zip
[root@server dir]# mv dir3 dir2
[root@server dir]# ll dir2
总用量 4
drwxr-xr-x 2 root root 4096 12月  1 15:59 dir3

5. mv dir1/* dir2/: 把dir1中的文件移动到dir2中(同名文件覆盖):

[root@server dir]# ll dir1
总用量 0
-rw-r--r-- 1 root root 0 12月  1 16:28 test1
-rw-r--r-- 1 root root 0 12月  1 16:28 test2
-rw-r--r-- 1 root root 0 12月  1 16:28 test3
-rw-r--r-- 1 root root 0 12月  1 16:28 test4
-rw-r--r-- 1 root root 0 12月  1 16:28 test5
[root@server dir]# ll dir2
总用量 0
-rw-r--r-- 1 root root 0 12月  1 16:28 test1
-rw-r--r-- 1 root root 0 12月  1 16:28 test2
-rw-r--r-- 1 root root 0 12月  1 16:28 test3
-rw-r--r-- 1 root root 0 12月  1 16:28 test5
-rw-r--r-- 1 root root 0 12月  1 16:28 test6
[root@server dir]# mv -v dir1/* dir2/
"dir1/test1" -> "dir2/test1"
"dir1/test2" -> "dir2/test2"
"dir1/test3" -> "dir2/test3"
"dir1/test4" -> "dir2/test4"
"dir1/test5" -> "dir2/test5"
[root@server dir]# ll dir2
总用量 0
-rw-r--r-- 1 root root 0 12月  1 16:28 test1
-rw-r--r-- 1 root root 0 12月  1 16:28 test2
-rw-r--r-- 1 root root 0 12月  1 16:28 test3
-rw-r--r-- 1 root root 0 12月  1 16:28 test4
-rw-r--r-- 1 root root 0 12月  1 16:28 test5
-rw-r--r-- 1 root root 0 12月  1 16:28 test6

6. mv -v: 显示过程信息:

[root@server dir]# mv -v file1 file2
"file1" -> "file2"
[root@server dir]# mv -v file1 ../
"file1" -> "../file1"

有可能会遇到在使用linux过程中执行mv,默认提示确认信息,这是因为系统已预配置了alias:

[root@server dir]# alias mv
alias mv='mv -i'
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值