dive into mark

You are here: dive into markArchivesJune 2007HOWTO recursively rename files with a lowercase file extension

Thursday, June 7, 2007

HOWTO recursively rename files with a lowercase file extension

In ZSH:

% autoload zmv
% zmv '(**/)(*).(*)' '$1$2.${(L)3}'

Affects all files in the current directory and all subdirectories. For example, it turns Serenity.Ogg into Serenity.ogg, no matter where it is under the current directory.

First parenthetical matches current directory and all subdirectories, second group matches filename sans extension, third group matches extension. ${(L)3} transforms the extension to lowercase.

If you only care about a particular extension, you can simplify1 it to this:

% zmv -W '(#i)**/*.mp3' '**/*.mp3'

The (#i) matches case-insensitively (.Mp3, .MP3, and so on). The -W flag lets you use regular * and ** wildcards in both the source and target patterns, and automagically turns them into groups and backreferences like the first example.

If you only care about a particular extension in the current directory (not subdirectories), it’s even easier:

% zmv -W '(#i)*.mp3' '*.mp3'

That’s all I know.

Further reading:

  1. For some definition of “simple” that includes enough line noise to choke a German Shepherd.

Filed under , , ,

5 comments

  1. The POSIX sh(1) version looks like (assuming GNU find and xargs)


    find . -type f -iname '*.mp3' -print0 |xargs -0n100 -P5 sh -c 'for f in "$@"; do mv "$f" "${f%???}mp3"; done' sh

    As a bonus it runs five threads in parallel for you.

    Comment by Andrew Neitsch — Thursday, June 7, 2007 @ 7:13 am

  2. What about filenames that look like this /a/b/c.d.e or /a/b.c/d.e

    Comment by Shakeel Mahate — Thursday, June 7, 2007 @ 7:59 am

  3. There is also rename.

    To translate uppercase names to lower, you’d use
    rename ‘y/A-Z/a-z/’ *

    If subdirectories are needed, use find with xargs:
    find ./ -type f -iname * -print0 | xargs rename ‘y/A-Z/a-z/’

    There is also:
    mrename
    krename
    kfilerename

    I recommend krename as it allows you to see your edits in “real time” before being applied. It has plugins that allow it to function based on permissions, date and time, do encoding conversions and transliteration.

    Comment by Anonymous — Thursday, June 7, 2007 @ 9:44 am

  4. Remember kids, always use the -n flag first.

    Comment by Anonymous — Thursday, June 7, 2007 @ 10:39 am

  5. There’s also the (somewhat easier to grasp for some) mmv with its counterparts (mcp, mad and mln).

    Comment by Shot — Friday, June 8, 2007 @ 7:00 pm

Respond privately

I am no longer accepting public comments on this post, but you can use this form to contact me privately. (Your message will not be published.)



Recent Stuff For You, Special Price Stay Here
  • Greasemonkey Hacks
Good Stuff Buy The Cow Go Away
Dive Into Python
Powered by Google Drink The Milk Don't Steal

 

posts / comments
© 2001-8 Mark Pilgrim