LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2014, 06:30 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
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
 
Old 11-23-2014, 06:54 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Last edited by suicidaleggroll; 11-23-2014 at 06:57 PM.
 
1 members found this post helpful.
Old 11-23-2014, 08:37 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
1 members found this post helpful.
Old 11-24-2014, 11:26 PM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
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
 
Old 11-25-2014, 01:11 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please remember to mark as SOLVED once you have a solution.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to remove leading zeros in a function santosh0782 Linux - Newbie 5 03-05-2014 04:53 AM
BASH: Keeping/replacing leading zeros with BC Checksumfail Programming 3 07-20-2012 04:29 PM
How to add leading zeros to numbers for file names rootaccess Linux - General 20 06-16-2012 08:35 PM
Renaming files (adding leading zeros) General Linux - Software 3 01-09-2011 10:17 AM
[Bash] Rename filenames with corresponding filenames Power2All Linux - Software 4 12-02-2009 05:15 AM


All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration