Linux shell script to add leading zeros to file names

后端 未结 10 1610
梦如初夏
梦如初夏 2020-12-04 06:43

I have a folder with about 1,700 files. They are all named like 1.txt or 1497.txt, etc. I would like to rename all the files so that all the filena

10条回答
  •  一生所求
    2020-12-04 07:21

    Let's assume you have files with datatype .dat in your folder. Just copy this code to a file named run.sh, make it executable by running chmode +x run.sh and then execute using ./run.sh:

    #!/bin/bash
    num=0
    for i in *.dat
    do
    
      a=`printf "%05d" $num`
      mv "$i" "filename_$a.dat"
      let "num = $(($num + 1))"
    done
    

    This will convert all files in your folder to filename_00000.dat, filename_00001.dat, etc.

提交回复
热议问题