LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Rename numerical filenames so all have the same number of leading zeros (https://www.linuxquestions.org/questions/linux-newbie-8/rename-numerical-filenames-so-all-have-the-same-number-of-leading-zeros-4175526294/)

kmkocot 11-23-2014 05:30 PM

Rename numerical filenames so all have the same number of leading zeros
 
Hi all,

I have a folder with on the order of 300,000 files with numerical filenames. Some have filenames up to six digits in length and others have filenames as short as one digit in length randing from 0.fa to 300000.fa. I would like to add leading zeros to files having fewer than 6 digits in the filename. Here's what I've come up with but it isn't working on files with just two or one digit in their file name (they end up with four or five characters in their filename).

Code:

rename 'unless (/0+[0-9]{6}.fa/) {s/^([0-9]{1,5}\.fa)$/000$1/g;s/0*([0-9]{6}\..*)/$1/}' *.fa
Any assistance would be greatly appreciated.

Thanks!
Kevin

suicidaleggroll 11-23-2014 05:54 PM

Whenever I need to do this I just use a simple loop
Code:

for i in *.fa; do
  fname=$i
  while [[ ${#fname} -lt 9 ]]; do
      fname=0$fname
  done
  echo mv $i $fname
done

You could also separate the number from the extension and use a printf "%06d" if you wanted to go that route instead, eg:
Code:

for i in *.fa; do
  num=${i/.fa/}
  fname=$(printf "%06d.fa" $num)
  echo mv $i $fname
done

Of course with other of these you would want to run it first with the echo in place to make sure it's working correctly, then remove the echo and run it again to actually do the operation.

grail 11-23-2014 07:37 PM

I am with the above post as far as what I would do, however, I see no reason why rename (perl version) should not work.

Looking at what you have tried, there are a number of issues I noted:

1. unless (/0+[0-9]{6}.fa/) - correct me if I am wrong, but are you not saying here that it has to be a minimum of 7 characters long?

2. Also, the above would assume that all file names start with a zero. Is this correct?

3. s/^([0-9]{1,5}\.fa)$/000$1/g; - not being a perl guru I will assume $1 is for the back-reference (as opposed to \1). Here we add three zeroes to the start, but if only one digit it will now only be 4 in length (prior to dot)

4. s/0*([0-9]{6}\..*)/$1/ - not sure why we suddenly went from '\.fa' to '\..*'? In addition to the above change, something which is only 4 characters long will never get changed by this


I know I did not really answer anything, but I hope some of this might help towards a solution.

kmkocot 11-24-2014 10:26 PM

Hi guys. Thank a lot for the help!

Grail, you pointed out two silly mistakes I made but in a nutshell, fixing issues 1 (changing that 6 to a 5) and 3 (five zeros instead of three, not sure what I was thinking there) made this code do what I wanted it to do.

Best,
Kevin

grail 11-25-2014 12:11 AM

Please remember to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 08:07 PM.