原文链接https://www.cnblogs.com/sunmmi/articles/6709125.html

shell 批量重命名

 

1.把文件名的第一字母批量改成a

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 8801.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8802.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8803.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8804.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8805.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8806.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8807.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8808.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 8809.txt
[root@jenkins test]# for i in `ls`;do mv -f $i `echo $i | sed 's/^./a/'`;done
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 a801.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a802.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a803.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a804.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a805.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a806.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a807.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a808.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a809.txt

方法二:

1
2
3
4
5
for i in `ls`
do
    newfile = `echo $i | sed 's/^./a/'`
    mv -f $i $newfile
done

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@tracker web30]# ll
total 88
-rw-r--r-- 1 root root  872 Oct 18 19:32 8901www.conf
-rw-r--r-- 1 root root  652 Oct 18 19:32 8902order.conf
-rw-r--r-- 1 root root  846 Oct 18 19:32 8904product.conf
-rw-r--r-- 1 root root  842 Oct 18 19:32 8906topic.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8907user.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8908cart.conf
-rw-r--r-- 1 root root  838 Oct 18 19:32 8909api.conf
-rw-r--r-- 1 root root  879 Oct 18 19:32 8910m.conf
-rw-r--r-- 1 root root  932 Oct 18 19:32 8911search.conf
-rw-r--r-- 1 root root 1233 Oct 18 19:32 8912webapi.conf
-rw-r--r-- 1 root root 1163 Oct 18 19:32 8913tuan.conf
-rw-r--r-- 1 root root  967 Oct 18 19:32 8914upload.conf
-rw-r--r-- 1 root root  923 Oct 18 19:32 8915static.conf
-rw-r--r-- 1 root root  824 Oct 18 19:32 8916temp.conf
-rw-r--r-- 1 root root  830 Oct 18 19:32 8920pay.conf
-rw-r--r-- 1 root root  841 Oct 18 19:32 8922passport.conf
-rw-r--r-- 1 root root  875 Oct 18 19:32 8924mapi.conf
-rw-r--r-- 1 root root  833 Oct 18 19:32 8932lqrcodeserver.conf
-rw-r--r-- 1 root root  839 Oct 18 19:32 8933ystorage.conf
-rw-r--r-- 1 root root  825 Oct 18 19:32 8934larea.conf
-rw-r--r-- 1 root root  841 Oct 18 19:32 8935lcaptchaserver.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8936supply.conf
 
# 文件的第二个名批量改成87
[root@tracker web30]# for i in `ls`;do mv -f $i `echo $i | sed 's/^8./87/'`;done
[root@tracker web30]# ll
total 88
-rw-r--r-- 1 root root  872 Oct 18 19:32 8701www.conf
-rw-r--r-- 1 root root  652 Oct 18 19:32 8702order.conf
-rw-r--r-- 1 root root  846 Oct 18 19:32 8704product.conf
-rw-r--r-- 1 root root  842 Oct 18 19:32 8706topic.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8707user.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8708cart.conf
-rw-r--r-- 1 root root  838 Oct 18 19:32 8709api.conf
-rw-r--r-- 1 root root  879 Oct 18 19:32 8710m.conf
-rw-r--r-- 1 root root  932 Oct 18 19:32 8711search.conf
-rw-r--r-- 1 root root 1233 Oct 18 19:32 8712webapi.conf
-rw-r--r-- 1 root root 1163 Oct 18 19:32 8713tuan.conf
-rw-r--r-- 1 root root  967 Oct 18 19:32 8714upload.conf
-rw-r--r-- 1 root root  923 Oct 18 19:32 8715static.conf
-rw-r--r-- 1 root root  824 Oct 18 19:32 8716temp.conf
-rw-r--r-- 1 root root  830 Oct 18 19:32 8720pay.conf
-rw-r--r-- 1 root root  841 Oct 18 19:32 8722passport.conf
-rw-r--r-- 1 root root  875 Oct 18 19:32 8724mapi.conf
-rw-r--r-- 1 root root  833 Oct 18 19:32 8732lqrcodeserver.conf
-rw-r--r-- 1 root root  839 Oct 18 19:32 8733ystorage.conf
-rw-r--r-- 1 root root  825 Oct 18 19:32 8734larea.conf
-rw-r--r-- 1 root root  841 Oct 18 19:32 8735lcaptchaserver.conf
-rw-r--r-- 1 root root  840 Oct 18 19:32 8736supply.conf

