Difference between revisions of "Renumber files"

From CCP4 wiki
Jump to navigationJump to search
 
Line 1: Line 1:
 
Let's say you have a list of images named something_###.img with ### spanning 381-560, and you want to renumber them to be from 1 to 180.
 
Let's say you have a list of images named something_###.img with ### spanning 381-560, and you want to renumber them to be from 1 to 180.
  
'''echo | awk '{for(i=1;i<181;i++) printf "mv something_%03d.img something_%03d.img\n",i+380,i;}' | bash -sf'''
+
 
 +
== Bash/awk one-liner ==
 +
 
 +
 
 +
  echo | awk '{for(i=1;i<181;i++) printf "mv something_%03d.img something_%03d.img\n",i+380,i;}' | bash -sf
  
 
will do it.  Notice that "%03d" will make sure that numbers are padded with zeros (and if you need 4 digits total, just use %04d instead).  Also, always pipe it to less first before piping to bash, just to make sure that you are not going to inadvertently delete something.
 
will do it.  Notice that "%03d" will make sure that numbers are padded with zeros (and if you need 4 digits total, just use %04d instead).  Also, always pipe it to less first before piping to bash, just to make sure that you are not going to inadvertently delete something.
  
 +
== Bash one-liner ==
 +
 +
  c=1 && for f in something_{381..560}.img ; do mv $f something_`printf "%03d" "$c"`.img ; c=$(($c+1)) ; done
 +
 +
== Bash script ==
 +
 +
  #!/bin/bash
 +
  # set your first desired image number
 +
  c=1
 +
 
 +
  # loop on the input file names.  the {X..Y} is called brace expansion.
 +
  for f in something_{381..560}.img ; do
 +
 
 +
  # infinitely useful printf as you noted
 +
    mv $f something_`printf "%03d" "$c"`.img
 +
 
 +
  # increment the counter
 +
    c=$(($c+1))
 +
  done # loop ends
 +
 +
== Perl one-liner ==
 +
 +
  perl -e 'for(<*>) {rename $_,$1.($2-380).$3 if /(.+?)(\d+)(.img)$/}'
  
 
Back to [[Useful scripts (aka smart piece of code)]]
 
Back to [[Useful scripts (aka smart piece of code)]]

Latest revision as of 15:04, 16 December 2010

Let's say you have a list of images named something_###.img with ### spanning 381-560, and you want to renumber them to be from 1 to 180.


Bash/awk one-liner[edit | edit source]

 echo | awk '{for(i=1;i<181;i++) printf "mv something_%03d.img something_%03d.img\n",i+380,i;}' | bash -sf

will do it. Notice that "%03d" will make sure that numbers are padded with zeros (and if you need 4 digits total, just use %04d instead). Also, always pipe it to less first before piping to bash, just to make sure that you are not going to inadvertently delete something.

Bash one-liner[edit | edit source]

 c=1 && for f in something_{381..560}.img ; do mv $f something_`printf "%03d" "$c"`.img ; c=$(($c+1)) ; done

Bash script[edit | edit source]

 #!/bin/bash
 # set your first desired image number
 c=1
 
 # loop on the input file names.  the {X..Y} is called brace expansion.
 for f in something_{381..560}.img ; do
 
 # infinitely useful printf as you noted
   mv $f something_`printf "%03d" "$c"`.img
 
 # increment the counter
   c=$(($c+1))
 done # loop ends

Perl one-liner[edit | edit source]

 perl -e 'for(<*>) {rename $_,$1.($2-380).$3 if /(.+?)(\d+)(.img)$/}' 

Back to Useful scripts (aka smart piece of code)