mv 命令

--no-target-directory 参数确保对目录进行重命名而不是移动

https://www.gnu.org/software/coreutils/manual/html_node/Target-directory.html#Target-directory

‘--no-target-directory’
Do 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. In 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.
# 将 source 目录重命名为 dest ,如果 dest 存在,则将 source 放到 dest 目录下。
[root@localhost ~]# mv source dest # 将 source 目录重命名为 dest ,如果 dest 存在,则提示是否覆盖。
[root@localhost ~]# mv -T source dest
mv: overwrite ‘dest’? y
mv: cannot move ‘source’ to ‘dest’: File exists

场景 通配符匹配文件或目录

通配符:
* 匹配任意个字符
? 匹配单个任意字符

格式

<dir>/* 移动指定目录下所有源到目标目录
<dir>/*xxx 后缀
<dir>/xxx* 前缀
<dir>/*xxx* 包含
<dir>/?.xxx 名称为单个字符的 xxx 文件

实战

# 全匹配
[root@localhost ~]# mv test/* dev
# 后缀匹配
[root@localhost ~]# mv dev/*.txt test/
# 前缀匹配
[root@localhost ~]# mv jie* dev
# 包含匹配
[root@localhost ~]# mv *ea* dev # 将匹配如 a.txt 不会匹配 ab.txt 。? 不是可选,而是表示任意单个字符
[root@localhost ~]# mv test/a?.txt dev [root@localhost ~]# mv *a??.txt dev # 可以指定带通配符的多个源
[root@localhost ~]# mv dev/* test/* ./

bugs

# 提示没有找到文件或目录,当指定多个源时,其他存在的源会正常执行。注意这仅仅只是提示。
mv: cannot stat ‘dev/*’: No such file or directory # 这只是提示
[root@localhost ~]# mv dev/* test/* ./
mv: cannot stat ‘dev/*’: No such file or directory 提示 dev/* 没有匹配到任何文件或目录
mv: cannot stat ‘test/*’: No such file or directory 提示 test/* 没有匹配到任何文件或目录
[root@localhost ~]# ll dev
total 0
[root@localhost ~]# ll test
total 0

场景 移动多个文件或目录到指定目录

格式

mv [OPTION]... SOURCE... DIRECTORY

实战

最后一个参数为目标,当移动多个文件或目录到指定目录时,最后一个参数必须是已存在的目录。
[root@localhost ~]# mv a/c.txt a.txt b.txt bak

场景 重命名文件或目录

格式

mv [OPTION]... [-T] SOURCE DEST

实战

# 重命名文件
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 2 09:53 a.txt
[root@localhost test]# mv a.txt b.txt
[root@localhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 2 09:53 b.txt
# 重命名目录
[root@localhost test]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 2 09:55 dev
[root@localhost test]# mv dev pro
[root@localhost test]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 2 09:55 pro

Linux 命令 mv的更多相关文章

  1. Linux 命令 - mv: 移动或重命名文件

    命令格式 cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY ...

  2. linux命令 mv

    后缀--backup=<备份模式>:若需覆盖文件,则覆盖前先行备份: -b:当文件存在时,覆盖前,为其创建一个备份: -f:若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文件或目 ...

  3. 【LINUX命令】之MV

    linux下重命名文件或文件夹的命令mv既可以重命名,又可以移动文件或文件夹. 例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 注意: mv命 ...

  4. linux命令(7):mv命令

    mv命令 mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] ...

  5. Linux命令行下cp,rm,mv命令的使用

    以下的内容来源于<鸟哥的私房菜> Linux命令行下的复制.删除与移动:cp,rm,mv cp(copy)复制        cp这个命令的用途很多,除了单纯的复制之外,还可以创建链接文件 ...

  6. linux 命令随笔 ls cd pwd mkdir rm mv cp cat nl

    Linux 命令练习 ls命令 ls就是list的简写,目的是打印当前目录下的清单 格式 ls[选项][目录名] 常用参数 -a –all 列出目录下的所有文件,包括以 . 开头的隐含文件 -l 除了 ...

  7. 【Linux基础】常用Linux命令: cd, cp, ls, mkdir, mv, rm, su, uname

    常用Linux命令:cd, cp, ls, mkdir, mv, rm, su, uname cd命令:切换当前工作目录至 dirName(目录参数) 其中 dirName 可为绝对路径或相对路径.若 ...

  8. Linux 命令之mv

    mv命令也是Linux中很常见命令 作用:可以用来移动文件或者将文件改名 命令格式: mv [选项] 源文件或目录 目标文件或目录 命令参数: -b :若需覆盖文件,则覆盖前先行备份. -f :for ...

  9. Linux命令-目录处理命令:mv

    注意:在linux下面,剪切文件和改名是同一个命令mv,而不是两个独立的命令. mv /tmp/beijing/chaoyangqu /root 移动chaoyangqu目录到root目录下面 mv ...

随机推荐

  1. PDMan-2.1.0 正式发布:用心开源,免费的国产数据库建模工具 PowerDesigner

    PDMan是一款开源免费的数据库模型建模工具,支持Windows,Mac,Linux等操作系统,是PowerDesigner之外,更好的免费的替代方案.他具有颜值高,使用简单的特点.包含数据库建模,灵 ...

  2. Azure DevOps (TFS) 与 Office 集成

    Azure DevOps Service 或者Azure DevOps Server 都支持与office工具集成,实现在office中完成工作项的导入导出和批量修改等功能.用户可以使用自己熟悉的of ...

  3. Android添加新按键

    1.前言 在Android开发中可能会遇到添加新的按键的需求,本文将简单介绍如何在Android系统中完成一个新的按键的添加. 当系统有新的按键需要添加时,Linux内核下的键码到Android系统中 ...

  4. vuejs怎么和thinkphp结合

    vue在服务端部署时,我们都知道通过npm run build 指令打包好的dist文件,通过http指定是可以直接浏览的,Thinkphp通过域名指向index.php文件才可以浏览.要使前端正常调 ...

  5. 【前端知识体系-CSS相关】CSS布局知识强化

    1.实现两栏/三栏布局的方法? 表格布局 float + margin布局 inline-block布局 flexbox布局(兼容性的问题) 1.1 基础布局 <style> * { ma ...

  6. vue router 常用操作

    1.  普通路由 const routes = [ { path: '/index', component: index } ] 2. 重定向 redirect const routes = [ { ...

  7. Java中List集合去除重复数据的六种方法

    1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list. ...

  8. Python爬虫实战之爬取糗事百科段子【华为云技术分享】

    首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把,这次我们尝试一下用爬虫把他们抓取下来. 友情提示 糗事百科在前一段时间进行了改版,导致之前的代码没法用了,会导致无法输出和CPU占用过高的 ...

  9. 2019-11-29-WPF-如何在绑定失败异常

    原文:2019-11-29-WPF-如何在绑定失败异常 title author date CreateTime categories WPF 如何在绑定失败异常 lindexi 2019-11-29 ...

  10. 在ASP.NET Core中添加的Cookie如果含有特殊字符,会被自动转义

    我们知道在Cookie中有些字符是特殊字符,这些字符是不能出现在Cookie的键值中的. 比如"="是Cookie中用来分隔键和值的特殊字符,例如:Key01=Value01,表示 ...