Batch rename sequential files by padding with zeroes

后端 未结 8 1177
天命终不由人
天命终不由人 2020-12-08 02:42

I have a bunch of files named like so:

output_1.png
output_2.png
...
output_10.png
...
output_120.png

What is the easiest way of renaming t

相关标签:
8条回答
  • 2020-12-08 03:11
    $rename output_ output_0 output_?   # adding 1 zero to names ended in 1 digit
    $rename output_ output_0 output_??  # adding 1 zero to names ended in 2 digits
    $rename output_ output_0 output_??? # adding 1 zero to names ended in 3 digits
    

    That's it!

    0 讨论(0)
  • 2020-12-08 03:11

    I'm following on from Adam's solution for OSX.

    Some gotchyas I encountered in my scenario were:

    1. I had a set of .mp3 files, so the sed was catching the '3' in the '.mp3' suffix. (I used basename instead of echo to rectify this)
    2. My .mp3's had spaces within their names, E.g., "audio track 1.mp3", this was causing basename+sed to screw up a little bit, so I had to quote the "$i" parameter.

    In the end, my conversion line looked like this:

    for i in *.mp3 ; do mv "$i" `printf "track_%02d.mp3\n" $(basename "$i" .mp3 | sed 's/[^0-9]*//g')` ; done
    
    0 讨论(0)
提交回复
热议问题