mv(move or rename files):顾名思义,移动或重命名文件。
常见用法:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
以下通过测试文件One.txt、Two.txt演示和记录各参数含义:(注意Two.txt文件比One.txt大一倍,方便区分)
[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 19:02 One.txt-rw-r--r-- 1 root root 736 May 30 19:09 Two.txt
--backup[=CONTROL] : make a backup of each existing destination file.该选项指定如果目标文件存在时的动作,共有四种:
1.CONTROL=none或off : never make backups,即不备份。
[root@localhost horen]# mv One.txt --backup=none Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 8-rw-r--r-- 1 root root 368 May 30 19:09 Two.txt
可以看到文件文件Two.txt被覆盖掉了。
2.CONTROL=numbered或t: make numbered backups,即数字编号的备份
[root@localhost horen]# cp Two.txt One.txt; cat One.txt >> Two.txt[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 19:41 One.txt-rw-r--r-- 1 root root 736 May 30 19:41 Two.txt[root@localhost horen]# mv One.txt --backup=numbered Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 19:41 Two.txt-rw-r--r-- 1 root root 736 May 30 19:41 Two.txt.~1~
可以看到文件Two.txt被覆盖前进行了备份,备份以数字编号1...n。
3.CONTROL=existing或nil: numbered if numbered backups exist, simple otherwise,即如果存在以数字编号的备份,则继续编号备份m+1...n
[root@localhost horen]# cp Two.txt One.txt; cat One.txt >> Two.txt[root@localhost horen]# ls -ltotal 24-rw-r--r-- 1 root root 368 May 30 19:50 One.txt-rw-r--r-- 1 root root 736 May 30 19:50 Two.txt-rw-r--r-- 1 root root 736 May 30 19:41 Two.txt.~1~[root@localhost horen]# mv One.txt --backup=existing Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 24-rw-r--r-- 1 root root 368 May 30 19:50 Two.txt-rw-r--r-- 1 root root 736 May 30 19:41 Two.txt.~1~-rw-r--r-- 1 root root 736 May 30 19:50 Two.txt.~2~
执行mv操作前已存在以数字编号的文件Two.txt.~1~,那么再次执行将产生Two.txt~2~,以次类推。如果之前没有以数字编号的文件,则使用下面讲到的简单备份。
4.CONTROL=simple或never: always make simple backups,即使用简单备份
[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 19:59 One.txt-rw-r--r-- 1 root root 736 May 30 19:59 Two.txt[root@localhost horen]# mv One.txt --backup=simple Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 19:59 Two.txt-rw-r--r-- 1 root root 736 May 30 19:59 Two.txt~
可以看到,在Two.txt被覆盖前进行了简单备份,简单备份只能有一份,再次被覆盖时,简单备份也会被覆盖。
-b like --backup but does not accept an argument
像--backup一样,但不接受参数。那么是怎么起作用的呢?上面提到了四种备份策略,该选项采用哪种呢?原来可以通过环境变量VERSION_CONTROL来控制备份策略:
[root@localhost horen]# export VERSION_CONTROL=simple[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 20:05 One.txt-rw-r--r-- 1 root root 736 May 30 20:05 Two.txt[root@localhost horen]# mv One.txt -b Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 20:05 Two.txt-rw-r--r-- 1 root root 736 May 30 20:05 Two.txt~
如果使用了-b选项,mv会去读取环境变量VERSION_CONTROL来作为备份策略。
-f, --force: do not prompt before overwriting该选项指示mv在覆盖前不需要提示,强制覆盖。
[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 20:13 One.txt-rw-r--r-- 1 root root 736 May 30 20:13 Two.txt[root@localhost horen]# mv One.txt -f Two.txt[root@localhost horen]# ls -ltotal 8-rw-r--r-- 1 root root 368 May 30 20:13 Two.txt
霸气侧漏不解释。
-i, --interactive: prompt before overwrite该选项指示mv在覆盖前给予提示:
[root@localhost horen]# cp Two.txt One.txt; cat One.txt >> Two.txt[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 20:15 One.txt-rw-r--r-- 1 root root 736 May 30 20:15 Two.txt[root@localhost horen]# mv One.txt -i Two.txtmv: overwrite `Two.txt'? n[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 30 20:15 One.txt-rw-r--r-- 1 root root 736 May 30 20:15 Two.txt
也许你会发现,即便没有-i选项,mv在覆盖文件前也会给出提示,这是为什么呢?
去查查配置文件.bash_profile或.bashrc,可许你会发现类似的下面几行:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
系统默认情况下会创建这三个alias,在文件覆盖前给出提示,当然你还可以定义其他的alias,比如:alias ll= 'ls -l'。
--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument,即去掉源文件名后面的斜线。关于这个选项,在gnu网页上找到了进一步说明:some shells can automatically append a trailing slash when performing file name completion on such symbolic links.即有些shell在处理符号链接时会自动在尾部添加一个斜线。
[root@localhost horen]# ls -ltotal 28drwxr-xr-x 2 root root 4096 May 31 09:57 mypathlrwxrwxrwx 1 root root 7 May 31 09:59 mypath.s -> mypath/-rw-r--r-- 1 root root 368 May 30 20:15 One.txt-rw-r--r-- 1 root root 736 May 30 20:15 Two.txt[root@localhost horen]# mv mypath.s/ yourpath.smv: cannot move `mypath.s/' to `yourpath.s': Not a directory[root@localhost horen]# mv mypath.s/ --strip-trailing-slashes yourpath.s[root@localhost horen]# ls -ltotal 28drwxr-xr-x 2 root root 4096 May 31 09:57 mypath-rw-r--r-- 1 root root 368 May 30 20:15 One.txt-rw-r--r-- 1 root root 736 May 30 20:15 Two.txtlrwxrwxrwx 1 root root 7 May 31 09:59 yourpath.s -> mypath/
上面,我在对符号链接mypath.s改名时,shell自动补齐后会出现一个斜线,因为该符号链接指向的是一个目录。使用该参数就可以去掉mypath.s/尾部的参数。事实上这并不是一个好的例子来说明该参数的作用,因为我们使用该参数来去掉源文件名的斜线还不如手动去删掉斜线来得方便,也许在使用程序处理时你并不能确定源文件是否尾部含有斜线,安全起见不妨加上该参数。
-S, --suffix=SUFFIXoverride the usual backup suffix,即替换通常的备份文件后缀,使用自定义后缀。
[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 31 10:31 One.txt-rw-r--r-- 1 root root 736 May 31 10:31 Two.txt[root@localhost horen]# mv One.txt -S .mysuf Two.txtmv: overwrite `Two.txt'? y[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 31 10:31 Two.txt-rw-r--r-- 1 root root 736 May 31 10:31 Two.txt.mysuf
上面定义了新了备份文件后缀.mysuf。该选项还是有用的,比如程序中可以使用日期作为备份文件后缀。
-t, --target-directory=DIRECTORYmove all SOURCE arguments into DIRECTORY,即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。
[root@localhost horen]# ls -ltotal 16-rw-r--r-- 1 root root 368 May 31 10:31 One.txt-rw-r--r-- 1 root root 736 May 31 10:31 Two.txt[root@localhost horen]# mkdir mypath[root@localhost horen]# ls -ltotal 24drwxr-xr-x 2 root root 4096 May 31 10:42 mypath-rw-r--r-- 1 root root 368 May 31 10:31 One.txt-rw-r--r-- 1 root root 736 May 31 10:31 Two.txt[root@localhost horen]# mv -t mypath One.txt Two.txt[root@localhost horen]# ls -ltotal 8drwxr-xr-x 2 root root 4096 May 31 10:42 mypath[root@localhost horen]# ls -l mypath/total 16-rw-r--r-- 1 root root 368 May 31 10:31 One.txt-rw-r--r-- 1 root root 736 May 31 10:31 Two.txt
-T, --no-target-directorytreat DEST as a normal file:把目标当作普通文件,真不知道该选项适用场合,暂不研究。
-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing,即只当源文件比目标文件新时才移动,当然如果目标文件不存在也移动:
[root@localhost horen]# ls -ltotal 24-rw-r--r-- 1 root root 368 May 31 10:47 One.txt-rw-r--r-- 1 root root 368 May 31 11:00 Three.txt-rw-r--r-- 1 root root 736 May 31 10:47 Two.txt[root@localhost horen]# mv One.txt -u Three.txt[root@localhost horen]# ls -ltotal 24-rw-r--r-- 1 root root 368 May 31 10:47 One.txt-rw-r--r-- 1 root root 368 May 31 11:00 Three.txt-rw-r--r-- 1 root root 736 May 31 10:47 Two.txt
上面One.txt文件比新建的Three.txt文件要旧,mv什么也没做。
-v, --verbose
explain what is being done : 显示执行过程,即执行过程打印更多信息:
[root@localhost horen]# ls -ltotal 24-rw-r--r-- 1 root root 368 May 31 10:47 One.txt-rw-r--r-- 1 root root 368 May 31 11:00 Tree.txt-rw-r--r-- 1 root root 736 May 31 10:47 Two.txt[root@localhost horen]# mv One.txt -v --backup=numbered Two.txtmv: overwrite `Two.txt'? y`One.txt' -> `Two.txt' (backup: `Two.txt.~1~')
参考文档: