日期:2014-05-16  浏览次数:20652 次

我使用过的Linux命令之mv - 文件或目录改名、移动位置

我使用过的Linux命令之mv - 文件或目录改名、移动位置

本文链接:http://codingstandards.iteye.com/blog/973122 ? (转载请注明出处)

用途说明

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]#

?

问题思考

相关资料

【1】中国IT实验室 linux下的mv命令使用详解
http://linux.chinaitlab.com/command/38019.html
【2】时光漂流瓶 linux下mv命令使用方法
http://www.9usb.net/200902/linux-mv.html
【3】Linux安全在线 linux mv命令参数及用法详解---移动或重命名文件或目录
http://www.linuxso.com/command/mv.html

?

返回 我使用过的Linux命令系列总目录

?