If you have a small requirement at work, you need to rename a series of files.
The original format is string_1.obj. Now you want to change the number in the file name to a fixed length. If the number is less than the number of digits, it is changed to string_001.obj.
I learned to write a script. It's a good dish. I 'd like to post it for your advice.
#! /bin/bash# this script is used to rename files# the orignal filename is as XXXX_1.obj# and the final filename is as XXX_0001.obj the width of the num is given as a parameterrm result.txtexport suffix=".obj"find . -name "*$suffix" > result.txtcat result.txt | while read onelinedoexport orifilename=${oneline##*/} #cut off the path of filenameexport prefilename=${orifilename%_*} #without "_"export numfilename=${oneline##*_}export numfilename=${numfilename%.*} #get the num in filename((i=$1-${#numfilename})) #get the num of zero which is neededexport zeros=`echo $numfilename | awk '{printf "%0'"$i"'d",0}{print}'` export finalfilename=$prefilename"_"$zeros$suffix#echo "$orifilename, $finalfilename"#rename "s/$orifilename/$finalfilename/" ./$finalfilenamemv $orifilename $finalfilenamedoneecho "Done"