mv 命令
--no-target-directory 参数确保对目录进行重命名而不是移动
https://www.gnu.org/software/coreutils/manual/html_node/Target-directory.html#Target-directory
1‘--no-target-directory’ 2Do not treat the last operand specially when it is a directory or a symbolic link to a directory. This can help avoid race conditions in programs that operate in a shared area. For example, when the command ‘mv /tmp/source /tmp/dest’ succeeds, there is no guarantee that /tmp/source was renamed to /tmp/dest: it could have been renamed to /tmp/dest/source instead, if some other process created /tmp/dest as a directory. However, if mv -T /tmp/source /tmp/dest succeeds, there is no question that /tmp/source was renamed to /tmp/dest. 3 4In the opposite situation, where you want the last operand to be treated as a directory and want a diagnostic otherwise, you can use the --target-directory (-t) option. 5 6 7 8# 将 source 目录重命名为 dest ,如果 dest 存在,则将 source 放到 dest 目录下。 9[root@localhost ~]# mv source dest 10 11# 将 source 目录重命名为 dest ,如果 dest 存在,则提示是否覆盖。 12[root@localhost ~]# mv -T source dest 13mv: overwrite ‘dest’? y 14mv: cannot move ‘source’ to ‘dest’: File exists
场景 通配符匹配文件或目录
1通配符: 2* 匹配任意个字符 3? 匹配单个任意字符
格式
1<dir>/* 移动指定目录下所有源到目标目录 2<dir>/*xxx 后缀 3<dir>/xxx* 前缀 4<dir>/*xxx* 包含 5<dir>/?.xxx 名称为单个字符的 xxx 文件
实战
1# 全匹配 2[root@localhost ~]# mv test/* dev 3# 后缀匹配 4[root@localhost ~]# mv dev/*.txt test/ 5# 前缀匹配 6[root@localhost ~]# mv jie* dev 7# 包含匹配 8[root@localhost ~]# mv *ea* dev 9 10# 将匹配如 a.txt 不会匹配 ab.txt 。? 不是可选,而是表示任意单个字符 11[root@localhost ~]# mv test/a?.txt dev 12 13[root@localhost ~]# mv *a??.txt dev 14 15# 可以指定带通配符的多个源 16[root@localhost ~]# mv dev/* test/* ./
bugs
1# 提示没有找到文件或目录,当指定多个源时,其他存在的源会正常执行。注意这仅仅只是提示。 2mv: cannot stat ‘dev/*’: No such file or directory # 这只是提示 3 4 5[root@localhost ~]# mv dev/* test/* ./ 6mv: cannot stat ‘dev/*’: No such file or directory 提示 dev/* 没有匹配到任何文件或目录 7mv: cannot stat ‘test/*’: No such file or directory 提示 test/* 没有匹配到任何文件或目录 8[root@localhost ~]# ll dev 9total 0 10[root@localhost ~]# ll test 11total 0
场景 移动多个文件或目录到指定目录
格式
mv [OPTION]... SOURCE... DIRECTORY
实战
1最后一个参数为目标,当移动多个文件或目录到指定目录时,最后一个参数必须是已存在的目录。 2[root@localhost ~]# mv a/c.txt a.txt b.txt bak
场景 重命名文件或目录
格式
mv [OPTION]... [-T] SOURCE DEST
实战
1# 重命名文件 2[root@localhost test]# ll 3total 0 4-rw-r--r--. 1 root root 0 Aug 2 09:53 a.txt 5[root@localhost test]# mv a.txt b.txt 6[root@localhost test]# ll 7total 0 8-rw-r--r--. 1 root root 0 Aug 2 09:53 b.txt 9# 重命名目录 10[root@localhost test]# ll 11total 0 12drwxr-xr-x. 2 root root 6 Aug 2 09:55 dev 13[root@localhost test]# mv dev pro 14[root@localhost test]# ll 15total 0 16drwxr-xr-x. 2 root root 6 Aug 2 09:55 pro