一天一条Linux指令-mv

来源:互联网 发布:mac f117笔记本 编辑:程序博客网 时间:2024/05/16 07:13

用途说明

mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。该命令如同MSDOS下的ren和move的组合。

 

常用参数

格式:mv file1 file2

将文件file1改名为file2。

 

格式:mv file dir

将文件file移动到目录dir中。

 

格式:mv file1 file2 file3 dir

格式:mv -t dir file1 file2 file3

将文件file1,file2和file3移动到目录dir中。

 

格式:mv -i file1 file2

将文件file1改名为file2,如果file2已经存在,则询问是否覆盖。i=interactive, prompt before overwrite。

一般情况下,我们使用的mv是一个别名:alias mv='mv -i'

 

格式:mv -f file1 file2

将文件file1改名为file2,即使file2存在,也是直接覆盖掉。f=force, do not prompt before overwriting。这是个危险的选项,最好不用加上它。

 

格式:mv dir1 dir2

如果目录dir2不存在,将目录dir1改名为dir2;否则,将dir1移动到dir2中。

 

使用示例

示例一 文件改名的例子

[root@jfht ~]# ls fangsong.ttf 
fangsong.ttf
[root@jfht ~]# mv fangsong.ttf 仿宋_GB2312.ttf 
[root@jfht ~]# ls fangsong.ttf 仿宋_GB2312.ttf  
ls: fangsong.ttf: 没有那个文件或目录
仿宋_GB2312.ttf
[root@jfht ~]# mv 仿宋_GB2312.ttf fangsong.ttf 
[root@jfht ~]# ls fangsong.ttf 仿宋_GB2312.ttf 
ls: 仿宋_GB2312.ttf: 没有那个文件或目录
fangsong.ttf
[root@jfht ~]#

 

示例二 移动文件的例子

最近发现/目录下有很多日志文件(某个日志路径 配置 有问题),想把它清除掉,先移动到/tmp目录中,tmpwatch会自动帮你去删除它们。

[root@web ~]# ls /LOG* | wc -l 
3484
[root@web ~]# mv /LOG* /tmp 
[root@web ~]# ls /LOG* | wc -l 
ls: /LOG*: 没有那个文件或目录
0
[root@web ~]#

 

示例三 mv是一个别名

[root@web tmp]# type -a mv 
mv is aliased to `mv -i'
mv is /bin/mv

[root@web tmp]# touch 1.txt 2.txt 
[root@web tmp]# mv 1.txt 2.txt 
mv:是否覆盖“2.txt”? y
[root@web tmp]#

0 0