【Linux】一步一步学Linux——mv命令(30)
00. 目录文章目录00. 目录01. 命令概述02. 命令格式03. 常用选项04. 参考示例05. 附录01. 命令概述mv命令是“move”单词的缩写,其功能大致和英文含义一样,可以移动文件或重命名文件。经常用来备份文件或者目录。02. 命令格式mv [选项]... 源文件 目标文件mv [选项]... 源文件... 目录mv [选项]... --target-director...
00. 目录
01. 命令概述
mv命令是“move”单词的缩写,其功能大致和英文含义一样,可以移动文件或重命名文件。经常用来备份文件或者目录。
02. 命令格式
mv [选项]... 源文件 目标文件
mv [选项]... 源文件... 目录
mv [选项]... --target-directory=DIRECTORY SOURCE...
03. 常用选项
将源文件重命名为目标文件,或将源文件移动至指定目录。
长选项必须使用的参数对于短选项时也是必需使用的。
--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 详细显示进行的步骤
--help 显示此帮助信息并退出
--version 显示版本信息并退出
备份文件的后缀为"~",除非以--suffix 选项或是SIMPLE_BACKUP_SUFFIX
环境变量指定。版本控制的方式可通过--backup 选项或VERSION_CONTROL 环境
变量来选择。以下是可用的变量值:
none, off 不进行备份(即使使用了--backup 选项)
numbered, t 备份文件加上数字进行排序
existing, nil 若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
simple, never 永远使用普通方式备份
04. 参考示例
4.1 重命名文件
[itcast@localhost test]$ ls
passwd
[itcast@localhost test]$ mv passwd password
[itcast@localhost test]$ ls
password
[itcast@localhost test]$
4.2 移动文件到指定目录中
[itcast@localhost test]$ ls
password
[itcast@localhost test]$ mkdir dir
[itcast@localhost test]$ mv password dir/
[itcast@localhost test]$
4.3 批量移动文件到指定目录中
[itcast@localhost test]$ mv a b c d dir/
[itcast@localhost test]$ tree dir
dir
├── a
├── b
├── c
├── d
└── password
0 directories, 5 files
[itcast@localhost test]$
4.4 批量移动文件到指定目录中 -t选项
[itcast@localhost test]$ ls
a b c d dir
[itcast@localhost test]$ mv -t dir a b c d
[itcast@localhost test]$ tree dir
dir
├── a
├── b
├── c
└── d
0 directories, 4 files
4.5 覆盖前询问
当移动文件时,目标位置恰好有同名文件,这时 mv 命令会覆盖掉原来的文件。如果想产生一个关于覆盖文件的提示,可以使用 -i
选项。
[itcast@localhost test]$ ls
a dir passwd
[itcast@localhost test]$ mv -i passwd a
mv:是否覆盖"a"? y
[itcast@localhost test]$
该提示会告诉我们目标位置处是否有同名文件。如果按 y 键,那么该文件将会被覆盖,否则不会。
4.6 覆盖前不询问
[itcast@localhost test]$ ls
a dir passwd
[itcast@localhost test]$ mv -f passwd a
[itcast@localhost test]$ ls
a dir
[itcast@localhost test]$
4.7 移动目录
[itcast@localhost test]$ ls
a dir
[itcast@localhost test]$ mkdir dir1
[itcast@localhost test]$ ls
a dir dir1
[itcast@localhost test]$ mv dir1/ dir
[itcast@localhost test]$ ls
a dir
[itcast@localhost test]$
如果目录dir不存在,将目录dir1改名为dir;否则,将dir1移动到dir中。
4.8 移动当前文件夹下的所有文件到上一级目录
[itcast@localhost test]$ ls
a dir
[itcast@localhost test]$ mv * ../
[itcast@localhost test]$ ls
[itcast@localhost test]$ ls ../
a dir password test 公共 模板 视频 图片 文档 下载 音乐 桌面
[itcast@localhost test]$
4.9 将当前dir目录中b文件移动到当前目录中
[itcast@localhost test]$ mv dir/b ./
[itcast@localhost test]$ ls
a b dir
[itcast@localhost test]$
4.10 创建备份文件
默认情况下,移动文件将会覆盖已存在的目标文件。但是如果移动错了文件,而目标文件已经被新的文件覆盖了,这时应该怎么办才好呢?可以用 -b
选项,该选项会在新文件覆盖旧文件时将旧文件做备份。
[itcast@localhost test]$ ls
a a.bak~ dir
[itcast@localhost test]$ mv -bv a dir/
"a" -> "dir/a" (备份:"dir/a~")
[itcast@localhost test]$ ls dir/
a a~ a.bak c d dir1
[itcast@localhost test]$
-b 不接受参数,mv会去读取环境变量VERSION_CONTROL来作为备份策略。
–backup该选项指定如果目标文件存在时的动作,共有四种备份策略:
VERSION_CONTROL=none或off : 不备份。
VERSION_CONTROL=numbered或t:数字编号的备份
VERSION_CONTROL=existing或nil:如果存在以数字编号的备份,则继续编号备份m+1…n:
执行mv操作前已存在以数字编号的文件log2.txt.1,那么再次执行将产生log2.txt2,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。
VERSION_CONTROL=simple或never:使用简单备份:在被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。
4.11 详细显示进行的步骤
[itcast@localhost test]$ ls
a.bak a.bak~ dir
[itcast@localhost test]$ mv -v a.bak dir/
"a.bak" -> "dir/a.bak"
[itcast@localhost test]$
4.12 更新选项
只在源文件文件比目标文件新,或目标文件不存在时才进行移动
[itcast@localhost test]$ ls -l a.bak dir/a.bak
-rw-rw-r-- 1 itcast itcast 0 7月 12 23:33 a.bak
-rw-rw-r-- 1 itcast itcast 0 7月 12 23:26 dir/a.bak
[itcast@localhost test]$ mv -u a.bak dir/
[itcast@localhost test]$ ls -l dir/a.bak
-rw-rw-r-- 1 itcast itcast 0 7月 12 23:33 dir/a.bak
[itcast@localhost test]$
4.13 不覆盖已存在文件
[itcast@localhost test]$ ls dir/
a a.bak c d dir1
[itcast@localhost test]$ ls
a a.bak~ dir
[itcast@localhost test]$ mv -n a dir/
[itcast@localhost test]$ ls
a a.bak~ dir
[itcast@localhost test]$
05. 附录
更多推荐
所有评论(0)