在linux日常管理中,修改文件名是常见的操作之一,我们主要通过mv命令来实现,以面是它的详细介绍。

一、文件/文件夹重命名

mv命令是linux中最基本的文件/文件夹重命名命令

语法:

mv [选项] 源  目标

案例实战:

#命令前查询当前目录下的文件名
[root@liuguohua opt]# ll
total 48
-rw-r--r-- 1 root root 0 Jun 24 12:27 liuguohua1.com.txt
-rw-r--r-- 1 root root 0 Jun 24 12:28 liuguohua.com.3.txt

[root@rocky95-69 opt]# mv liuguohua1.com.txt liuguohua.com.txt     #对本目录下的liuguohua1.com.txt重新命名为liuguohua.com.txt

#命名后查询当前目录下的文件名
[root@liuguohua opt]# ll
total 48
-rw-r--r-- 1 root root 0 Jun 24 12:28 liuguohua.com.3.txt
-rw-r--r-- 1 root root 0 Jun 24 12:27 liuguohua.com.txt

#把/opt/目录下的liuguohua.com.txt文件移到到/opt目录,并重命名为liuguohua
[root@liuguohua opt]# mv liuguohua.com.txt /tmp/liuguohua
[root@rocky95-69 opt]# ll /tmp/
total 468
-rw-r--r--. 1 root root 18 Jun 24 22:26 last_files_set_mode.pl
drwxr-xr-x 2 root root 21 Jun 24 12:38 lgh_dir
-rw-r--r-- 1 root root 0 Jun 24 12:27 liuguohua

二、批量重命名文件

rename命令

基本语法

rename 's/原字符串/新字符串/'  文件匹配模式

案例实战:

[root@liuguohua opt]# ll
total 48
-rw-r--r-- 1 root root 0 Jun 25 21:04 lgh.txt
-rw-r--r-- 1 root root 0 Jun 24 12:28 liuguohua.com.3.txt
-rw-r--r-- 1 root root 0 Jun 25 21:04 zhongguo.txt

#rhel/centos系不支持复杂正则表达式,故批量改名命令如下:
[root@liuguohua opt]# rename .txt .log *.txt

[root@liuguohua opt]# ll
total 48
-rw-r--r-- 1 root root 0 Jun 25 21:04 lgh.log
-rw-r--r-- 1 root root 0 Jun 24 12:28 liuguohua.com.3.log
-rw-r--r-- 1 root root 0 Jun 25 21:04 zhongguo.log

#如果是ubuntu/debian系,批量改名命令如下:
[root@ubuntu opt]# rename 's/\.txt$/.log/' *.txt

#perl正式下的rename还有以下案例
大小写转换(将所有大写改为小写)
rename 'y/A-Z/a-z/' *

#将文件名中的"befor"字符替换为"after"字符
rename 's/befor/after/' *befor*
声明:欢迎大家光临本站,学习IT运维技术,转载本站内容,请注明内容出处”来源刘国华教育“。如若本站内容侵犯了原著者的合法权益,请联系我们进行处理。