Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
There are less than 24 hours left to vote in the 2011 LinuxQuestions.org Members Choice Awards. Click here to go to the polls. Vote now and make sure your voice is heard!
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142
Rep:
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.
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142
Original Poster
Rep:
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.
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.
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.
Distribution: Centos, knoppix, Fedora, Mepis, Zenwalk, Mint
Posts: 142
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.