mv

mv命令可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。 在跨文件系统移动文件时,mv先拷贝,再将原有文件删除,而链至该文件的链接也将丢失

格式

mv [选项] 源文件或目录 目标文件或目录

参数选项

参数 备注
-b 若需覆盖文件,则覆盖前先行备份。
-f force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖;
-i 若目标文件 (destination) 已经存在时,就会询问是否覆盖!
-u 若目标文件已经存在,且 source 比较新,才会更新(update)
-t --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。

实例

  • 文件改名

    命令:mv oldNameFile newNameFile

    [root@VM_0_9_centos ~]# mkdir test
    [root@VM_0_9_centos ~]# cd test
    [root@VM_0_9_centos test]# touch oldNameFile
    [root@VM_0_9_centos test]# ls
    oldNameFile
    [root@VM_0_9_centos test]# mv oldNameFile newNameFile
    [root@VM_0_9_centos test]# ls
    newNameFile
    [root@VM_0_9_centos test]#
  • 移动文件

    命令:mv myFile.txt ./temp/myFile.txt

    [root@VM_0_9_centos test]# touch myFile.txt
    [root@VM_0_9_centos test]# tree
    .
    |-- myFile.txt
    `-- temp 1 directory, 1 file [root@VM_0_9_centos test]# mv myFile.txt ./temp/myFile.txt
    [root@VM_0_9_centos test]# tree
    .
    `-- temp
    `-- myFile.txt 1 directory, 1 file
  • 将文件myFile1改名为MyFile2,即使MyFile2存在,也是直接覆盖掉

    命令:mv -f myFile1 myFile2

    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile1
    -rw-r--r-- 1 root root 0 Oct 27 10:06 myFile2
    [root@VM_0_9_centos test]# mv myFile1 myFile2
    mv: overwrite ?.yFile2?. n
    [root@VM_0_9_centos test]# mv -f myFile1 myFile2
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2
    [root@VM_0_9_centos test]#
  • 文件覆盖前做简单备份

    命令: **mv -b myFile1 myFile2 **

    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:14 myFile1
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2
    [root@VM_0_9_centos test]# mv -b myFile1 myFile2
    mv: overwrite ?.yFile2?. y
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:14 myFile2
    -rw-r--r-- 1 root root 0 Oct 27 10:05 myFile2~

    -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.txt2,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。

    4.CONTROL=simple或never:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。

    [root@VM_0_9_centos test]# mv -b --backup=numbered myFile2
    mv: missing destination file operand after ?.yFile2?
    Try 'mv --help' for more information.
    [root@VM_0_9_centos test]# mv -b --backup=numbered myFile1 myFile2
    mv: overwrite ?.yFile2?. y
    [root@VM_0_9_centos test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Oct 27 10:27 myFile2
    -rw-r--r-- 1 root root 0 Oct 27 10:16 myFile2.~1~
    [root@VM_0_9_centos test]#
  • 移动多个文件到一个目录

    命令:mv -t backup/ myFile2~ myFile2.1

    [root@VM_0_9_centos test]# mkdir backup
    [root@VM_0_9_centos test]# ll
    total 4
    drwxr-xr-x 2 root root 4096 Oct 27 10:33 backup
    -rw-r--r-- 1 root root 0 Oct 27 10:27 myFile2
    -rw-r--r-- 1 root root 0 Oct 27 10:16 myFile2~
    -rw-r--r-- 1 root root 0 Oct 27 10:16 myFile2.~1~
    [root@VM_0_9_centos test]# mv -t backup/ myFile2~ myFile2.~1~
    [root@VM_0_9_centos test]# tree
    .
    |-- backup
    | |-- myFile2~
    | `-- myFile2.~1~
    `-- myFile2 1 directory, 3 files

参考

每天一个linux命令:mv(7)的更多相关文章

  1. 每天一个 Linux 命令(21):find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

  2. 每天一个linux命令目录

    出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...

  3. 每天一个 Linux 命令(20):find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  4. 每天一个linux命令(20):find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  5. 每天一个Linux命令

    每天一个Linux命令(1):ls命令 每天一个Linux命令(2):cd命令 每天一个Linux命令(3):pwd命令 每天一个 Linux 命令(4):mkdir 每天一个 Linux 命令(5) ...

  6. 每天一个linux命令-转载

    每天一个linux命令目录 转载自: http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html   开始详细系统的学习linux常用命令 ...

  7. [转]每天一个linux命令目录

    [转]每天一个linux命令目录 http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每 ...

  8. 每天一个Linux命令(20)--find命令之exec

    find 是我们很常用的一个Linux命令,但是我们一般查找出来的额并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是 comm ...

  9. 每天一个Linux命令 (转)

    一. 文件目录操作命令: 1.每天一个linux命令(1):ls命令 2.每天一个linux命令(2):cd命令  3.每天一个linux命令(3):pwd命令 4.每天一个linux命令(4):mk ...

随机推荐

  1. js DOM0级事件和DOM2级事件

    注册事件有两种方式,分别是DOM0级和DOM2级 DOM0级就是通过事件绑定的形式dom元素只能有(绑定)一个事件处理函数,他的特点是同一个元素绑定相同事件, 后面函数会覆盖前面的 绑定: dom.o ...

  2. 进程管理工具uptime,top,htop

    进程管理工具uptime,top,htop 一uptime 显示当前时间,系统已启动的时间.当前上线人数,系统平均负载(1.5.10分钟的平均负载,一般不会超过1) 系统平均负载:指在特定时间间隔内运 ...

  3. UITextfield 允许和禁止编辑

    1.enabled属性 2.resignFirstResponder,设置的时候,如果不起作用,可以延时一会儿,因为键盘升起需要时间. dispatch_after(dispatch_time(DIS ...

  4. POJ 3525 Most Distant Point from the Sea (半平面交)

    Description The main land of Japan called Honshu is an island surrounded by the sea. In such an isla ...

  5. springboot下的多数据源切换

    今天在考虑如果分公司也用系统了,怎么办,是单独的数据库,还是一起使用?所以就想到了切换数据源来实现,但是发现,只是读写分离,还要再改一下,根据用户地域来切换数据源,今天先照着例子做一下. 看了好多文章 ...

  6. 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】

    题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...

  7. Java总结第一期

    神奇的小阳阳阳再度归来,大家一定想我了吧~哦,谢谢,谢谢,谢谢各位的掌声,thank you,thank you@ 第一章: 下面给大家简单介绍Java: Java技术可以应用在几乎所有类型和规模的设 ...

  8. LOJ 2721 「NOI2018」屠龙勇士——扩展中国剩余定理

    题目:https://loj.ac/problem/2721 1.注意别一输入 p[ i ] 就 a[ i ] %= p[ i ] ,因为在 multiset 里找的时候还需要真实值. 2.注意用 m ...

  9. id4用用户名和密码方式控制身份验证

    建议看这个文章的时候先学习一下B站的id4教程以及文章中推荐的事例教程和官方例子: https://www.jianshu.com/p/259ef2256ec5

  10. SQL 交叉连接与内连接

    交叉连接 ,没有任何限制方式的连接. 叫做交叉连接. 碰到一种SQL 的写法. select * from  t1,t2 .     这其实是交叉连接 .   t1  是三条 ,  t2 是两条.  ...