Linux 命令(文件和目录管理 - mv)
来源:互联网 发布:146总决赛詹姆斯数据 编辑:程序博客网 时间:2024/05/21 12:41
简述
mv 命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆盖。
- 简述
- 命令介绍
- 命令选项
- 使用范例
命令介绍
命令名称
mv命令全称
move基本语法
mv [选项]… [-T] 源文件 目标文件
或:mv [选项]… 源文件… 目录
或:mv [选项]… -t 目录 源文件…功能描述
将源文件重命名为目标文件,或将源文件移动至指定目录。
命令选项
--backup[=CONTROL]
为每个已存在的目标文件创建备份 -b
类似 --backup
,但不接受参数 -f, --force
覆盖前不询问 -i, --interactive
覆盖前询问 -n, --no-clobber
不覆盖已存在文件。如果指定了 -i、-f、-n 中的多个,仅最后一个生效。 --strip-trailing-slashes
去掉每个源文件参数尾部的斜线 -S, --suffix=SUFFIX
替换常用的备份文件后缀 -t, --target-directory=DIRECTORY
指定源文件要移动到目标目录 -T, --no-target-directory
将目标文件视作普通文件处理 -u, --update
当目标文件不存在,或者源文件比目标文件新时,才执行移动操作。 -v, --verbose
详细显示进行的步骤 -Z, --context
显示文件除组信息外的详细信息 --help
在标准输出上显示帮助信息并退出 --version
在标准输出上显示版本信息并退出使用范例
1.移动文件
注意:文件的源地址和目标地址必须不同。
[wang@localhost linux]$ lshello.sh linux-program.pdf# 将 hello.sh 从当前目录移动至 /home/wang/script/[wang@localhost linux]$ mv hello.sh /home/wang/script/[wang@localhost linux]$ lslinux-program.pdf[wang@localhost linux]$ ls /home/wang/script/hello.sh
正如所见,当移动 hello.sh 文件时,先前目录的文件就被删除了。
2.移动多个文件
如果想一次移动多个文件,可以将他们放在一行并用空格分开。
[wang@localhost c]$ mv hello.c qt5_cadaques.pdf /home/wang/script/[wang@localhost c]$ ls /home/wang/script/hello.c hello.sh qt5_cadaques.pdf
如果文件有规律可循的话,那么可以使用通配符。比如,为了移除所有以 .sh 为扩展名的文件,可以用下面的命令:
[wang@localhost c]$ mv *.sh /home/wang/script/
3.移动目录
[wang@localhost doc]$ tree /home/wang/script//home/wang/script/├── hello.c├── hello.sh└── qt5_cadaques.pdf0 directories, 3 files[wang@localhost doc]$ tree linux/linux/└── linux-program.pdf0 directories, 1 file[wang@localhost doc]$ mv linux/ /home/wang/script/[wang@localhost doc]$ tree /home/wang/script//home/wang/script/├── hello.c├── hello.sh├── linux│ └── linux-program.pdf└── qt5_cadaques.pdf1 directory, 4 files
4.重命名文件或目录
也可以用 mv 命令来重命名文件或目录,不过目标位置和源位置必须相同,文件名必须不同。
[wang@localhost c]$ lsQmlBook-In-Chinese.pdf qwt-6.1.1.pdf[wang@localhost c]$ mv qwt-6.1.1.pdf qwt.pdf[wang@localhost c]$ lsQmlBook-In-Chinese.pdf qwt.pdf
5.打印详细信息
当移动或重命名一大堆文件或目录时,可能会想在不去目标位置去查看的情况下知道自己的命令是否成功地执行了,这就要用到 -v
选项了。
[wang@localhost c++]$ mv -v qwt.pdf /home/wang/script/"qwt.pdf" -> "/home/wang/script/qwt.pdf"
6.使用交互模式
当移动文件时,目标位置恰好有同名文件,这时 mv 命令会覆盖掉原来的文件。如果想产生一个关于覆盖文件的提示,可以使用 -i
选项。
[wang@localhost doc]$ mv -i hello.sh /home/wang/script/mv:是否覆盖"/home/wang/script/hello.sh"? y[wang@localhost doc]$
该提示会告诉我们目标位置处是否有同名文件。如果按 y 键,那么该文件将会被删除,否则不会。
7.使用更新选项
选项 -u
,当目标文件不存在,或者源文件比目标文件新时,才执行移动操作。
[wang@localhost doc]$ ls -l hello.sh -rw-rw-r--. 1 wang wang 0 10月 19 13:08 hello.sh[wang@localhost doc]$ ls -l /home/wang/script/hello.sh-rw-rw-r--. 1 wang wang 0 10月 19 13:09 /home/wang/script/hello.sh[wang@localhost doc]$ mv -uv hello.sh /home/wang/script/[wang@localhost doc]$
可以看到文件没有被移动,是因为 hello.sh 最近的修改时间戳比 /home/wang/script/hello.sh 的旧。
8.不覆盖任何已存在的文件
选项 -i
会询问是否要覆盖文件,选项 -n
则不会允许覆盖任何已存在的文件。
[wang@localhost doc]$ mv -vn hello.sh /home/wang/script/[wang@localhost doc]$ ls *.shhello.sh
加上 -v
选项,可以看到文件没有被移动到了目标目录下,使用 ls 再次验证,原文件仍然存在。
9.复制时创建备份
默认情况下,移动文件将会覆盖已存在的目标文件。但是如果移动错了文件,而目标文件已经被新的文件覆盖了,这时应该怎么办才好呢?可以用 -b
选项,该选项会在新文件覆盖旧文件时将旧文件做备份。
[wang@localhost doc]$ mv -bv hello.sh /home/wang/script/"hello.sh" -> "/home/wang/script/hello.sh" (备份:"/home/wang/script/hello.sh~")[wang@localhost doc]$ ls -l /home/wang/script/hello.sh*-rw-rw-r--. 1 wang wang 0 10月 19 12:52 /home/wang/script/hello.sh-rw-rw-r--. 1 wang wang 0 10月 19 12:45 /home/wang/script/hello.sh~
目标目录下出现了名为 hello.sh 和 hello.sh~ 的文件,波浪符号(~)意味着这些文件是备份文件。从属性中可以看到,备份文件比 hello.sh 要旧。
10.无条件覆盖已经存在的文件
当希望无论如何都覆盖已经存在的文件或目录时,可以使用 -f
选项。使用此参数时,必须知道你在做什么。
[wang@localhost doc]$ mv -fv hello.sh /home/wang/script/"hello.sh" -> "/home/wang/script/hello.sh"
- Linux 命令(文件和目录管理 - mv)
- 一天一linux命令(9) cp rm mv文件和目录的管理
- 【Linux文件目录命令】mv命令
- 【Linux命令-文件管理类】mv命令
- Linux文件目录操作命令 mv
- Linux命令:cp 命令--创建文件和目录的副本 mv命令--移动或重命名文件/目录
- linux命令学习-文件目录之mv命令
- Linux文件和目录管理相关命令
- linux 文件和目录管理--基础命令
- linux管理文件和目录的命令
- Linux 命令(文件和目录管理
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- Linux命令 find和mv的结合使用:查找文件,移动到某个目录
- Linux文件与目录管理 - ls, cp, mv
- Linux 命令(文件和目录管理 - pwd)
- Linux 命令(文件和目录管理 - cd)
- Linux 命令(文件和目录管理 - ls)
- apostophe
- 利用http缓存数据
- 读过的书
- 突然好想写博客!
- Python量化交易平台开发教程系列2-类CTP交易API的Python封装设计
- Linux 命令(文件和目录管理 - mv)
- 在Ubuntu中下载、编译Android源码
- 3.1 android控件的用法
- Android 解决字体随系统调节而变化的问题
- 深入理解javascript原型和闭包(4)——隐式原型
- jQuery插件之ajaxFileUpload
- android开发之神兵利器-androidstudio
- spring官网下载spring的jar包、源码包、文档
- Python量化交易平台开发教程系列3-vn.py项目中API封装的编译