批量修改文件名称的shell脚本
#! /bin/bash
# imagine the folder contains ten thousand images
# named such as leon001362.png, leon104378.png
folder="your folder"
filelist=$(ls $folder)
format='.png'
for filename in $filelist
do
# pick 001362, 104378
num=${filename:4:6}
# use regular regression to make a integer
# 001362 to 1362 and 104378 unchanged
num=$(expr $num | sed 's/^0*//')
# rename
mv $filename ${num}${format}
done
这是跟一个项目有关的脚本,描述起来比较麻烦,直接上代码
#! /bin/bash
# You have a folder, which contains many images, such as, 1.png, 2.png ... 10000.png.
# You have a txt, which labels the class of a image, such as,
# "100 105 1" means 100.png, 101.png, 102.png, 103.png, 104.png and 105.png belong to class 1.
# "107 108 0" means 107.png and 108.png belong to class 0.
# But the txt has some errors, it contains "100 105 1" and "105 107 0"
# that means 105.png belongs to class 1 and 0, we want to change it to class 0.
total=0
array[0]=0
while read line
do
# seperate every line into a array, "100 105 1" into a array,
# array[0]=100, array[1]=105, array[2]=1
OLD_IFS="$IFS"
IFS=" "
arr=($line)
IFS="$OLD_IFS"
for(( i=${arr[0]}; i<=${arr[1]}; i=i+1 ))
do
array[$total]=$i
let total+=1
file="the image folder""$i"".png"
# if i.png exists in the image folder
if [ ! -f "$file" ]; then
echo "$i"".png"
fi
done
done < "the txt folder"
echo "total:"$total
len=${#array[@]}
echo "len:"$len
# remove the dupicated elements
for((i=0;i<$len;i++))
do
# notice the space near the brackets '[ ]'
if [[ ${array[$i]} -eq ${array[$i+1]} ]] || [[ ${array[$i]} -eq ${array[$i-1]} ]]; then
echo "equal:"$i
echo "frame:"${array[$i]}
fi
done
len=${#array[@]}
echo "len:"$len