0

This question already has an answer here:

I have filenames like:

Some Text (1).jpg
Some Text (2).jpg
..
Some Text (15).jpg
Some Text (16).jpg
..
Some Text (101).jpg
Some Text (102).jpg

And I would like to add 0 befor the numbers, so that I have always 3 numbers. The upper examples would become:

Some Text (001).jpg
Some Text (002).jpg
..
Some Text (015).jpg
Some Text (016).jpg
..
Some Text (101).jpg
Some Text (102).jpg

Is there any way to automate that?

marked as duplicate by Costas, HalosGhost, techraf, Toby Speight, sam Dec 13 '16 at 16:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I think this question should have a million duplicate answers in stack exchange.... Are your file names containing spaces? – George Vasiliou Dec 13 '16 at 13:26
  • @GeorgeVasiliou yes – nbar Dec 13 '16 at 13:29
  • Does your system have the perl-based prename / rename command? – steeldriver Dec 13 '16 at 13:32
  • @steeldriver Yes it looks like $ prename Usage: rename [-v] [-n] [-f] perlexpr [filenames] – nbar Dec 13 '16 at 13:34
  • 1
    The \r usually indicates that you have DOS-style line endings in your script. That's a separate issue that you will need to address. If you want to use the perl-based rename, try prename -nv -- 's/\((\d+)\)/sprintf "(%03d)", $1/e' *.jpg. If that works satisfactorily, remove the n option. – steeldriver Dec 13 '16 at 13:58
0

Also this works ok :

for file in *.jpg
do 
  number=$(grep -Eo '[0-9]*' <<<$file)
  newname=$(sed "s/([0-9]*).txt/(0$number).txt/"<<<$file)
  #echo "old file = $file - new name=$newname"
  mv "$file" "$newname"
done

Not the answer you're looking for? Browse other questions tagged or ask your own question.