LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 05-31-2009, 02:30 AM   #1
samnjugu
Member
 
Registered: Apr 2006
Location: Atlanta
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142

Rep: Reputation: 16
Batch rename files


Am looking for a script that will batch rename a folder containing music files I have converted from wma to ogg, the converted ogg files have a name like trackname.wma.ogg. I have seen scripts that will take off the extension or the beginning of a file name but nothing about the center portion. Any help will be greatly appreciated.
 
Old 05-31-2009, 02:41 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
One way to do it would be to use awk (but there may be better ways):

file='stuff.wma.ogg'
newfile=`echo $file | awk -F '.' '{print $1 "." $3}'`

You just need to wrap that in a loop, e.g.

Code:
for file in *.wma.ogg
do
  # Do stuff in here
done
This is the syntax for Bash, by the way. See this if you want to learn more about Bash scripting.
 
Old 05-31-2009, 10:54 AM   #3
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 9,166

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
Code:
rename wma.ogg ""ogg *ogg
The above works if you're in the directory with the files, will change all files ending in ogg.
 
Old 05-31-2009, 02:25 PM   #4
samnjugu
Member
 
Registered: Apr 2006
Location: Atlanta
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142

Original Poster
Rep: Reputation: 16
Am running a Centos 5.3 which doesn't have the rename cmd enabled "I have tried it" as a lot of scripts on the net use it. I will try the first script and see if I can make it work.
 
Old 05-31-2009, 03:13 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679
You could simply use
mv "${file}" "${file%.wma.ogg}.ogg"

This removes ".wma.ogg" and adds the .ogg back.

You can also use
mv "${file}" "${file/.wma.ogg/.ogg}"

Suppose that you want a more general solution that keeps the first part, and the last part, regardless of the extensions used:
mv "${file}" "${file%%.*}.${file##*.}"

If you can use variable expansion instead of an external program, you may find dramatic speed increases.
Especially since you want to execute it inside a loop.
 
Old 06-01-2009, 02:00 PM   #6
samnjugu
Member
 
Registered: Apr 2006
Location: Atlanta
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142

Original Poster
Rep: Reputation: 16
Thank you everyone for your replies worked like a charm the scripts work great. This is what makes linux great.
 
Old 06-01-2009, 04:51 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012Reputation: 2012
The perl installation in Debian and related distros (and maybe some others, I'm not sure which ones have it and which don't) includes a rename utility that has a syntax like sed. So if you have it installed you can run:

rename 's/.wma.ogg/.ogg/' *


There are also several gui and cli batch-renaming programs available, such as krename, gprename, mrename, pyrenamer, to name a few I found listed in apt.

One neat package I've found is called renameutils. One of the included programs is "qmv", which will load a group of filenames into a text editor so you can manually alter all of them at once, and when you save the text it will rename everything according to the changes you made.


Most tagging tools for audio files also include a renaming feature. I usually use easytag myself, for instance, and you can do string substitutions on the filename with it.


In general though, in scripts the parameter substitution technique detailed by jschiwal is the most efficient for command-line use, it being a bash built-in function. You can use it in any command, not just mv, so it might be possible to use it directly in whatever command or script you're using for the original conversion, for example.
 
Old 06-02-2009, 11:06 AM   #8
samnjugu
Member
 
Registered: Apr 2006
Location: Atlanta
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142

Original Poster
Rep: Reputation: 16
Hi David am still new to bash scripting so I will try and work it into the conversion script and see if I can run it all at once. Actually that is starting to sound like such a nice idea I can't wait to try it out, as I was having to run the scripts separately. I will also try it with other commands too, thank for your suggestions.
 
  


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
Trying to batch rename some files in bash, no luck. Any ideas? sajro Linux - Newbie 17 03-26-2010 04:57 PM
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 02:26 PM
Batch Rename shelfitz Linux - Newbie 19 10-12-2006 11:23 PM
Batch rename question hellblade Linux - Software 4 05-03-2004 04:57 PM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 07:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 11:41 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
Open Source Consulting | Domain Registration