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 Thanks! Kevin |
Whenever I need to do this I just use a simple loop
Code:
for i in *.fa; do Code:
for i in *.fa; do |
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. |
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 |
Please remember to mark as SOLVED once you have a solution.
|
All times are GMT -5. The time now is 08:07 PM. |