Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a large folder of .m4b audio books which in their current format won't play on my Android phone. However they do work fine if they are renamed to .m4a

Is there a quick method or terminal command that can rename every .m4b file in a folder to .m4a? There is no need for any conversion of the files, simply renaming the file extension works perfectly fine.

share|improve this question
2  
FYI: rename is a PERL script and accepts regular expressions. Debian systems also have a rename.ul command as part of the util-linux-ng package. If perl is not installed (ok, highly unlikely ;) ) rename also is not. –  Rinzwind May 22 '12 at 6:42

2 Answers 2

up vote 6 down vote accepted

This will do the job for you.

rename 's/.m4b$/.m4a/' *.m4b

For a test run you can use this command:

rename 's/.m4b$/.m4a/' *.m4b -vn

-v means "verbose" and it will output the names of the files when it renames them.

-n will do a test run where it won't rename any files, But will show you a list of files that would be renamed.

share|improve this answer
    
Much better answer than mine. I was being lazy and trying to not use regular expressions... –  adempewolff May 22 '12 at 6:10
    
Thank you, works a treat. Thanks to @adempewolff for the effort too –  coversnail May 22 '12 at 6:15

A very quick way to rename files, if that is all you need to do, and do not need to convert them to another format, is to use Bash's parameter expansions, which are detailed very nicely at the Bash wiki.

There are several different ways of changing the extension, but I use here the simple ${var/original/replacement} paradigm:

for file in *.m4b; do mv -- "${file}" "${file/%m4b/m4a}"; done

If you want to see what would be changed by the command, place echo before mv and the changes will be listed.

Needless to say this oneliner could be modified for other files too, and you can also use parameter expansions to remove file extensions too.

share|improve this answer
    
+1 This way works the same as the answer by @Basharat Sial except it will also work when you have lots of files matching *.m4b. Otherwise you will hit the error argument list too long. –  matt burns Jun 13 at 8:53
    
Also, to watch it happen, add the verbose flag: for file in *.m4b; do mv -v -- "${file}" "${file/%m4b/m4a}"; done –  matt burns Jun 13 at 8:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.