linux 批量更改文件名后缀,批量修改文件名及扩展名
1.變量基礎(chǔ)知識
${var}????????變量var的值,?與$var相同
${var-DEFAULT}如果var沒有被聲明,?那么就以$DEFAULT作為其值?*
${var:-DEFAULT}如果var沒有被聲明,?或者其值為空,?那么就以$DEFAULT作為其值?*
${var=DEFAULT}如果var沒有被聲明,?那么就以$DEFAULT作為其值?*
${var:=DEFAULT}如果var沒有被聲明,?或者其值為空,?那么就以$DEFAULT作為其值?*
${var+OTHER}如果var聲明了,?那么其值就是$OTHER,?否則就為null字符串
${var:+OTHER}如果var被設(shè)置了,?那么其值就是$OTHER,?否則就為null字符串
${var?ERR_MSG}如果var沒被聲明,?那么就打印$ERR_MSG?*
${var:?ERR_MSG}如果var沒被設(shè)置,?那么就打印$ERR_MSG?*
${!varprefix*}匹配之前所有以varprefix開頭進(jìn)行聲明的變量
${!varprefix@}匹配之前所有以varprefix開頭進(jìn)行聲明的變量
#!/bin/bash
#
#
#定義path變量
path?=?/data/back/
#搜索path路徑下包含tar.gz的文件并刪除
find?$path?-name?"*.tar.gz"?-type?f?|xagrs?rm?-f
#優(yōu)化之后
find?${path:=/data/back/}?-name?"*.tar.gz"?-type?f?|xagrs?rm?-f
#此方式path變量沒有賦值的情況,有賦值使用賦值,沒有賦值使用=號后面的值
#此方式主要針對刪除操作
2.變量子串知識
${#string}????????????????????????$string的長度
${string:position}????????????????在$string中,?從位置$position開始提取子串
${string:position:length}????????在$string中,?從位置$position開始提取長度為$length的子串
${string#substring}????????????????從變量$string的開頭,?刪除最短匹配$substring的子串
${string##substring}????????????????從變量$string的開頭,?刪除最長匹配$substring的子串
${string%substring}????????????????從變量$string的結(jié)尾,?刪除最短匹配$substring的子串
${string%%substring}????????????????從變量$string的結(jié)尾,?刪除最長匹配$substring的子串
${string/substring/replacement}????????使用$replacement,?來代替第一個匹配的$substring
${string//substring/replacement}使用$replacement,?代替所有匹配的$substring
${string/#substring/replacement}如果$string的前綴匹配$substring,?那么就用$replacement來代替匹配到的$substring
${string/%substring/replacement}如果$string的后綴匹配$substring,?那么就用$replacement來代替匹配到的$substring
批量修改文件名
-rw-r--r--?1?root?root?0?Dec??1?00:10?eddy_10299_1_finished.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:10?eddy_10299_2_finished.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:10?eddy_10299_3_finished.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:10?eddy_10299_4_finished.jpg
#!/bin/bash
#?author:eddy?date:2015-11-30
#?rename?file
#?v1.0
for??i?in?`ls?temp/*.jpg`
do
mv?$i?`echo?${i%_finished*}.jpg`
done
以上方式偏于復(fù)雜及不好理解
使用linux自帶的rename命令進(jìn)行批量重命名
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_10.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_1.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_2.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_3.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_4.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_5.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_6.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_7.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_8.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?eddy_9.txt
[root@eddy?temp]#?rename?"eddy"?"yys"?eddy_*
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_10.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_1.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_2.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_3.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_4.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_5.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_6.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_7.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_8.txt
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_9.txt
批量修改擴(kuò)展名
依然可以用rename實現(xiàn)
[root@eddy?temp]#?rename?"txt"?"jpg"?yys_*
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_10.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_1.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_2.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_3.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_4.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_5.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_6.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_7.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_8.jpg
-rw-r--r--?1?root?root?0?Dec??1?00:33?yys_9.jpg
總結(jié)
以上是生活随笔為你收集整理的linux 批量更改文件名后缀,批量修改文件名及扩展名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有关人口及养老金收支相关的数据汇总(一)
- 下一篇: HTML---iframe框架及优缺点