0

This question already has an answer here:

I have multiple folders named as follow:

Name1  
Name2  
...  
Name9  
Name10  
Name11  
...  

I need to rename them using mv command into:

Name01  
Name02  
...  
Name09  
Name10  
Name11  
...  

Any ideas?

marked as duplicate by don_crissti, roaima, Archemar, chaos, G-Man Sep 10 '15 at 15:36

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.

0

You seem to be actually renaming only 1-9, so that simplifies things tremendously:

for f in `seq 0 9`
do
  mv Name${f} Name0${f}
done

If you start moving into triple digits, things get a bit more complicated, but not insurmountably so:

for f in `seq 0 95`
do
  g=`printf %03.f $f`
  mv Name${f} Name${g}
done
  • Actually i dont have all the folders of the sequence from 0-9. i might have Name2 and Name5 missing. But yet your solution is working fine. I only get a warning for the missing sequence folders. – MatrixStandard Sep 10 '15 at 12:51
  • for avoiding errors with non-existing directories, use e.g. for f in name? ; do mv $f ${f//name/name0} ; done – Fiximan Sep 10 '15 at 13:37
  • 10x @Fiximan! That's what i was looking for. – MatrixStandard Sep 11 '15 at 7:28
0
rename -v 's/(\w)(\d)$/${1}0${2}/' Name[0-9]
  • Can't use rename. I need to change the name of folders not files. Thanks anyway! – MatrixStandard Sep 11 '15 at 6:46
  • @MatrixStandard rename can operate all notwithstanding folders or files. If you have files which you do not like to rename you can use Name[0-9]/ just for folders. Or more correct to make it through find: find ./* -prune -type d -name 'Name[0-9]' -exec rename -v 's/(\w)(\d)$/${1}0${2}/' {} + – Costas Sep 11 '15 at 8:10

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