Linux系统中的mv令是一个常用的基本命令,它的主要功能是对文件或目录重命名,或者移除目录。下面由学习啦小编为大家整理了linux中的mv命令的相关知识,希望对大家有帮助!

linux中的mv令详解

mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。

注意事项:mv与cp的结果不同,mv好像文件“搬家”,文件个数并未增加。而cp对文件进行复制,文件个数增加了。

(1)用法:

用法: mv [选项]... [-T] 源文件 目标文件

或: mv [选项]... 源文件... 目录

或: mv [选项]... -t 目录 源文件...

(2)功能:

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

(3)选项参数:

1) -b: 当文件存在时,覆盖前,为其创建一个备份

2) -f 若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目录

3) -i 交互式操作,覆盖前先行询问用户,如果源文件与目标文件或目标目录中的文件同名,则询问用户是否覆盖目标文件。这样可以避免误将文件覆盖。

4) -f -force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖

5) -u 若目标文件已经存在,且 source 比较新,才会更新(update)

linux中的mv命令实例

1)[sunjimeng@localhost Document]$ mv text1 mytext 由于此处源文件test1与目标文件是在同一目录下,可以看作仅仅是改了文件的名字

[sunjimeng@localhost Document]$ ll //目录下为空

总用量 0

[sunjimeng@localhost Document]$ cat >text1 <

> I am MenAngel

> PWD=$(pwd)

> I am testing the order of mv!

> EOF

[sunjimeng@localhost Document]$ ll

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 text1

[sunjimeng@localhost Document]$ mv text1 mytext //执行mv命令

[sunjimeng@localhost Document]$ cat mytext

I am MenAngel

PWD=/home/sunjimeng/Document

I am testing the order of mv!

[sunjimeng@localhost Document]$ ll //可见已经改名

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext

[sunjimeng@localhost Document]$

2)[sunjimeng@localhost Document]$ mv mytext{,.txt} 与[sunjimeng@localhost Document]$ mv text text.txt 给文件名增加后缀

[sunjimeng@localhost Document]$ mv mytext{,.txt} //增加后缀名的原始方法

[sunjimeng@localhost Document]$ ll

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

[sunjimeng@localhost Document]$ touch text

[sunjimeng@localhost Document]$ ll

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

[sunjimeng@localhost Document]$ mv text text.txt //利用mv的改名目录增加后缀

[sunjimeng@localhost Document]$ ll

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

3)[root@localhost Documents]# mv ../Document/* . 将文件从源目录移动到目标目录,这里源目录和目标目录可以任意指定。.代表当前目录

[sunjimeng@localhost Document]$ ll //Document下游两个文件

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

[sunjimeng@localhost Document]$ cd ../Documents //进入同级兄弟目录Documents,发现其下为空

[sunjimeng@localhost Documents]$ ll

总用量 0

