zoukankan      html  css  js  c++  java
  • 每天一个linux命令:mv(7)

    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
      

    参考

  • 相关阅读:
    .net core Ocelot Consul 实现API网关 服务注册 服务发现 负载均衡
    .net core grpc 实现通信(一)
    Shell脚本
    LNMP学习内容总结①
    2018/12/18学习内容摘要
    2019/12/16学习内容摘要(Vim)
    第一周进度及学习总结
    2019/12/12学习内容摘要(Linux系统用户与用户组管理②)
    2019/12/13学习内容摘要(Linux磁盘管理①)
    2019/12/11学习内容摘要(Linux系统用户与用户组管理①)
  • 原文地址:https://www.cnblogs.com/DiDi516/p/11746790.html
Copyright © 2011-2022 走看看