LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-08-2011, 05:55 PM   #1
General
Member
 
Registered: Aug 2005
Distribution: Debian 7
Posts: 526

Rep: Reputation: 31
Renaming files (adding leading zeros)


I need to rename some files. They are named:

NP1-1.mp3
NP1-2.mp3
NP1-3.mp3
...
NP1-4120.mp3
NP1-4121.mp3

However, as there is no lead 0, the order gets confused by some software (some software mistakenly lists it as NP1-1.mp3, then NP1-10.mp3). I'd like to add zeros so the files are named like this:

NP1-0001.mp3
NP1-0002.mp3
NP1-0003.mp3
...
NP1-4120.mp3
NP1-4121.mp3

How can that be done?
 
Old 01-08-2011, 07:35 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
I'm sure there are better ways, but if all of your files are named like that you could try
Code:
for i in NP1*.mp3
  do
    mv ${i} $(echo ${i} | sed -e 's/-/-000000/' \
                -e 's/-0*\([0-9]\{4,\}\)\(.mp3\)/-\1\2/')
  done
if you limit to only 4-wide you may get errors about the already 4-wide files being the same. If you want a 5-wide change the \{4, to \{5,.

 
Old 01-08-2011, 08:45 PM   #3
RSridhar
LQ Newbie
 
Registered: Sep 2009
Posts: 13

Rep: Reputation: 4
better would be to use
rename - -0 NP1-?.mp3
rename - -0 NP1-??.mp3
rename - -0 NP1-???.mp3

The globbing char ? matching with file name
 
1 members found this post helpful.
Old 01-09-2011, 10:17 AM   #4
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
@RSrdhar, thanks. I knew there had to be a better way. All these years and somehow I've managed to never discover rename. So much simpler for OP.
 
  


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
Renaming Files..Help npeddle Linux - Newbie 9 12-20-2010 03:17 PM
Renaming files tekmann33 Linux - Newbie 6 05-22-2009 11:08 AM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 03:06 PM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 11:14 AM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 02:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:04 PM.

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