文章目录


mv命令介绍

功能:

Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

重命名或者移动文件名字

常用选项:

-b 目标文件存在创建备份,备份文件是"文件名后跟~"

-u 当源文件比目的文件修改时间新时才移动

-v 显示移动信息

–help的内容如下:

Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat 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
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

Report mv bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'mv invocation'

案例:

1. -b 如果要拷贝过去的文件夹下有目标文件,则加~

[root@zmedu ~]# mkdir test1 test2
[root@zmedu ~]# touch test1/a.txt
[root@zmedu ~]# touch test1/b.txt
[root@zmedu ~]# touch test/a.txt
touch: cannot touch `test/a.txt': No such file or directory
[root@zmedu ~]# touch test2/a.txt
[root@zmedu ~]# mv -b test2/a.txt test1
mv: overwrite `test1/a.txt'? y
[root@zmedu ~]# ls test1/
a.txt a.txt~ b.txt
[root@zmedu ~]#
可以看到多了一个a.txt,而且第二个a.txt后面有~符号

2. -f 强制覆盖

[root@zmedu ~]# ls
test1 test2
[root@zmedu ~]# ll test1/
total 0
-rw-r--r-- 1 root root 0 Nov 17 20:42 a.txt
-rw-r--r-- 1 root root 0 Nov 17 20:42 a.txt~
-rw-r--r-- 1 root root 0 Nov 17 20:42 b.txt
[root@zmedu ~]# ll test2/
total 0
-rw-r--r-- 1 root root 0 Nov 17 20:48 a.txt
[root@zmedu ~]# mv -f test2/a.txt test1/
[root@zmedu ~]# ll test1/
total 0
-rw-r--r-- 1 root root 0 Nov 17 20:48 a.txt
-rw-r--r-- 1 root root 0 Nov 17 20:42 a.txt~
-rw-r--r-- 1 root root 0 Nov 17 20:42 b.txt
[root@zmedu ~]#

3. -i:默认选项,当目标文件存在时,提示是否覆盖

[root@zmedu ~]# touch a.txt
[root@zmedu ~]# ls
a.txt test1 test2
[root@zmedu ~]# mv -i a.txt test1/
mv: overwrite `test1/a.txt'? y
[root@zmedu ~]#

4. -n 如果文件存在则不覆盖

[root@zmedu ~]# ls
a.txt test1 test2
[root@zmedu ~]# mv -vn a.txt test1/
[root@zmedu ~]# ll test1/
total 0
-rw-r--r-- 1 root root 0 Nov 17 20:51 a.txt
-rw-r--r-- 1 root root 0 Nov 17 20:42 a.txt~
-rw-r--r-- 1 root root 0 Nov 17 20:42 b.txt

5. -v 显示详细信息

[root@zmedu ~]# mv -v a.txt  test1/
mv: overwrite `test1/a.txt'? y
`a.txt' -> `test1/a.txt'
[root@zmedu ~]#

总结:

学习的过程中多用 --help是必要的 ,如果有不懂的参数,直接–help一下,英文的解释会更清晰明了。