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)

    (4)实例:

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

  1. [sunjimeng@localhost Document]$ ll //目录下为空
  2. 总用量
  3. [sunjimeng@localhost Document]$ cat >text1 <<EOF //新建文件文档并从标准输入中输入数据到文件
  4. > I am MenAngel
  5. > PWD=$(pwd)
  6. > I am testing the order of mv!
  7. > EOF
  8. [sunjimeng@localhost Document]$ ll
  9. 总用量
  10. -rw-rw-r--. sunjimeng sunjimeng 5 : text1
  11. [sunjimeng@localhost Document]$ mv text1 mytext //执行mv命令
  12. [sunjimeng@localhost Document]$ cat mytext
  13. I am MenAngel
  14. PWD=/home/sunjimeng/Document
  15. I am testing the order of mv!
  16. [sunjimeng@localhost Document]$ ll //可见已经改名
  17. 总用量
  18. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext
  19. [sunjimeng@localhost Document]$

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

  1. [sunjimeng@localhost Document]$ mv mytext{,.txt} //增加后缀名的原始方法
  2. [sunjimeng@localhost Document]$ ll
  3. 总用量
  4. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  5. [sunjimeng@localhost Document]$ touch text
  6. [sunjimeng@localhost Document]$ ll
  7. 总用量
  8. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  9. -rw-rw-r--. sunjimeng sunjimeng 5 : text
  10. [sunjimeng@localhost Document]$ mv text text.txt //利用mv的改名目录增加后缀
  11. [sunjimeng@localhost Document]$ ll
  12. 总用量
  13. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  14. -rw-rw-r--. sunjimeng sunjimeng 5 : text.txt

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

  1. [sunjimeng@localhost Document]$ ll //Document下游两个文件
  2. 总用量
  3. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  4. -rw-rw-r--. sunjimeng sunjimeng 5 : text.txt
  5. [sunjimeng@localhost Document]$ cd ../Documents //进入同级兄弟目录Documents,发现其下为空
  6. [sunjimeng@localhost Documents]$ ll
  7. 总用量
  8. [sunjimeng@localhost Documents]$ mv ../Document/* . //将Document下的所有文件(*),移动到当前目录(.)。
  9. mv: 无法将"../Document/mytext.txt" 移动至"./mytext.txt": 权限不够 //Linux用组名和用户名来管理文件,此时当前用户没有权限移动文件,必须改为root用户
  10. mv: 无法将"../Document/text.txt" 移动至"./text.txt": 权限不够
  11. [sunjimeng@localhost Documents]$ su root
  12. 密码:
  13. ABRT 已检测到 '1' 个问题。预了解详细信息请执行:abrt-cli list --since 1462423345
  14. [root@localhost Documents]# mv ../Document/* .
  15. [root@localhost Documents]# ll //移动完成
  16. 总用量 4
  17. -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt
  18. -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt
  19. [root@localhost Documents]# ls -l ../Document //查看Document目录已经没有任何东西
  20. 总用量 0

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

  1. [root@localhost Documents]# ll
  2. 总用量
  3. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  4. -rw-rw-r--. sunjimeng sunjimeng 5 : text.txt
  5. [root@localhost Documents]# mv -t ./* ../Document //-t参数的功能就是让他们的位置发生变化,这里第一个参数是目标路径
  6. mv: 目标"./mytext.txt" 不是目录
  7. [root@localhost Documents]# mv -t ../Document ./* //位置调换一下就行了
  8. [root@localhost Documents]# ll
  9. 总用量 0
  10. [root@localhost Documents]# ll
  11. 总用量 0
  12. [root@localhost Documents]# ls -l ../Document
  13. 总用量 4
  14. -rw-rw-r--. 1 sunjimeng sunjimeng 73 5月 5 22:02 mytext.txt
  15. -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 5 22:08 text.txt

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

  1. [root@localhost Document]# mkdir mytext
  2. [root@localhost Document]# ll
  3. 总用量
  4. drwxr-xr-x. root root 5 : mytext
  5. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt
  6. -rw-rw-r--. sunjimeng sunjimeng 5 : text
  7. [root@localhost Document]# mv mytext.txt mytext //与实例一不同的是,这里mytext是个目录
  8. [root@localhost Document]# ll
  9. 总用量
  10. drwxr-xr-x. root root 5 : mytext
  11. -rw-rw-r--. sunjimeng sunjimeng 5 : text
  12. [root@localhost Document]# ls -l mytext
  13. 总用量
  14. -rw-rw-r--. sunjimeng sunjimeng 5 : mytext.txt 

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

  1. [root@localhost Document]# cat >myword <<EOF
  2. > this is my word!
  3. > EOF
  4. [root@localhost Document]# cat >text <<EOF
  5. > this is my text!
  6. > EOF
  7. [root@localhost Document]# mv -b myword text //在一个文件即将覆盖另一个文件时,默认是提醒的,所以加上-i参数和不加是一样的
  8. mv:是否覆盖"text" y
  9. [root@localhost Document]# cat myword
  10. cat: myword: 没有那个文件或目录
  11. [root@localhost Document]# cat text
  12. this is my word!
  13. [root@localhost Document]# ll
  14. 总用量
  15. drwxr-xr-x. root root 5 : mytext //这里text里存的是前面myword的内容,text的内容备份到text~中,需要特殊软件才能查看
  16. -rw-r--r--. root root 5 : text
  17. -rw-rw-r--. sunjimeng sunjimeng 5 : text~

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

  1. [root@localhost Document]# mkdir text
  2. [root@localhost Document]# touch ./text/{text1,text2,text3}
  3. [root@localhost Document]# cd text
  4. [root@localhost text]# mv * ../
  5. [root@localhost text]# cd ../
  6. [root@localhost Document]# ll
  7. 总用量
  8. drwxr-xr-x. root root 5 : text
  9. -rw-r--r--. root root 5 : text1
  10. -rw-r--r--. root root 5 : text2
  11. -rw-r--r--. root root 5 : text3

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

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

  1. [root@localhost Document]# ll
  2. 总用量
  3. drwxr-xr-x. root root 5 : text
  4. -rw-r--r--. root root 5 : text2
  5. -rw-r--r--. root root 5 : text3
  6. -rw-r--r--. root root 5 : text4
  7. [root@localhost Document]# mv text2 text3
  8. mv:是否覆盖"text3" n
  9. [root@localhost Document]# mv -i text2 text3
  10. mv:是否覆盖"text3" n
  11. [root@localhost Document]# mv -f text2 text3
  12. [root@localhost Document]# ll
  13. 总用量
  14. drwxr-xr-x. root root 5 : text
  15. -rw-r--r--. root root 5 : text3
  16. -rw-r--r--. root root 5 : text4

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

  1. [root@localhost Document]# mkdir testDir
  2. [root@localhost Document]# ll //下面的操作先将文件text3和text4放到textDir目录下
  3. 总用量
  4. drwxr-xr-x. root root 5 : testDir
  5. drwxr-xr-x. root root 5 : text
  6. -rw-r--r--. root root 5 : text3
  7. -rw-r--r--. root root 5 : text4
  8. [root@localhost Document]# mv {text3,text4} ./testDir
  9. [root@localhost Document]# mv testDir Dir //由于Dir不存在,所以testDir改名为Dir
  10. [root@localhost Document]# mv Dir text //由于text是存在的,所以将Dir移到text目录下
  11. [root@localhost Document]# ll
  12. 总用量
  13. drwxr-xr-x. root root 5 : text //下面验证了这一点
  14. [root@localhost Document]# cd text
  15. [root@localhost text]# ll
  16. 总用量
  17. drwxr-xr-x. root root 5 : Dir
  18. [root@localhost text]# cd Dir
  19. [root@localhost Dir]# ll
  20. 总用量
  21. -rw-r--r--. root root 5 : text3
  22. -rw-r--r--. root root 5 : text4

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

  1. [root@localhost /]# mv --help
  2. 用法:mv [选项]... [-T] 源文件 目标文件
  3.  或:mv [选项]... 源文件... 目录
  4.  或:mv [选项]... -t 目录 源文件...
  5. Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
  6.  
  7. Mandatory arguments to long options are mandatory for short options too.
  8. --backup[=CONTROL] 为每个已存在的目标文件创建备份
  9. -b 类似--backup 但不接受参数
  10. -f, --force 覆盖前不询问
  11. -i, --interactive 覆盖前询问
  12. -n, --no-clobber 不覆盖已存在文件
  13. 如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
  14. --strip-trailing-slashes 去掉每个源文件参数尾部的斜线
  15. -S, --suffix=SUFFIX 替换常用的备份文件后缀
  16. -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
  17. -T, --no-target-directory treat DEST as a normal file
  18. -u, --update move only when the SOURCE file is newer
  19. than the destination file or when the
  20. destination file is missing
  21. -v, --verbose explain what is being done
  22. -Z, --context set SELinux security context of destination
  23. file to default type
  24. --help 显示此帮助信息并退出
  25. --version 显示版本信息并退出
  26.  
  27. The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
  28. The version control method may be selected via the --backup option or through
  29. the VERSION_CONTROL environment variable. Here are the values:
  30.  
  31. none, off 不进行备份(即使使用了--backup 选项)
  32. numbered, t 备份文件加上数字进行排序
  33. existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  34. simple, never 永远使用普通方式备份
  35.  
  36. GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
  37. 请向<http://translationproject.org/team/zh_CN.html> 报告mv 的翻译错误
  38. 要获取完整文档,请运行:info coreutils 'mv invocation'

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

  1. [root@localhost /]# mv --version
  2. mv (GNU coreutils) 8.22
  3. Copyright (C) Free Software Foundation, Inc.
  4. 许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
  5. 本软件是自由软件:您可以自由修改和重新发布它。
  6. 在法律范围内没有其他保证。
  7.  
  8. Mike ParkerDavid MacKenzie Jim Meyering 编写。

(5)其他:

用-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命令(9)mv命令的更多相关文章

  1. 小知识点:linux下的mv命令怎么用?

    linux下的mv命令怎么用? mv a.txt b.txt  将a.txt 改名为b.txtmv a.txt /mnt/b.txt    同时更改路径为/mnt/mv a.txt /opt/ftp/ ...

  2. Linux命令学习-mv命令

    Linux中,mv命令的全称是move,主要作用是移动文件或文件夹,类似于Windows下的剪切功能,同时还可以用于修改名字. 假设当前处于wintest用户的主目录,路径为 /home/wintes ...

  3. 每天一个linux命令:mv命令

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

  4. linux常用命令(6)mv命令

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

  5. linux常用命令:mv 命令

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

  6. linux命令:mv命令

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

  7. Linux CentOS7 VMware 环境变量PATH、cp命令、mv命令、文档查看cat/more/less/head/tail——笔记

    一.环境变量PATH PATH一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径. 命令在这几个目录里面就不需要敲绝对路径 echo $PATH 例子:把/tmp/ 加到 $PA ...

  8. linux命令之------Mv命令

    Mv命令 1)作用:用来为文件或目录改名/或将文件或目录一如其他位置 2)-i:若指定目录已有同名文件,则先询问是否覆盖旧文件: 3)-f:在mv操作要覆盖某已有的目标文件时,不给任何指示: 4)案例 ...

  9. 【Linux常见命令】mv命令

    mv - move (rename) files mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法: mv [OPTION]... [-T] SOURCE DEST mv [OPTIO ...

  10. 环境变量PATH、cp命令、mv命令、文档查看cat/more/less/head/tail 各个命令的使用介绍

    第2周第2次课(3月27日) 课程内容: 2.10 环境变量PATH2.11 cp命令2.12 mv命令2.13 文档查看cat/more/less/head/tail 2.10 环境变量PATH P ...

随机推荐

  1. ASP.NET CORE RAZOR :将文件上传至 ASP.NET Core 中的 Razor 页面

    本部分演示使用 Razor 页面上传文件. 本教程中的 Razor 页面 Movie 示例应用使用简单的模型绑定上传文件,非常适合上传小型文件. 有关流式传输大文件的信息,请参阅通过流式传输上传大文件 ...

  2. VB.NET小结

    在满头困惑与不断的摸索中.NET视频终究是看完了,感觉这是迄今为止的视频材料中最令人头疼的一个,漫天的繁体字和标准的台湾术语,真是让人欲罢不能.只是看着看着也就慢慢习惯了,大概可以理解老师在讲什么,可 ...

  3. JavaScript的join()

    JavaScript join() 方法 JavaScript Array 对象 定义和用法 join() 方法用于把数组中的所有元素放入一个字符串. 元素是通过指定的分隔符进行分隔的. 语法 arr ...

  4. 【数据结构】29、hashmap=》tableSizeFor 中求大于等于当前数的最小2的幂

    最近面试被问到hashmap的实现,因为前段时间刚好看过源码,显得有点信心满满,但是一顿操作下来的结论是基础不够扎实... 好吧,因为我开始看hashmap是想了解这到底是一个什么样的机制,具体有啥作 ...

  5. CentOS下使用yum快速安装memcached

    1. 查找Memcached yum search memcached 首先检查yum软件仓库中是否存在memcached,如果有 直接进入第3步安装即可,否则执行第2步. 2. 安装第三方软件库(可 ...

  6. 调用bat文件执行java文件

    set path=./jre7/bin--设置jre路径,可以写jre的全路径java -cp "lib/*;" -Xms256m -Xmx512m com.shentong.Ma ...

  7. mysql使用存储过程制造测试数据

    DELIMITER $$ DROP PROCEDURE IF EXISTS message_insert_procedure; CREATE PROCEDURE `test`.`message_ins ...

  8. 读取Excel中的数据到DataSet

    读取Excel中的数据到DataSet 1.引用命名空间 using System.Data.OleDb; 2.输入Excel文件,输出DataSet public DataSet ExecleDs( ...

  9. SPSS统计功能与模块对照表

    SPSS统计功能 - 应用速查表第一列为统计方法,中间为统计功能,最后一列为所在模块 1 ANOVA Models(单因素方差分析:简单因子) : 摘要 描述 方差 轮廓 - SPSS Base 2 ...

  10. SVN版本号控制软件-图片含义具体解释

    转载请注明出处:http://blog.csdn.net/zhuwentao2150/article/details/51195154 自己定义SVN图标显示风格 SVN的图标是能够自己定义风格的 右 ...