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'm trying to rename my movie collection. All of the files are currently named using dots instead of spaces, i.e. Men.in.Black.avi. I want to replace all of the dots with spaces which isn't terribly difficult, but I need to preserve the last dot for the file extension, i.e. .avi, .mp4, .ogg, etc.

My Googling has provided no solutions. I'm also a Javascript developer and could see some snazzy applications for it. So, any suggestions?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

One way would be to use the Perl rename utility. The Perl regex \.(?=.*\.) matches a dot only when there is another dot later.

rename 's/\.(?=.*\.)/ /g' *.*.*

If you want to act on files in subdirectories as well:

shopt -s globstar    # you can put this in your ~/.bashrc
rename 's/\.(?=.*\.)/ /g' **/*.*.*

This works assuming you don't have dots in your directory names. Otherwise I think the simplest method would be to use find:

find -depth -name '*.*.*' -exec rename 's/\.(?=.*\.)/ /g' {} +

Alternatively, using zsh (this only renames regular files and leaves directory names alone, due to the glob qualifier (.) at the end):

autoload zmv    # you can put this in your ~/.zshrc
zmv -Q '(**/)(*.*)(.[^.]##)(.)' '$1${2//./ }$3'
share|improve this answer
    
Once I finally went back and tested it this worked swimmingly. I'm also now using it to dynamically rename some file uploads on my site. Thanks so much! –  trezy Nov 2 '12 at 17:46

While writing this question it occurred to me that I hadn't checked the Ubuntu Software Center. I found an app, pyRenamer, which did a fantastic job of accomplishing my task. I ended up just changing all of the dots to spaces, then changing all instances of the file extensions that were preceded by a space, i.e. " avi", " mp4", " ogg", to replace the spaces with dots. This isn't the best option as there could be a movie with avi or ogg in the title which would be a major pain in the bum, but it did the trick this time.

I'm still interested in the simpler RegEx solution.

share|improve this answer
cd $movielibrary
rename -n 's/\./ /g;s/ ([^ ]*$)/.$1/' *

Does it look good? Rerun the last command without -n.

share|improve this answer

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.