原文链接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 ] # 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 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 |
5.批量把文件名后缀去掉