John Bokma MexIT
freelance Perl programmer

Using Emacs Dired to rename files the easy way

Monday, March 30, 2009 | 0 comments

At the end of the afternoon Esme complained about her Creative Zen Nano not playing the mp3 podcasts I had downloaded for her last week in the correct order. It soon turned that some files had an extra leading zero in the filename, in total 3 digits, which caused the podcasts being played out of order now and then. I had missed this when I downloaded all podcasts using:

wget -r -l 1 -A mp3 http://www.lauraspeaksdutch.info/audio/

Meaning: download recursively, one level deep, everything that's an mp3 file.

Advertisements:    Hire an Experienced Perl Developer    Python Programmer available    Software Developer for Hire

Esme had already connected the mp3 player to her Dell notebook and was about to manually rename them when I suddenly said: "Hold on, there is a much easier way, connect the player to my computer". My computer is a Dell Vostro 200 ST, currently running Ubuntu 8.10 GNU/Linux.

While Esme connected the Zen, I started an Emacs session, and said to her: I have to read how to do it exactly, but it should be a piece of cake. So I opened the GNU Emacs manual, often present on my desk since I am still not familiar with all the ins and outs of this editor, and looked up the chapter on the directory editor, Dired.

About a year ago I decided to switch to Emacs. It took quite some time to actually take the plunge, and it will take a lot more to feel really at home using Emacs, hence the book on my desk, even though I have been using the editor to edit Perl, XML, and plain text files for months now. And more recently: coding Python. Moreover, each time I code on Windows XP using TextPad I miss Emacs more and more. And the solution to this will be that I switch to Emacs on XP as well.

Anyway, back to renaming: I pressed C-x d (Ctrl-x followed by d) in Emacs to invoke the directory editor dired and typed /media/ES followed by tab at the prompt. Since there was only one device mounted starting with ES: ESMEZEN, the autocompletion finished the directory path, and I pressed return.

The root directory of Esme's Zen Nano opened in dired.
The root directory of Esme's Zen Nano opened in dired.

Next, I had to read up how I could make the directory listing editable. After browsing for a few seconds through the manual I came upon page 352, which showed the answer in section 29.16 "Editing the Dired Buffer":

M-x wdired-change-to-wdired-mode

Don't panic: you don't have to type the entire command. Emacs tries to complete it when you press tab. So I pressed the meta key, alt in my case, together with x, typed wdir followed pressing the tab key. Emacs completed it, and I pressed Enter.

Next, Emacs displayed a useful help message in the echo area: Press C-c C-c when finished or C-c ESC to abort changes. I removed the extraneous leading zeroes in the blink of an eye, pressed C-c C-c, that's Ctrl+C twice, to write the changes back to Esme's Zen Nano.

While at first it looks like more work, I am convinced that I could do it faster this way than renaming the files one by one via Explorer on Vista. And the next time I'll do it faster, because I now know how to do it.

Emacs Related

Advertisements:    Perl Coder For Hire    Software Developer for Hire    Custom Python Development

Also today

Please post a comment | read 0 comments | RSS feed