[sunjimeng@localhost Documents]$ mv ../Document/* . //将Document下的所有文件(*),移动到当前目录(.)。

mv: 无法将"../Document/mytext.txt" 移动至"./mytext.txt": 权限不够 //Linux用组名和用户名来管理文件,此时当前用户没有权限移动文件,必须改为root用户

mv: 无法将"../Document/text.txt" 移动至"./text.txt": 权限不够

[sunjimeng@localhost Documents]$ su root

密码:

ABRT 已检测到 '1' 个问题。预了解详细信息请执行:abrt-cli list --since 1462423345

[root@localhost Documents]# mv ../Document/* .

[root@localhost Documents]# ll //移动完成

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

[root@localhost Documents]# ls -l ../Document //查看Document目录已经没有任何东西

总用量 0

4)[root@localhost Documents]# mv -t ../Document ./* 功能同(3),但区别是源文件的路径和目标路径的位置发生了变化

[root@localhost Documents]# ll

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

[root@localhost Documents]# mv -t ./* ../Document //-t参数的功能就是让他们的位置发生变化,这里第一个参数是目标路径

mv: 目标"./mytext.txt" 不是目录

[root@localhost Documents]# mv -t ../Document ./* //位置调换一下就行了

[root@localhost Documents]# ll

总用量 0

[root@localhost Documents]# ll

总用量 0

[root@localhost Documents]# ls -l ../Document

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

5)[root@localhost Document]# mv mytext.txt mytext 如果第二个参数不是目录名,才将源文件改名,否则,移动源文件到该目录下(与实例1作比较)

[root@localhost Document]# mkdir mytext

[root@localhost Document]# ll

总用量 4

drwxr-xr-x. 2 root root 6 5月 5 22:34 mytext

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

[root@localhost Document]# mv mytext.txt mytext //与实例一不同的是,这里mytext是个目录

[root@localhost Document]# ll

总用量 0

drwxr-xr-x. 2 root root 23 5月 5 22:35 mytext

-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text

[root@localhost Document]# ls -l mytext

总用量 4

-rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt

6)[root@localhost Document]# mv -b myword text 源文件和目标文件都是存在的,因此会有覆盖提醒,-b用于在覆盖时备份文件

[root@localhost Document]# cat >myword <

> this is my word!

> EOF

[root@localhost Document]# cat >text <

> this is my text!

> EOF

[root@localhost Document]# mv -b myword text //在一个文件即将覆盖另一个文件时,默认是提醒的,所以加上-i参数和不加是一样的

mv:是否覆盖"text"? y

[root@localhost Document]# cat myword

cat: myword: 没有那个文件或目录

[root@localhost Document]# cat text

this is my word!

[root@localhost Document]# ll

总用量 8

drwxr-xr-x. 2 root root 23 5月 5 22:35 mytext //这里text里存的是前面myword的内容,text的内容备份到text~中,需要特殊软件才能查看

-rw-r--r--. 1 root root 17 5月 5 22:41 text

-rw-rw-r--. 1 sunjimeng sunjimeng 17 5月 5 22:41 text~

7) [root@localhost text]# mv * ../ 将当前目录下的所有内容移动到父级目录(特殊情况)

[root@localhost Document]# mkdir text

[root@localhost Document]# touch ./text/{text1,text2,text3}

[root@localhost Document]# cd text

[root@localhost text]# mv * ../

[root@localhost text]# cd ../

[root@localhost Document]# ll

总用量 0

drwxr-xr-x. 2 root root 6 5月 5 22:57 text

-rw-r--r--. 1 root root 0 5月 5 22:57 text1

-rw-r--r--. 1 root root 0 5月 5 22:57 text2

-rw-r--r--. 1 root root 0 5月 5 22:57 text3

8)[root@localhost Document]# mv -f text2 text3 强制执行操作,并不做任何提醒

9)[root@localhost Document]# mv -i text2 text3 加不加-i在覆盖时都会提醒

[root@localhost Document]# ll

总用量 0

drwxr-xr-x. 2 root root 6 5月 5 23:05 text

-rw-r--r--. 1 root root 0 5月 5 22:57 text2

-rw-r--r--. 1 root root 0 5月 5 22:57 text3

-rw-r--r--. 1 root root 0 5月 5 22:57 text4

[root@localhost Document]# mv text2 text3

mv:是否覆盖"text3"? n

[root@localhost Document]# mv -i text2 text3

mv:是否覆盖"text3"? n

[root@localhost Document]# mv -f text2 text3

[root@localhost Document]# ll

总用量 0

drwxr-xr-x. 2 root root 6 5月 5 23:05 text

-rw-r--r--. 1 root root 0 5月 5 22:57 text3

-rw-r--r--. 1 root root 0 5月 5 22:57 text4

10)[root@localhost Document]# mv Dir text 将Dir目录移动到text目录下(text存在时),如果不存在直接将Dir改名为text

[root@localhost Document]# mkdir testDir

[root@localhost Document]# ll //下面的操作先将文件text3和text4放到textDir目录下

总用量 0

drwxr-xr-x. 2 root root 6 5月 5 23:09 testDir

drwxr-xr-x. 2 root root 6 5月 5 23:05 text

-rw-r--r--. 1 root root 0 5月 5 22:57 text3

-rw-r--r--. 1 root root 0 5月 5 22:57 text4

[root@localhost Document]# mv {text3,text4} ./testDir

[root@localhost Document]# mv testDir Dir //由于Dir不存在,所以testDir改名为Dir

[root@localhost Document]# mv Dir text //由于text是存在的,所以将Dir移到text目录下

[root@localhost Document]# ll

总用量 0

drwxr-xr-x. 3 root root 16 5月 5 23:10 text //下面验证了这一点

[root@localhost Document]# cd text

[root@localhost text]# ll

总用量 0

drwxr-xr-x. 2 root root 30 5月 5 23:09 Dir

[root@localhost text]# cd Dir

[root@localhost Dir]# ll

总用量 0

-rw-r--r--. 1 root root 0 5月 5 22:57 text3

-rw-r--r--. 1 root root 0 5月 5 22:57 text4

11)[root@localhost /]# mv --help

[root@localhost /]# mv --help

用法:mv [选项]... [-T] 源文件 目标文件

或:mv [选项]... 源文件... 目录

或:mv [选项]... -t 目录 源文件...

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.

--backup[=CONTROL] 为每个已存在的目标文件创建备份

-b 类似--backup 但不接受参数

-f, --force 覆盖前不询问

-i, --interactive 覆盖前询问

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

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

--strip-trailing-slashes 去掉每个源文件参数尾部的斜线

-S, --suffix=SUFFIX 替换常用的备份文件后缀

-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY

-T, --no-target-directory treat DEST as a normal file

-u, --update move only when the SOURCE file is newer

than the destination file or when the

destination file is missing

-v, --verbose explain what is being done

-Z, --context set SELinux security context of destination

file to default type

--help 显示此帮助信息并退出

--version 显示版本信息并退出

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.

The version control method may be selected via the --backup option or through

the VERSION_CONTROL environment variable. Here are the values:

none, off 不进行备份(即使使用了--backup 选项)

numbered, t 备份文件加上数字进行排序

existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份

simple, never 永远使用普通方式备份

GNU coreutils online help:

请向 报告mv 的翻译错误

要获取完整文档,请运行:info coreutils 'mv invocation'

12)[root@localhost /]# mv --version

[root@localhost /]# mv --version

mv (GNU coreutils) 8.22

Copyright (C) 2013 Free Software Foundation, Inc.

许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本。

本软件是自由软件:您可以自由修改和重新发布它。

在法律范围内没有其他保证。

由Mike Parker、David MacKenzie 和Jim Meyering 编写。

补充:其他

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

linux命令 mv -v,linux中的mv命令相关推荐

  1. linux mkfs 源码,Linux系统下移植busybox中mkfs.vfat命令

    为了延长磁盘寿命来存储音视频文件,打发在格式化磁盘过程中将簇的大小设置大点.因为存储的音视频文件现对较大,那么将簇大小设置得尽可能大,这样可以增强磁盘读取数据的性能,同时也不会浪费太多空间. 但发现我 ...

  2. linux命令 mv -v,Linux中mv命令的高级用法示例

    前言 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 命令格式: mv [选项] 源文件或 ...

  3. linux nslookup命令安装,在CentOS中安装nslookup命令

    域名查询工具nslookup并不是Win系统的专利,Linux系统中也可以使用,不过要安装,默认没有. 在CentOS中安装nslookup命令: $ sudo yum install bind-ut ...

  4. linux 修改proc目录,Linux_Linux中系统参数修改命令sysctl的使用讲解,sysctl配置与显示在/proc/sys目录 - phpStudy...

    Linux中系统参数修改命令sysctl的使用讲解 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发.IP碎片去除以及源路由检查等.用 ...

  5. linux脚本添加source,shell中的source命令的巧妙用法

    首先,通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录.例如,当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如sourc ...

  6. linux wget返回值_Linux中的Wget命令与示例

    GNU Wget是用于从Web下载文件的命令行实用程序.使用Wget,您可以使用HTTP,HTTPS和FTP协议下载文件. Wget提供了许多选项,使您可以下载多个文件,恢复下载,限制带宽,递归下载, ...

  7. python运行命令_对python中执行DOS命令的3种方法总结

    1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息. import os os.system("ls") 2. ...

  8. python执行shell命令行_python执行命令行:python中执行shell命令行read结果

    +++++++++++++++++++++++++++++ python执行shell命令 1 os.system  (只有这个方法是边执行边输出,其他方法是最后一次性输出) 可以返回运行shell命 ...

  9. qt执行命令行失败_QT中QProcess调用命令行的痛苦经历

    在QT程序中需要将某些目录和文件压缩为一个rar的压缩包,于是想到了在QT中通过QProcess类调用命令行的rar.exe来达到效果,但是没想到QProcess类用起来很麻烦,而且达不到效果,折腾了 ...

最新文章

  1. 解决ViewPager缓存导致不能实时刷新数据
  2. Android 开发 Activity里获取View的宽度和高度 转载
  3. 开发板、Windows、Ubuntu三者互联——韦东山嵌入式Linux学习笔记08
  4. 探索javascript----获得节点计算后样式
  5. 怎么发表博客,还不能显示在自己的博客首页上,这还不如玩单机!
  6. 小鱼便签_同样是写便签,这样更酷
  7. 事务以及@Transcational注解
  8. LeetCode 6034. 数组的三角和
  9. vgc机器人编程1到13题_工业机器人编程与实操-期末试题
  10. 消息中间件kafka与activeMQ、rabbitMQ、zeroMQ、rocketMQ的比较
  11. 解读Scorm(0):标准
  12. 如何制作透明背景的图片
  13. C# 使用Graphics对象的方法绘制粗边图形/圆/椭圆/线段
  14. 人工势场python_ROS及SLAM进阶教程(十一)多机器人编队人工势场法协同避障算法原理及实现...
  15. 有道词典单词本导入到欧路词典单词本
  16. 宏晶STC单片机使用STC-ISP串口烧录失败的原因与解决方法汇总
  17. 国内考勤管理系统做的比较好的几款软件?
  18. centos linux系统后门程序
  19. 中点圆c语言程序,[图形学] 画圆(基于中点算法)
  20. 前瞻:数据科学中的探索性数据分析(DEA)

热门文章

  1. python文本框焦点设置_如何在tkinter输入框中设置焦点
  2. 快速排序(Quick Sort)—挖坑填数法
  3. ov9712雄迈模组分析
  4. 前端如何打安卓apk包及手机在线调试
  5. Sparrow——基础搭建
  6. 谓语动词变化------时态 情态 语态 否定
  7. 让我思潮翻滚的IBM面试内容
  8. windows7 vl版_与你分享 | Windows 7
  9. 基于JavaWeb的仓库管理系统设计与实现(Hibernet、Struts、Mybatis、JSP、Spring、SQLPlus)
  10. 【Halcon】区域分割:background_seg