I am trying to rename a set of files. The files have this form


I have files numberes from 1 to 1500, and I want to rename only the ones between 100 and 199

I have tried the following rename command

rename -n 's/frame1[\d]{2}_/sprintf("%05d", $&)/e' *.bin

adapting other command I was using for other purposes, but the output is

frame198_simobject0.bin -> 00000simobject0.bin
frame199_simobject0.bin -> 00000simobject0.bin

I don't really understand how the substitution rule for the command is working, so some help here would be very useful.

2 Answers 2


$& is the whole matched string - including the leading frame and trailing _. Since this is non-numeric, the attempt to format it according to %05d fails.

You probably want something like s/frame(1\d{2})_/sprintf("frame%05d_", $1)/e


$ rename -n 's/frame(1\d{2})_/sprintf("frame%05d_", $1)/e' *.bin
rename(frame198_simobject0.bin, frame00198_simobject0.bin)
rename(frame199_simobject0.bin, frame00199_simobject0.bin)

Note that you don't need character range brackets [...] around the \d.

  • thanks, this works like a charm. What does the $1 means?
    – jjcasmar
    Jan 10, 2020 at 23:10
  • @jjcasmar $1 is a reference to the first capture group i.e. the text matching (1\d{2}). See for example Use Parentheses for Grouping and Capturing Jan 10, 2020 at 23:16
  • thank you very much!
    – jjcasmar
    Jan 10, 2020 at 23:20
  • @jjcasmar If one of the answers solved your issue, please take a moment to accept it by clicking on the checkmark on the left. That will mark the question as answered and is the way that thanks are conveyed on the Stack Exchange sites.
    – terdon
    Jan 11, 2020 at 0:06

If you want to only rename a susbset of the files, a simple solution is to only pass those files to rename. So, instead of using *bin, use brace expansion, to only pick up files with the right names:

rename -n 's/frame(\d+)_/sprintf("%05d", $1)/e'  frame{100..199}_simobject0.bin
  • I didnt even know this. What I was trying was to rename to then mv the files to a different folder. Now I know I can just mv that subset. I vote up this solution, but I must accept the previous one
    – jjcasmar
    Jan 11, 2020 at 13:35
  • @jjcasmar oh, yes, please do. This is just a minor addition, steeldriver gave the solution.
    – terdon
    Jan 12, 2020 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .