Help with adding leading zeros to a filename

Hi
i need help in adding leading zero to filenames
e.g file name in my folder are
1_234sd.txt
23_234sd.txt
the output i need is
001_234sd.txt
023_234sd.txt
can i do this shell scripting
please help

Hi,

Welcome to the forum. follow the forum rules while posting request.

echo 1_234sd.txt | awk -F "_" '{printf "%03d_%s\n",$1,$2}'
echo 23_234sd.txt |  awk -F "_" '{printf "%03d_%s\n",$1,$2}'
1 Like
#!/bin/ksh93

ls *_234sd.txt |
while read FILENAME
do
   IFS="_" typeset -a aFilename=($FILENAME)
   IFS="" NEWFILENAME=$(printf "%03d_%s" ${aFilename[0]} ${aFilename[1]})
   echo "$FILENAME --> $NEWFILENAME"
   #mv $FILENAME $NEWFILENAME
done
1 Like