Linux 下使用 shell 批量修改文件名的三种方法
转载:twt企业IT社区
1、通过sed替换
[root@node01 wolf]# find / -type f -name wolf.log
/wolf.log
/tmp/wolf.log
/root/wolf/wolf.log
[root@node01 wolf]# sed -i 's#wolf#yujing#g' find / -type f -name wolf.log
[root@node01 wolf]# find / -type f -name "wolf.log" | xargs cat
yujing
yujing
yujing
[root@node01 wolf]#
2、通过变量子串替换
vi laolang.log
wolf_20170806_1_wolf.jpg
wolf_20170806_2_wolf.jpg
wolf_20170806_3_wolf.jpg
wolf_20170806_4_wolf.jpg
wolf_20170806_5_wolf.jpg
wolf_20170806_6_wolf.jpg
wolf_20170806_7_wolf.jpg
wolf_20170806_8_wolf.jpg
wolf_20170806_9_wolf.jpg
wolf_20170806_10_wolf.jpg
[root@node01 data]# touch cat laolang.log |tr "\n" " "
[root@node01 data]# ll
total 8316
drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 0 Jul 29 14:16 cat
drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1
drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2
drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3
-rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_wolf.jpg
[root@node01 data]# f=wolf_20170806_10_wolf.jpg
[root@node01 data]# echo $f
wolf_20170806_10_wolf.jpg
[root@node01 data]# echo ${f%wolf*.jpg}
wolf_20170806_10_
[root@node01 data]# mv $f echo ${f%wolf*.jpg}.jpg
[root@node01 data]# ll
total 8316
drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 0 Jul 29 14:16 cat
drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1
drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2
drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3
-rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_wolf.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_wolf.jpg
[root@node01 data]# for f in ls *wolf.jpg ;do mv $f echo ${f%wolf*.jpg}.jpg;done
[root@node01 data]# ll
total 8316
drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 0 Jul 29 14:16 cat
drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1
drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2
drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3
-rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.jpg
3、修改扩展名
把上面的jpg更换为log
[root@node01 data]# f=wolf_20170806_10_.jpg
[root@node01 data]# echo ${f/%jpg/log}
wolf_20170806_10_.log
[root@node01 data]# mv $f ${f/%jpg/log}
[root@node01 data]# ls
apache-maven-3.3.9 day1 laolang.log wolf_20170806_2_.jpg wolf_20170806_5_.jpg wolf_20170806_8_.jpg
apache-maven-3.3.9-bin.tar.gz day2 wolf_20170806_10_.log wolf_20170806_3_.jpg wolf_20170806_6_.jpg wolf_20170806_9_.jpg
cat day3 wolf_20170806_1_.jpg wolf_20170806_4_.jpg wolf_20170806_7_.jpg
[root@node01 data]# ll
total 8316
drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 0 Jul 29 14:16 cat
drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1
drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2
drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3
-rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.jpg
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.jpg
[root@node01 data]# for f in ls *.jpg ;do mv $f ${f/%jpg/log} ;done
[root@node01 data]# ll
total 8316
drwxr-xr-x 6 root root 4096 Jul 8 07:24 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jul 8 07:23 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 0 Jul 29 14:16 cat
drwxr-xr-x 2 root root 4096 Jul 29 06:00 day1
drwxr-xr-x 2 root root 4096 Jul 29 11:50 day2
drwxr-xr-x 2 root root 4096 Jul 29 14:12 day3
-rw-r--r-- 1 root root 251 Jul 29 14:16 laolang.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_10_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_1_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_2_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_3_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_4_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_5_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_6_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_7_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_8_.log
-rw-r--r-- 1 root root 0 Jul 29 14:16 wolf_20170806_9_.log
推荐阅读:
5T技术资源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,单片机,树莓派,等等。在公众号内回复「1024」,即可免费获取!!
评论