Sunday, 15 April 2012

bash - Shell command to format numbers inside filenames -



bash - Shell command to format numbers inside filenames -

i have folder containing next files:

trackingdata-00-1.data, trackingdata-00-2.data, ..., trackingdata-00-2345.data

and rename them formatting numbers 4 digits

trackingdata-00-0001.data, trackingdata-00-0002.data, ..., trackingdata-00-2345.data

how can accomplish bash shell command?

a pure bash solution:

for f in trackingdata-00-*.data; [[ $f =~ trackingdata-00-([0-9]+).data ]] mv "$f" $(printf "trackingdata-00-%04d.data" ${bash_rematch[1]}) done

a regular look extracts number pad , stores in bash_rematch array. printf used create new file name, number reinserted , padded zeros.

bash shell

No comments:

Post a Comment