2.文件的前3个字符改成BBB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 a801.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a802.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a803.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a804.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a805.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a806.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a807.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a808.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 a809.txt
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB1.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB2.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB3.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB4.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB5.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB6.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB7.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB8.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB9.txt

 3.文件前面批量加个字符abc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB1.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB2.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB3.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB4.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB5.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB6.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB7.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB8.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 BBB9.txt
[root@jenkins test]# for i in `ls`;do mv -f $i `echo abc_$i`;done
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB1.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB2.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB3.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB4.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB5.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB6.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB7.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB8.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB9.txt

 4.文件名所有小写字变成大写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB1.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB2.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB3.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB4.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB5.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB6.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB7.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB8.txt
-rw-r--r-- 1 root root 0 Apr 14 15:02 abc_BBB9.txt
[root@jenkins test]# for i in `ls`;do mv -f $i `echo $i | tr 'a-z' 'A-Z'`;done
[root@jenkins test]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB1.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB2.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB3.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB4.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB5.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB6.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB7.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB8.TXT
-rw-r--r-- 1 root root 0 Apr 14 15:02 ABC_BBB9.TXT


原文链接https://www.cnblogs.com/pangblog/p/3243931.html

1、删除所有的 .bak 后缀:

rename 's/\.bak$//' *.bak

注意,这个命令的格式组织如下:s/   \.bark$   / / 是s/para1/para2/ 这个有点想sed的语法,将para1匹配的字符串换成para2

2、把 .jpe 文件后缀修改为 .jpg:

rename 's/\.jpe$/\.jpg/' *.jpe

3、把所有文件的文件名改为小写:

rename 'y/A-Z/a-z/' *

4、将 abcd.jpg 重命名为 abcd_efg.jpg:

for var in *.jpg; do mv "$var" "${var%.jpg}_efg.jpg"; done

其中,此处涉及到shell的字符串匹配操作:

