文章目录
1. mv 命令说明
mv 用于移动文件、目录或重命名文件,是 Linux 系统中常用的命令之一。参数如下:
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
选项 | 作用 |
---|---|
-b | 当覆盖文件时会对被覆盖文件进行先行备份。 |
-i | 交互式操作,当目标文件已经存在时,会询问是否覆盖。 |
-n | 试探性覆盖文件(依据为目标文件是否存在)。 |
-u | 只有在源文件比目标文件新或者目标文件不存在时才进行操作。 |
-f | 强制操作,不进行任何提示,如果目标存在时,会直接覆盖。 |
-t | 即指定mv的目标目录,该选项适用于移动多个源文件到一个目录的情况,此时目标目录在前,源文件在后。 |
2. mv 命令语法
mv [选项] [-T] 源文件 目标文件;
或:mv [选项] 源文件... 目录;
或:mv [选项] -t 目录 源文件;
3. mv 命令示例
3.1 重命名文件
不加参数,可以将文件重命名
mv 1.log 2.txt
若重命名的文件存在,会被强制覆盖
mv 2.txt 3.log
3.2 -i(覆盖提示)
aaa 目录下已有 4.log,所以加了 -i 后,会提示是否覆盖,y 覆盖、n 不覆盖
3.3 -f(强制覆盖)
不管目标文件是否存在,-f 都会强制覆盖,且没有提示
mv -f 4.txt 5.log
3.4 -n(存在不覆盖)
如果文件存在,-n 没有任何效果
mv -n 6.txt 7.log
当目标文件不存在时,-n 就是重命名
mv -n 6.txt 8.log
3.5 -u(以新盖旧)
只有在源文件比目标文件新或者目标文件不存在时才进行操作
mv -u 7.log 8.log
若目标文件的日期新,则没有任何效果
mv -u 8.log 9.log
若目标文件不存在,则进行重命名
mv -u 8.log 10.log
3.6 -b(覆盖时备份)
当覆盖文件时会对被覆盖文件进行先行备份
mv -b 11.log 12.log
3.7 移动文件至目录
mv 9.log bbb/
当然也可以使用通配符,比如 ? * 这些
mv *.log bbb/ #将所有以 .log 结尾的文件移动至bbb文件夹
3.8 移动目录
若目标目录不存在,则是将源目录重命名
mv bbb/ ccc
若目标目录存在,则是将源目录移动至目标目录下
mv ccc/ ddd/
4. 总结
mv 常用于移动文件、目录或重命名文件、目录,我一般用通配符移动一批文件。