Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Batch file rename with shell script?

Batch file rename with shell script?
Thread Tools
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Jun 30, 2008, 11:57 AM
 
I have a whole bunch of iTunes files in various folders with a " 1" suffix like this
~/Music/iTunes/.../SomeSong 1.m4a
and I want to get rid of that suffix (they're leftovers from a batch conversion).

There must be an easy way to get this done in the shell, but I'm not a great shell programmer...

I know if you want to add something to a file name it can be done easily in the shell like this
code:
for i in *.m4a; do mv $i "The Prefix"$i; done;


But what if I want to remove part of the file name? Any suggestions how to remove this " 1" from the file names in the shell?
     
Addicted to MacNN
Join Date: Aug 2004
Location: FFM
Status: Offline
Reply With Quote
Jun 30, 2008, 02:01 PM
 
     
Simon  (op)
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Jun 30, 2008, 03:55 PM
 
Nice!

I'd still like to see a shell script solution though.
     
Mac Elite
Join Date: Apr 2000
Location: Los Angeles, CA
Status: Offline
Reply With Quote
Jul 1, 2008, 11:34 AM
 
This works for bash:

Code:
for i in *.m4a do name=$(echo $i | sed -e "s/ 1.m4a$/.m4a/") mv "$i" "The Prefix$name"; done;
Replacing all occurrences of files ending in "1.m4a" with ".m4a" (because " 1" might appear elsewhere as part of the filename). Note you also want to wrap the filenames in quotes for mv, especially as they may contain spaces.
     
Simon  (op)
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Jul 1, 2008, 11:52 AM
 
Awesome, thanks!
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:48 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2