1> ${variable#pattern}
如果pattern匹配variable的开始部分,从variable的开始处删除字符直到第一个匹配的位置,包括匹配部分,返回剩余部分。

2> ${variable##pattern}
如果pattern匹配variable的开始部分,从variable的开始处删除字符直到最后一个匹配的位置,包括匹配部分,返回剩余部分。

3> ${variable%pattern}
如果pattern匹配variable的结尾部分,从variable的结尾处删除字符直到第一个匹配的位置,包括匹配部分,返回剩余部分。

4> ${variable%%pattern}
如果pattern匹配variable的结尾部分,从variable的结尾处删除字符直到最后一个匹配的位置,包括匹配部分,返回剩余部分。

5、将 abcd_efg.jpg 重命名为 abcd_lmn.jpg:

for var in *.jpg; do mv "$var" "${var%_efg.jpg}_lmn.jpg"; done

6、把文件名中所有小写字母改为大写字母:

for var in `ls`; do mv -f "$var" `echo "$var" |tr a-z A-Z`; done

tr:traslate or delete characters

7、把格式 *_?.jpg 的文件改为 *_0?.jpg:

for var in `ls *_?.jpg`; do mv "$var" `echo "$var" |awk -F '_' '{print $1 "_0" $2}'`; done

8、把文件名的前三个字母变为 vzomik:

for var in `ls`; do mv -f "$var" `echo "$var" |sed 's/^.../vzomik/'`; done

9、把文件名的后四个字母变为 vzomik:

for var in `ls`; do mv -f "$var" `echo "$var" |sed 's/....$/vzomik/'`; done

参考:

shell 批量重命名的更多相关文章

  1. Linux命令行bash批量重命名文件

    本文介绍下,在linux下使用shell批量重命名文件的例子,有需要的朋友参考下吧. 在linux中,重命名文件名,需要用到mv命令.如果需要批量重命名名一批文件,就需要写bash脚本或命令行了. 例 ...

  2. shell下批量重命名svn文件的方法

    shell下批量重命名svn文件的方法 目标: 将svn目录下所有文件重命名 , 原文件前缀为 ucc_ , 批量改为 xmd_ 用tree看下当前svn目录 ucc_1.c ucc_1.h ucc_ ...

  3. linux下的文件操作——批量重命名

    概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...

  4. Linux批量重命名文件

    五种方法实现Linux批量重命名文件 Linux批量重命名文件是指对某些特定的文件统一进行重新命名,以改变原来一批文件的名称,这里介绍五种方法来实现. Linux批量重命名文件会涉及到改变一个字母.改 ...

  5. Java 实现批量重命名,亲测可用(精简版)

    之前在网上下载了很多视频,解压缩后,发现里面每个文件前面都有一长串的网址,导致我根本看不清每个视频的名字到底叫什么? 网上搜了一些批量重命名的方法,可都不是我想要的,既然这样,干脆自己动手用Java写 ...

  6. python之对指定目录文件夹的批量重命名

    python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...

  7. C#实现文件批量重命名源码下载

    本文要实现的功能是 文件批量重命名,当选择一个文件夹时,通过操作可以把文件夹下面所有文件进行重命名.建立了HoverTreeBatch项目. 然后 定义文件夹信息: DirectoryInfo _Th ...

  8. 使用java对文件批量重命名

    有时候从网络上下载的电视剧或者动漫,名字上都会被该网站加上前缀或者后缀,如图: 那么处女座的同学就不同意了,不行,我就是想让它按照我的习惯方式命名!但是呢,一个个修改是不是特别麻烦,如果是上百个呢?如 ...

  9. linux下rename用法--批量重命名

    Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,早期的Linux发行版基本上使用的是C语言版本的,现在已经很难见到C语言版本的了, 由于历史原因,在Perl语言 ...

随机推荐

  1. opencv 学习一安装环境vs2015+opencv3

    参考博客 http://www.cnblogs.com/skyfsm/p/6840202.html 针对 模块计算机类型“X64”与目标计算机类型“X86”这个问题,我使用cmake 对环境的工程进行 ...

  2. Luogu4688 [Ynoi2016]掉进兔子洞 【莫队,bitset】

    题目链接:洛谷 我们知道要求的是\([l_1,r_1],[l_2,r_2],[l_3,r_3]\)的可重集取交的大小,肯定是要用bitset的,那怎么做可重集呢? 那就是要稍微动点手脚,首先在离散化的 ...

  3. 数据结构实验之查找一:二叉排序树 (SDUT 3373)

    二叉排序树(Binary Sort Tree),又称二叉查找树(Binary Search Tree),也称二叉搜索树. #include <stdio.h> #include <s ...

  4. mac 安装 mysql 5.7

    下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下一步,经过一系列安装步骤后,会跳出一个这样的界面,请注意!!! 上面红框中是你 ...

  5. Visual Studio2019安装步骤

    学校使用的版本是2012版本,而现在讲的版本是2019版本,差别不大,个人认为2019更能胜任学习任务. 另外VS2019是完全免费的,版本越高越好了!毕竟C++都出了C++20对吧. Step 1: ...

  6. AOP 与 Spring中AOP使用(上)

    AOP简介 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的延续 ...

  7. CSS中的cursor属性

    css中的cursor这个属性是用来设置光标形状的. 这个属性定义了鼠标指针放在一个元素边界范围内时所用的光标的形状. 默认值:auto 继承性:yes 出现版本:css2 JavaScript语法: ...

  8. 013-多线程-基础-Fork/Join框架、parallelStream讲解

    一.概述 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 它同ThreadPoolExecut ...

  9. 使用Lock对象实现同步效果

    Lock是一个接口,为了使用一个Lock对象,需要用到   Lock lock = new ReentrantLock();   与 synchronized (someObject) 类似的,loc ...

  10. jmeter 随机取一个值的方法

    1.添加用户自定义变量 在要用到随机值的地方写入 ${__RandomFromMultipleVars(1|2|0)} 例子: 效果: