Linux 命令 mv

Stella981
• 阅读 537

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
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
ES6 新增的数组的方法
给定一个数组letlist\//wu:武力zhi:智力{id:1,name:'张飞',wu:97,zhi:10},{id:2,name:'诸葛亮',wu:55,zhi:99},{id:3,name:'赵云',wu:97,zhi:66},{id:4,na
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这