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.

§

Five comments here (latest 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.

    — Andrew Neitsch #

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

    — Shakeel Mahate #

  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.

    — Anonymous #

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

    — Anonymous #

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

    — Shot #

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.)



§

firehosecodeplanet

© 2001–9 Mark Pilgrim