文章来源 :云计算就该这么学
1、通过sed替换
[[email protected] wolf]# find / -type f -name wolf.log
/wolf.log
/tmp/wolf.log
/root/wolf/wolf.log
[[email protected] wolf]# sed -i 's#wolf#yujing#g' find / -type f -name wolf.log
[[email protected] wolf]# find / -type f -name "wolf.log" | xargs catyujing
yujing
yujing
[[email protected] 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
[[email protected] data]# touch cat laolang.log |tr "\n" " "
[[email protected] 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
[[email protected] data]# f=wolf_20170806_10_wolf.jpg
[[email protected] data]# echo $f
wolf_20170806_10_wolf.jpg
[[email protected] data]# echo ${f%wolf*.jpg}
wolf_20170806_10_
[[email protected] data]# mv $f echo ${f%wolf*.jpg}.jpg
[[email protected] 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
[[email protected] data]# for f in ls *wolf.jpg ;do mv $f echo ${f%wolf*.jpg}.jpg;done
[[email protected] 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
[[email protected] data]# f=wolf_20170806_10_.jpg
[[email protected] data]# echo ${f/%jpg/log}
wolf_20170806_10_.log
[[email protected] data]# mv $f ${f/%jpg/log}[[email protected] 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
[[email protected] 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
[[email protected] data]# for f in ls *.jpg ;do mv $f ${f/%jpg/log} ;done
[[email protected] 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
侵权请私聊公众号删文