Shell script: rename files and prepend leading zeros








you may want to rename the files in a directory, where the file names start with numbers, e.g. 1.txt, 2.txt, 3.txt, 10.txt, 100.txt, to file names with padding zeros: 001.txt, 002.txt, 010.txt, 100.txt. The script below do the work for you:
for f in $(ls *.txt)
do
    mv $f $(printf %03d.%s ${f%.*} ${f##*.})
done



No comments:

Post a Comment